Question

I know JSP fits this description, but I'm looking for more modern alternatives.

Unfortunately it seems most modern languages and frameworks are dynamically typed, even the ones which are Java-based.

Related question (JSP was the only answer here): Are there any web frameworks for JVM with data binding checked at compilation time?

Était-ce utile?

La solution

This is rather old question, but I have an answer, since I've implemented static-mustache library to provide a type-safe template engine based on mustache syntax.

It checks both syntax errors and type-errors (like missing property) at compile-time. It requires zero build configuration as it's a standard annotation processor.

Templates remain pure mustache templates with all type-information extracted from normal Java-class used for rendering.

Autres conseils

First of all, JSP is on version 2.1 within Java Enterprise Edition; in other words, it is more modern that most languages out there.

Now, if want to get high-tech then how about checking all your beans' types yourself at compile time using java.lang.instrument? Scan your mappings and (your choice of any language) templates and check field name = such and such type, one by one. http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html

I have to apologize in advance since i dont quite understand the question, but...ill throw out 2 templating engine names i've used and liked, perhaps they will do what you need.

Dust

Mustache

This is an interesting question. I assume that the static type checking is required for data that is stored and retrieved from a database, since any data that is not will be generated at runtime rather than compile time. SQL (and most other relational databases) provide, what language writers calls static type checking, giving granular control over the data stored, therefore it would seem that a web framework is required that ensures strict mapping between the data types stored and how it is retrieved into the framework.

I have some experience using Hibernate with the Spring Framework, Spring Web MVC for rendering templates and storing the data in a MySQL database.

An entity definition for hibernate can look similar to the following: (there are lots of variations)

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "layoutItem", schema = "ivb")
public abstract class LayoutItem implements Serializable, DisplayItemInf {
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  private long id;

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name="LayoutSize")
  private LayoutSize size;

  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name="LayoutPosition")
  private LayoutPosition position;

  @Column(name = "display")
  private int display;

  @Column(name = "styleItem")
  private String styleItem;

  private String CSSclass = "";

  public void addClass(String CSSclass) {
    this.CSSclass = CSSclass+" ";
  }

  ...other getter and setter definitions...

}

All the fields are given static data types, hibernate then maps these to corresponding types for the fields within the MySql tables. The advantage of using Spring is that they provide a version of Eclipse called the The Spring Tool Suite, every time a file is saved the relevant class is recompiled and the redeployed to a locally running Server. This will then throw Type errors if any issues occur within the entity mappings to the database. Hibernate provides many annotations that control the Types and are as granular as those provide by SQL.

Within the framework several different layers are created with the data access layer being at the bottom of the pile, business logic and web site presentation being higher up. This more layered approach to creating a framework means that the data is strictly typed towards the bottom of the pile before Business logic is carried out and then passed to the more loosely typed MVC section for rendering to a template (JSP for example) and displaying as a web page.

Whether this approach is correct for your situation would, I guess, need careful assessment. IMHO Spring is a fairly heavy weight solution compared to Django (which also has entities, although not as strictly mapped), or Ruby On Rails (no experience here :). There are probably other methods to couple Hibernate for static typing and JSP for templates, although the benefits of using STS are definitely a plus with Spring. Lift uses the strictly typed Scala language and also runs on the JVM, it is not as heavy weight as Spring so would probably be quicker to deploy.

If you want to venture outside of the JVM you have Asp.Net MVC with Razor views, all the compile time checking you can ask for, with strongly typed views. As a bonus you get great intellisense and refactoring abilities.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top