78
votes

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?

16
For anyone that is interested, I ended up going with JSP in the end and it has worked out very well. With the re-usability provided with jsp tag files and the nice taglibs available in spring and jstl it's not the horrible JSP I remember from 2004 with tons of scriptlets and all that.digitaljoel
Kindly suggest the alternative .praveenpds
Here is the related section in the official Spring MVC documentation: View Technologies.informatik01

16 Answers

53
votes

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.

50
votes

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.

17
votes

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.

16
votes

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>
16
votes

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

9
votes

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

8
votes

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.

5
votes

(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.

3
votes

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.

3
votes

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

3
votes

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

2
votes

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 !!!

2
votes

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

1
votes

You can run Facelets ontop of Spring Webflow

1
votes

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

1
votes

Apache Velocity a good alternate to Java Server Pages.