在用Spring Boot设置FacesServlet 之后
,大惊小怪地看到了这个问题 之后 ,我通过添加几个配置文件以及来自spring-web的ServletRegistrationBean使其与JSF /
primefaces一起使用。web.xml和faces-config.xml文件对Spring
Boot的最小化设置(无大量xml文件)的想法有点伤害,但这是我可以用JSF找到的最小化设置,如果有人知道更好/更干净的话方式,请让我知道。
这是我的gradle.build文件中的依赖项:
compile group: "org.springframework.boot", name: "spring-boot-starter"compile group: "org.springframework", name: "spring-web", version: "4.0.2.RELEASE"compile group: "org.apache.tomcat.embed", name: "tomcat-embed-core", version: tomcatVersioncompile group: "org.apache.tomcat.embed", name: "tomcat-embed-logging-juli", version: tomcatVersioncompile group: "org.apache.tomcat.embed", name: "tomcat-embed-jasper", version: tomcatVersioncompile group: "org.primefaces", name: "primefaces", version: "4.0"compile group: "com.sun.faces", name: "jsf-api", version: "2.1.21"compile group: "com.sun.faces", name: "jsf-impl", version: "2.1.21"
哪里
tomcatVersion是“ 7.0.34”。这里的主要更改是:
- 删除
spring-boot-starter-web
,其中还包括M. Deinum指出的Spring MVC - 包括
spring-boot-starter
(更轻松的启动)和spring-web
- 明确包含tomcat嵌入的依赖项,因为
spring-boot-starter-web
这里不再存在。
这是我的主班的新内容:
package com.hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.context.embedded.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import javax.faces.webapp.FacesServlet;@Configuration@ComponentScan@EnableAutoConfigurationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ServletRegistrationBean servletRegistrationBean() { FacesServlet servlet = new FacesServlet(); return new ServletRegistrationBean(servlet, "*.xhtml"); }}@EnableAutoConfiguration如果您希望Spring
Boot自动设置Tomcat,并且ServletRegistrationBean将xhtml请求映射到FacesServlet。
我的webapp目录中的WEB-INF / faces-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?><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/web-facesconfig_2_2.xsd" version="2.2"> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> </application></faces-config>
并且web.xml文件也位于webapp / WEB-INF中:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version="2.5"> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping></web-app>
servlet定义似乎是多余的,但是由于某些原因,如果我从web.xml文件或servlet注册bean中删除它们,则xhtml页面的呈现将无法正常工作或根本无法工作。编辑:原来,在web.xml文件中不需要servlet映射,这只是一个IDE问题,在我的情况下,Intellij
Idea不知道在servlet注册bean中定义了servlet映射,因此它抱怨关于缺少期望的servlet的映射的信息,但是该应用程序仍然可以正常运行。
感谢所有贡献者。



