Alternatives to JSP for Spring MVC view layer

JavaSpringSpring MvcJsp

Java Problem Overview


I'm looking to create a new app from scratch and will probably use Spring MVC and possibly Spring Web Flow. The projects created by Spring Roo use Spring MVC and optionally Web Flow. What are some good alternatives for view technology, or is JSP with Spring and JSTL taglibs and jQuery the way to go?

Java Solutions


Solution 1 - Java

I recently discovered Thymeleaf.

It looks to be a complete replacement for JSPs and has integration with Spring MVC. The template approach looks more like HTML and may be more palatable to your UI designers. They have a small write-up that compares the two solutions side-by-side.

Solution 2 - Java

In the standard Java EE API, the only alternative to JSP is Facelets. As far now (2010) JSF is the only MVC framework which natively supports Facelets.

Spring MVC supports out of the box only JSP, but it has a configurable view resolver which allows you to use Facelets anyway. Other candiates are 3rd party templating frameworks such as Velocity, Freemarker, and Thymeleaf which can be configured as a view technology for Spring MVC. Spring documentation has integration examples with Velocity and Freemarker.

Solution 3 - Java

I recently started going with plain HTML and jQuery for presentation with Spring MVC only creating a JSON view.

So far it's going quite well and even though I have to do the javascript work, it makes for much easier interaction with my designer and quicker turnaround times when he has changes because I don't have to convert his HTML into my JSP. The jury is still out on overall site maintainability.

Solution 4 - Java

Springs 3 documentation also suggests FreeMarker. Freemarker is (as far as I can tell) fast and has some integration of Spring features like binding.

Solution 5 - Java

You can have as many view technologies as you want on Spring MVC. I have FreeMarker and JSP view resolvers. When I run into a view that it's too complicated in FreeMarker (or just more convenient in JSP) I create a JSP view. For instance, Spring with JSTL makes a great job handling forms. For that I use JSP views, but for pretty much everything else I have FreeMarker views.

Have a look to the Spring MVC documentation to see how to configure several view resolvers, basically:

<bean name="freeMarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
   <property name="cache" value="true"/>
   <property name="prefix" value=""/>
   <property name="suffix" value=".ftl"/>
   <property name="order" value="1"/> <!--NOTICE THE ORDER-->
</bean>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
    <property name="order" value="2"/> <!--NOTICE THE ORDER-->
</bean>

Solution 6 - Java

Spring MVC provides integration with many different view technologies. I would recommend using FreeMarker or Velocity.

Solution 7 - Java

While this is an old question I thought I would offer an up-and-coming alternative which is Scalate.

Scalate is powerhouse in templating options. The only probablem is that Scalate requires lots of dependencies (while it requires Scala it does not require you write in Scala).

My current favorite though is Handlebars.java which does have Spring integration.

Solution 8 - Java

(My previous answer was getting badly dated here.) Freemarker is at least as good as Velocity. But Thymeleaf is looking even more compelling, together with layout-dialect it may make template frameworks like sitemesh and tiles unnecessary. For JSF, Thoughtworks' criticism seems valid:

> We continue to see teams run into trouble using JSF - JavaServer Faces - and are recommending you avoid this technology. Teams seem to choose JSF because it is a Java EE standard without really evaluating whether the programming model suits them. We think JSF is flawed because its programming model encourages use of its own abstractions rather than fully embracing the underlying web model. JSF, like ASP.NET webforms, attempts to create stateful component trees on top HTML markup and the stateless HTTP protocol. The improvements in JSF 2.0 and 2.2, such as the introduction of stateless views and the promotion of GET, are steps in the right direction, maybe even an acknowledgement that the original model was flawed, but we feel this is a too little too late. Rather than dealing with the complexity of JSF we recommend teams use simple frameworks and work closely with web technologies including HTTP, HTML and CSS.

Solution 9 - Java

I use Stripes and Spring together. Stripes stays out of your way most of the time, but augments Spring nicely when you need it I find.

Solution 10 - Java

I am using velocity and Spring MVC. Also, i am hosting my application on Googles App engine and I have no issues.

Solution 11 - Java

You could also use Angular (Client side framework) for your View layer in Spring MVC.

Solution 12 - Java

I think Tiles could help you.
You can define templates and use JSTL inside.

Solution 13 - Java

My suggestions is not to look at view framework as described in most of the above which was not written on top of spring MVC since you will end up in issues like postbacks which means you won't be able to submit the data from this view technology and get back the response from sever. example like validation , edit data submission which refreshes back with data from server WILL NOT WORK .

This is because java beans in some above of view technology don't use Spring container lifecycle. You will only be able to use them for pure view example stateless request. example with JSF you won't be able to use postbacks since jsf postbacks only work if you use jsf life cycle and if you use spring framework JSF view resolver with spring mvc you won't be able to do postback so you need to replace jsf servlet controller instead of spring mvc controller .

Again since your full project requirement is not clear and if you want no postback requirement you can use some of above choices .

one example view technology which is written on top og spring mvc is zk framework based zk mvc in which you can extend your spring mvc controllers from ZK GenericForwardComposer to handle events. You can always use Spring to handle the lifecycle of these controllers using Spring framework.

you can google to find similiar other products .

This review is based on high level design of framework life cycle.

All the best !!!

Solution 14 - Java

You can run Facelets ontop of Spring Webflow

Solution 15 - Java

What about phpj?

It can be used as view templates or you can make your web server system from scratch

I made phpj because i dont like to having to update my application and load it with tomcat all the time, so with this i can use static locations for my web application using apache-like configurations

Solution 16 - Java

Apache Velocity a good alternate to Java Server Pages.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondigitaljoelView Question on Stackoverflow
Solution 1 - JavaShawn SherwoodView Answer on Stackoverflow
Solution 2 - JavaBalusCView Answer on Stackoverflow
Solution 3 - JavadigitaljoelView Answer on Stackoverflow
Solution 4 - JavarompetrollView Answer on Stackoverflow
Solution 5 - JavamonzonjView Answer on Stackoverflow
Solution 6 - JavayfrangiView Answer on Stackoverflow
Solution 7 - JavaAdam GentView Answer on Stackoverflow
Solution 8 - Javaпутин некультурная свиньяView Answer on Stackoverflow
Solution 9 - JavaQuotidianView Answer on Stackoverflow
Solution 10 - JavaAthens HollowayView Answer on Stackoverflow
Solution 11 - JavaddsultanView Answer on Stackoverflow
Solution 12 - JavaamanasView Answer on Stackoverflow
Solution 13 - JavaAgassisView Answer on Stackoverflow
Solution 14 - JavaTommy BView Answer on Stackoverflow
Solution 15 - JavalarsigView Answer on Stackoverflow
Solution 16 - JavaHarshanaView Answer on Stackoverflow