简体   繁体   中英

Any Type-safe alternatives to JSP templating

Is there something safer than JSP? I haven't found any meaningful answers through Google. We currently use the Spring MVC framework and JSP to render HTML. I have came to hate the dynamic nature of the JSP rendering, in that you reference a model like ${model.someAttribute} but have no way of telling whether this attribute is available at the given module or specifies the type of the model that the template expects. It becomes very difficult to refactor the models' getters since Eclipse doesn't know which templates use the model and also cannot relate the getters to the property accessors in templates.

Is there a templating technology out there, that is both less ugly than spitting out HTML code via imperative code and more type/compile safe than JSPs?

You can use type hints, eg the type attribute in jsp:useBean and in the attribute declarations of JSP tag files (and probably in other places like JSTL tags). IntelliJ will then do all the good static type-based stuff for you (accurate code completion suggestions, refactoring, find usages, show quick javadoc, etc.).

I just recently wrote about this in the context of JSP tag files .

Lift (based on Scala ) offers a very nice model with good separation of the HTML layout from the actual application code (called snippets). While the barrier to entry may look high at first, the gains are really big in the somewhat longer term. One big upside is that it runs on the JVM so you can re-use most of your Java libraries and other code.

I believe JXT 's goals include both XSS safety and type model safety, and it was designed to be easy to transition to from JSPs.

Their pipeline description explains that their expressions map more closely to Java expressions so you will get Java-esque type errors if your expressions have a type mismatch.

Haven't actually used it, but Twirl (https://www.playframework.com/documentation/2.5.x/JavaTemplates ) calls itself "a type safe template engine based on Scala" (can apparently be used in Play for Java).

Quote:

A type safe template engine based on Scala Play comes with Twirl, a powerful Scala-based template engine, whose design was inspired by ASP.NET Razor. Specifically it is:

compact, expressive, and fluid: it minimizes the number of characters and keystrokes required in a file, and enables a fast, fluid coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. The parser is smart enough to infer this from your code. This enables a really compact and expressive syntax which is clean, fast and fun to type.

easy to learn: it allows you to quickly become productive, with a minimum of concepts. You use simple Scala constructs and all your existing HTML skills.

not a new language: we consciously chose not to create a new language. Instead we wanted to enable Scala developers to use their existing Scala language skills, and deliver a template markup syntax that enables an awesome HTML construction workflow.

editable in any text editor: it doesn't require a specific tool and enables you to be productive in any plain old text editor.

Disclaimer: I'm the author of Hepek . Currently, it supports only the Play framework.

Scalatags gives you safe HTML by construction. On top of it, Hepek has really nice API for constructing forms, grid, code highlighting etc. Basically, it is just Scala, no template parsing, no worrying does a variable exists.. :)

I think Rocker is exactly what you've been looking for. It is compiled, type-safe and fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM