Java Spring MVC 视图层的 JSP 替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2746598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternatives to JSP for Spring MVC view layer
提问by digitaljoel
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?
我希望从头开始创建一个新的应用程序,并且可能会使用 Spring MVC 和 Spring Web Flow。Spring Roo 创建的项目使用 Spring MVC 和可选的 Web Flow。视图技术有哪些好的替代方案,或者 JSP 与 Spring 和 JSTL taglibs 和 jQuery 是要走的路吗?
采纳答案by BalusC
In the standard Java EE API, the only alternative to JSP is Facelets. As far now (2010) JSFis the only MVC framework which natively supports Facelets.
在标准的 Java EE API 中,JSP 的唯一替代品是Facelets。到目前为止(2010 年)JSF是唯一一个原生支持 Facelets 的 MVC 框架。
Spring MVC supports out of the box only JSP, but it has a configurable view resolverwhich allows you to use Faceletsanyway. Other candiates are 3rd party templating frameworks such as Velocity, Freemarker, and Thymeleafwhich can be configured as a view technology for Spring MVC. Spring documentation has integration exampleswith Velocity and Freemarker.
Spring MVC 仅支持开箱即用的 JSP,但它有一个可配置的视图解析器,它允许您无论如何都使用Facelets。其他候选者是 3rd 方模板框架,例如Velocity、Freemarker和Thymeleaf,它们可以配置为 Spring MVC 的视图技术。Spring 文档有与 Velocity 和 Freemarker 的集成示例。
回答by Quotidian
回答by Athens Holloway
回答by 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' criticismseems valid:
(我之前的回答在这里已经过时了。)Freemarker 至少和 Velocity 一样好。但是 Thymeleaf 看起来更加引人注目,加上布局方言,它可能使像 sitemesh 和 tile 之类的模板框架变得不必要。对于 JSF,Thoughtworks 的批评似乎有道理:
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.
我们继续看到团队在使用 JSF(JavaServer Faces)时遇到问题,并建议您避免使用这种技术。团队似乎选择 JSF,因为它是 Java EE 标准,而没有真正评估编程模型是否适合他们。我们认为 JSF 是有缺陷的,因为它的编程模型鼓励使用自己的抽象,而不是完全拥抱底层 Web 模型。JSF 与 ASP.NET webforms 一样,尝试在顶部 HTML 标记和无状态 HTTP 协议上创建有状态组件树。JSF 2.0 和 2.2 中的改进,例如引入无状态视图和推广 GET,是朝着正确方向迈出的一步,甚至可能承认原始模型存在缺陷,但我们觉得这为时已晚。
回答by yfrangi
Spring MVC provides integration with many different view technologies. I would recommend using FreeMarkeror Velocity.
Spring MVC 提供了与许多不同视图技术的集成。我建议使用FreeMarker或Velocity。
回答by rompetroll
Springs 3 documentationalso suggests FreeMarker. Freemarker is (as far as I can tell) fast and has some integration of Spring features like binding.
Springs 3 文档还建议使用FreeMarker。Freemarker(据我所知)速度很快,并且集成了一些 Spring 功能,例如绑定。
回答by monzonj
You can have as many view technologies as you want on Spring MVC. I have FreeMarkerand 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.
您可以在 Spring MVC 上拥有任意数量的视图技术。我有FreeMarker和 JSP 视图解析器。当我遇到一个在 FreeMarker 中太复杂的视图(或者在 JSP 中更方便)时,我创建了一个 JSP 视图。例如,带有 JSTL 的 Spring 可以很好地处理表单。为此,我使用 JSP 视图,但对于其他几乎所有内容,我都有 FreeMarker 视图。
Have a look to the Spring MVC documentation to see how to configure several view resolvers, basically:
查看 Spring MVC 文档以了解如何配置多个视图解析器,基本上:
<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>
回答by Adam Gent
While this is an old question I thought I would offer an up-and-coming alternative which is Scalate.
虽然这是一个老问题,但我认为我会提供一个新兴的替代方案,即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).
Scalate 在模板选项方面是强大的。唯一的问题是 Scalate 需要很多依赖项(虽然它需要 Scala,但不需要你用 Scala 编写)。
My current favorite though is Handlebars.javawhich does have Spring integration.
我目前最喜欢的是Handlebars.java,它确实集成了 Spring。
回答by digitaljoel
I recently started going with plain HTML and jQuery for presentation with Spring MVC only creating a JSON view.
我最近开始使用纯 HTML 和 jQuery 进行演示,Spring MVC 只创建了一个 JSON 视图。
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.
到目前为止,它进展顺利,即使我必须做 javascript 工作,它也使与我的设计师的交互更加容易,并且在他有更改时可以更快地周转时间,因为我不必将他的 HTML 转换为我的 JSP。整体站点的可维护性尚无定论。