ChatGPT解决这个技术问题 Extra ChatGPT

Alternatives to JSP for Spring MVC view layer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 7 months ago. The community reviewed whether to reopen this question 7 months ago and left it closed: Original close reason(s) were not resolved Improve this question

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?

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.
Kindly suggest the alternative .
Here is the related section in the official Spring MVC documentation: View Technologies.

S
Shawn Sherwood

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.


Thymeleaf by itself is great, but together with layout dialect layout dialect I love it!
Why should I use thymeleaf while facelets? Also, facelet's template approach looks like html and more..Why should we use it? What makes it a good thing according to facelets? for me "Nothing". maybe thymeleaf greater than jsp.So java ee most recent developed 'facelets' for the template language.And so JSP is old technology.Why would you compare with him?its an old technology?Sorry my bad English.
K
Koray Tugay

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.


"As far now (2010) JSF is the only MVC framework which natively supports Facelets." Did you mean "As far now (2010) Facelets is the only view technology which natively supports the JSF MVC framework."? Isn't Facelets (which is a view technology) supposed to suppport a MVC framework like JSF? (Sorry if what I say does not make sense, I am very new to JSF stuff).
Facelets is included in JSF itself since JSF 2.0. So you don't need to install it separately. Facelets itself can be used with any other MVC framework such as Spring MVC, but you'd have to install and configure it manually.
d
digitaljoel

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.


Can the generated restful API handle pagination?
your restful api can do whatever you want. For pagination just add a start and end or offset and page size or whatever you need to get the appropriate results.
Hi, I know this is late, but just a hint, Thymeleaf attempts to address the exact problem you mention in your answer (developer-designer interaction). It is probably worth a look, then.
thanks @acdcjunior. I went to a presentation on Thymeleaf at SpringOne and it looked pretty interesting and natural until the presenter said in his tests Thymeleaf was 8x - 10x slower than JSP.
Yeah, indeed it is slower. Check this thread: forum.thymeleaf.org/Performance-issue-td3722763.html . For me, that was the reason I didn't dive blindly into it (coz normally I develop for internet portals), but for "intranet" apps, those where JSF would be acceptable, I feel thymeleaf would be too. Hopefully, though, their performance improve in the future. Cheers!
i
informatik01

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>

i
informatik01

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


n
naXa stands with Ukraine

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


A
Adam Gent

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.


Interesting that it doesn't list any Spring compatibility. Have you successfully integrated it with Spring MVC?
stackoverflow.com/questions/5913373/scala-spring-and-scalate and github.com/scalate/scalate/tree/master/scalate-spring-mvc . I did something similar to the former. Spring is rather easy to integrate other stuff wiht.
@digitaljoel See also Using Scalate with Spring MVC
N
Nathan Hughes

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


Why is it the best alternative? The error checking in velocity is pretty bad. So why is it better?
@Adam: it is a lot simpler, and doesn't allow scriptlets, I count those as improvements over JSP. I remember writing tests for my velocity templates was pretty simple too.
Q
Quotidian

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.


A
Athens Holloway

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


d
ddsultan

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


Can you server side render that?
If I understand you correctly you would like to ask if Angular does serverside redering. Starting from Angular 2.x it provides this functionality. You can have a look at this reference: Server-side Rendering
Sorry I should have been more specific. I meant is there a way to server side render an Angular app within Java without a nodejs server?
There is one community project you might want to check for Java: Angular UniversalJ. I think it utilizes Node in the background.
A
Agassis

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


n
naXa stands with Ukraine

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


T
Tommy B

You can run Facelets ontop of Spring Webflow


l
larsig

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


H
Harshana

Apache Velocity a good alternate to Java Server Pages.