免责声明
我是根据我认为您想要实现的目标来回答这个问题的,尽管我的答案与问题标题不符。
您说“我的目的是强制 JSF 注释工作,因为在实际项目中没有它们这是不可能的。”我猜你的意思是“不可能”,因为将托管 bean 放入 faces-config.xml 中很麻烦。因此,为此我将不使用 faces-config.xml 来管理 bean。
我将向您展示一种使用 Spring 注释的替代方案,它非常不麻烦,而且我觉得可以实现您最初的目标。
Answer
例子 -https://github.com/Zergleb/Spring-Boot-JSF-Example https://github.com/Zergleb/Spring-Boot-JSF-Example
前几天我查看了你的问题,并决定尝试让这项工作成功,并将我的结果放在 github 上(上面的链接)。这个示例应该允许您使用 Spring 注释而不是 JSF 注释来编写 JSF 应用程序,例如您会说
@Component
@Scope("view")
//The example above contains an implementation of the View Scope in Spring.
代替
@ManagedBean
@ViewScope
然后您就可以使用 Spring 进行所有依赖项注入。
我使用 gradle 而不是 maven,所以这意味着您的依赖项位于 build.gradle 而不是 pom.xml 中,我必须添加这些依赖项才能使一切正常工作。我想这些应该很容易翻译成 pom.xml 。
compile group: 'javax.el', name: 'el-api', version: '1.0'
compile group: 'com.sun.el', name: 'el-ri', version: '1.0'
compile group: "javax.servlet.jsp" name: "jsp-api" version: "2.1"
我的 web.xml 现在只有一个 servlet,我删除了 servlet-mapping 和 web.xml 的所有其他属性
(我仍在研究如何完全删除此 web.xml,请检查示例以了解我是否发现它的任何更新)
<web-app ... same as before>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
</web-app>
faces-config.xml 现在没有托管 bean
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" version="2.2">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
我现在没有这个,但我们可能想考虑在 web.xml 中留一个空,我没有对此进行大量研究,但 github 上的 spring 项目示例之一包含此代码
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/src/main/webapp/WEB-INF/web.xml https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/src/main/webapp/WEB-INF/web.xml
<!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
我希望这能回答你的问题。如果我遗漏了一些内容,请尝试参考示例代码。
Example
https://github.com/Zergleb/Spring-Boot-JSF-Example https://github.com/Zergleb/Spring-Boot-JSF-Example
运行一个 spring boot 应用程序,该应用程序应该在一个共享公共上下文的应用程序中运行 Spring MVC 和 JSF。(我将其包含在答案中,因为您在问题中引用了此链接Spring Boot 和 JSF/Primefaces/Richfaces https://stackoverflow.com/questions/22544214/spring-boot-and-jsf-primefaces-richfaces这表明混合 Spring MVC 和 JSF 是不可能的,但我已经在我的示例代码中工作了。