使用 JSF 作为 Spring MVC 的视图技术

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18744910/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:13:51  来源:igfitidea点击:

Using JSF as view technology of Spring MVC

springjspjsfspring-mvcprimefaces

提问by Astronaut

I am currently implementing a small Spring MVC PoC, and I would like to use JSF as the view technology since most people in my company are used to a J2EE with Primefaces environment.

我目前正在实现一个小的 Spring MVC PoC,我想使用 JSF 作为视图技术,因为我公司的大多数人都习惯了带有 Primefaces 环境的 J2EE。

Does Spring MVC 3 support JSF, or simply JSP? I have read multiple articles mixing the two.

Spring MVC 3 支持 JSF 还是简单的 JSP?我已经阅读了多篇文章,将两者混合在一起。

My need is to create an appealing UI. Is there a simple way to do this using Spring MVC with the JSP as the view technology?

我的需要是创建一个吸引人的用户界面。有没有一种简单的方法可以使用 Spring MVC 和 JSP 作为视图技术来做到这一点?

Our application uses schedules/calendars in multiples pages. It's basically a time management APP

我们的应用程序在多个页面中使用时间表/日历。它基本上是一个时间管理APP

回答by BalusC

You're making a conceptual mistake. JSF is not a view technology. JSF is a MVC framework. Exactly like as Spring MVC, albeit they have both a different ideology; JSF is component based MVC and Spring MVC is request based MVC. Thus they are full competitors. You cannot mix them. You should choose the one or the other. Instead, JSP and Facelets are true view technologies. Since Java EE 6 (December 2009), JSP is deprecatedand replaced by Facelets (XHTML) as default view technology for JSF.

你犯了一个概念上的错误。JSF 不是视图技术。JSF 是一个 MVC 框架。与 Spring MVC 完全一样,尽管它们具有不同的意识形态;JSF 是基于组件的 MVC,而 Spring MVC 是基于请求的 MVC。因此他们是完全的竞争者。你不能混合它们。您应该选择其中之一。相反,JSP 和 Facelets 是真正的视图技术。自 Java EE 6(2009 年 12 月)以来,JSP 已被弃用,取而代之的是 Facelets (XHTML) 作为 JSF 的默认视图技术。

You can use Spring MVC with JSP view technology. You can also use Spring MVC with Facelets view technology(and many others). But you can not use Spring MVC with JSF components let alone with JSF component libraries like PrimeFaces. JSF output components may work, but JSF input components won't work at all. Spring MVC has already its own <form:xxx>tags for input. Even if you mix them, you will end up with half of the functionality from both frameworks in a mingled and confusing code base. This is not making any sense. If all you want is to use the same UI as PrimeFaces, just grab jQuery UI. It's also exactly what PrimeFaces is using under the covers. PrimeFaces is a jQuery-based JSF component library.

您可以将 Spring MVC 与JSP 视图技术结合使用。您还可以将 Spring MVC 与Facelets 视图技术(以及许多其他技术)结合使用。但是你不能将 Spring MVC 与 JSF 组件一起使用,更不用说像 PrimeFaces 这样的 JSF 组件库了。JSF 输出组件可能工作,但 JSF 输入组件根本不起作用。Spring MVC 已经有自己<form:xxx>的输入标签。即使您将它们混合在一起,您最终也会在混合且令人困惑的代码库中获得来自两个框架的一半功能。这没有任何意义。如果您只想使用与 PrimeFaces 相同的 UI,只需获取jQuery UI。这也正是 PrimeFaces 在幕后使用的内容。PrimeFaces 是一个基于 jQuery 的 JSF 组件库。

From the other side on, it can also be very good that you confused Spring IoC/DI with Spring MVC. Spring IoC/DI is in turn usabletogether with JSF. You can replace the JSF managed bean facility (@ManagedBeanand friends) by Spring managed bean facility (@Componentand friends), usually with the sole purpose in order to use @Autowiredin a JSF backing bean. But that's it. The JSF MVC framework lifecycle, the JSF components and the view technology remain unchanged. The standard Java EE equivalent of that would be using CDI(and EJB).

另一方面,您将 Spring IoC/DI 与 Spring MVC 混淆也可能是一件好事。的Spring IoC / DI又使用与JSF一起。您可以用@ManagedBeanSpring 管理的 bean 设施(@Component和朋友)替换 JSF 管理的 bean 设施(和朋友),通常只有一个目的是为了@Autowired在 JSF 支持 bean 中使用。但就是这样。JSF MVC 框架生命周期、JSF 组件和视图技术保持不变。标准的 Java EE 等价物将使用CDI(和EJB)。

The same story applies to Spring Security. You can use it together with JSF, you should however not follow Spring Security + Spring MVC targeted documentation/examples in order to configure it, but only Spring Security + JSF ones. Do note that Spring Security constraints on business actions only works when you replace the JSF managed bean facility by Spring managed bean facility. So that would still require a "Integrate Spring in JSF" as described in previous paragraph. The standard Java EE equivalent of this all would be using container managed security(JAAS/JASPIC) via <security-constraint>entries in web.xml.

