2

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?

Community
  • 1
  • 1
Alex R
  • 11,364
  • 15
  • 100
  • 180
  • How about python/php/groovy or do you want java only? – Papasmile Oct 22 '13 at 19:30
  • None of these 3 will give you statically typed binding at compile time, since they are dynamic interpreted languages. Or am I missing something? – Alex R Oct 22 '13 at 22:51
  • sorry, now re-reading "typed binding"... – Papasmile Oct 23 '13 at 17:15
  • 1
    Seeing as how 'template languages' attempt to *de-couple* model from view... that is a little counter-intuitive. Are you just trying get red errors in your IDE or do you have another reason? – Papasmile Oct 23 '13 at 17:57
  • That's it, you hit the nail on the head. I have an aging PHP codebase that I want to refactor. Having an IDE that can catch errors is nice. I don't mind rewriting in a different language as part of the refactoring. – Alex R Oct 24 '13 at 00:17
  • Check out http://groovy.codehaus.org/Eclipse+Plugin and http://pydev.org/ eclipse plugins and see what you think. – Papasmile Oct 24 '13 at 21:09
  • http://www.3pintech.com/products/fast-code/ , http://www.normalesup.org/~simonet/soft/ow/eclipse-closure-templates.en.html – Papasmile Oct 24 '13 at 21:10
  • Why do you need something like that? – Luiggi Mendoza Oct 25 '13 at 20:39
  • @Papasmile, thank you for the links; the 4th one (Closure Templates) looks promising, the other three not so much. I need to investigate Closure Templates. – Alex R Oct 26 '13 at 00:10
  • @Luiggi, I like to have compile-time checking of variable names... it's similar to having a spell-checker in your email application. It's always better to catch the typo before you hit 'Send'. – Alex R Oct 26 '13 at 00:10

5 Answers5

2

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.

Victor Nazarov
  • 825
  • 8
  • 11
  • Amazing, first correct answer in 3 years! Thank you – Alex R Nov 04 '16 at 16:21
  • In the intervening 3 years since posting my question, the PHP code has been rewritten in Spring MVC with Thymeleaf. Thymeleaf is uniquely nice (serverless testing in the browser is a huge productivity boost for UI implementation), but the lack of compile-time binding still bothers me. Now if someone could just create a Static-Thymeleaf... THAT would be awesome! – Alex R Nov 04 '16 at 16:23
1

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

Papasmile
  • 624
  • 1
  • 4
  • 22
  • I could do that with PHP, using the built-in tokenizer to scan the template for references to undefined symbols in the data classes. But, this being a rewrite, I want to make things simpler, not worse than they already are. Writing source code scanners is definitely more complicated than it needs to be. You are right JSP has evolved a bit since the last time I used it, thanks for pointing that out. – Alex R Oct 26 '13 at 00:03
1

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

Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42
1

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.

Henry Florence
  • 2,848
  • 19
  • 16
0

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.

Tormod Hystad
  • 2,345
  • 1
  • 19
  • 15