同样的故事也适用于 Spring Security。您可以将它与 JSF 一起使用,但是您不应该遵循 Spring Security + Spring MVC 目标文档/示例来配置它,而只能遵循 Spring Security + JSF 的文档/示例。请注意,Spring Security 对业务操作的约束仅在您用 Spring 托管 bean 设施替换 JSF 托管 bean 设施时才有效。所以这仍然需要“在 JSF 中集成 Spring”,如前一段所述。标准的Java EE相当于这一切都将被使用容器管理的安全性通过(JAAS / JASPIC)<security-constraint>中的条目web.xml

The same story also applies to Spring WebFlow. You only also need to make sure that you're using most recent version of Spring WebFlow as older versions cause conflictswhen used together with multiple JSF component libraries. Moreover, since JSF 2.2, new Faces Flowsfeature was introduced as part of standard Java EE API, hereby basically making Spring WebFlow superfluous.

同样的故事也适用于 Spring WebFlow。您还只需要确保您使用的是最新版本的 Spring WebFlow,因为旧版本与多个 JSF 组件库一起使用时会导致冲突。此外,从 JSF 2.2 开始,新的Faces Flows特性被引入作为标准 Java EE API 的一部分,因此基本上使 Spring WebFlow 变得多余。

Then there is Spring Boot. This does not have a direct equivalent in Java EE. Spring Boot basically enables you to execute a Java EE application using a plain Java application class with a main()method "in an easy and abstract way". Without Spring Boot it's surely possible (otherwise Spring Boot would never have existed), it's only a bit more work as to configuration as you have to take into account server-specific details based on its documentation. For example: Undertowand Jetty.

然后是 Spring Boot。这在 Java EE 中没有直接的等价物。Spring Boot 基本上使您能够使用普通 Java 应用程序类和main()“以一种简单和抽象的方式”的方法来执行 Java EE 应用程序。如果没有 Spring Boot,它肯定是可能的(否则 Spring Boot 永远不会存在),它只是在配置方面多做一点工作,因为您必须根据其文档考虑特定于服务器的详细信息。例如:UndertowJetty

Coming back to JSF and Spring MVC, if really necessary, you can safely run Spring MVC and JSF next to each other in same web application, but they won't interoperate in server side. They will run completely independently. They will at most touch each other in the client side, if some JavaScript in a JSF-generated HTML page happens to invoke a Spring based REST web service call in the same web application. But that Spring web service would then not need/have to know anything about JSF in order to respond accordingly. The standard Java EE equivalent of that Spring REST webservice is JAX-RS.

回到 JSF 和 Spring MVC,如果真的有必要,您可以安全地在同一个 Web 应用程序中并排运行 Spring MVC 和 JSF,但它们不会在服务器端互操作。它们将完全独立运行。如果 JSF 生成的 HTML 页面中的某些 JavaScript 碰巧在同一 Web 应用程序中调用基于 Spring 的 REST Web 服务,则它们最多会在客户端相互接触。但是,该 Spring Web 服务将不需要/不必了解有关 JSF 的任何信息即可做出相应的响应。与 Spring REST webservice 等效的标准 Java EE 是JAX-RS

The upcoming Java EE 8 will come with a new request based MVC framework, named just "MVC", based on lessons of both JSF and Spring MVC, hereby supplanting Spring MVC and providing a standard alternative to JSF.

即将推出的 Java EE 8 将带有一个新的基于请求的 MVC 框架,名为“ MVC”,基于 JSF 和 Spring MVC 的经验教训,从而取代 Spring MVC 并提供 JSF 的标准替代方案。

See also:

也可以看看:

回答by M. Deinum

Spring MVC and JSF don't really mix. You can use JSF for the view related stuff and have Spring manage and wire the backed (services, daos etc.). But trying to match @Controllers with JSF pages isn't something that really works nicely (next to that both are different stacks request based against component-based).

Spring MVC 和 JSF 并没有真正混合。您可以将 JSF 用于与视图相关的内容,并让 Spring 管理和连接支持的(服务、daos 等)。但是尝试将 @Controllers 与 JSF 页面匹配并不是真正有效的东西(旁边是基于组件的不同堆栈请求)。

To integrate Spring with JSF you will need to add the SpringBeanFacesELResolverto your faces-config.xml. That will lookup beans from Springs application context. For this to work you have to use plain JSF annotations and not the CDI based annotations.

要将 Spring 与 JSF 集成,您需要将 加入SpringBeanFacesELResolver到您的 faces-config.xml 中。这将从 Springs 应用程序上下文中查找 bean。为此,您必须使用普通的 JSF 注释,而不是基于 CDI 的注释。

回答by Manoj Gupta

Spring Webflow can be a help here. Check out this sample project. https://github.com/spring-projects/spring-webflow-samples/tree/master/primefaces-showcase

Spring Webflow 在这里可以提供帮助。查看此示例项目。 https://github.com/spring-projects/spring-webflow-samples/tree/master/primefaces-showcase