【发布时间】:2014-08-31 12:54:50
【问题描述】:
我曾尝试使用来自 Spring Boot and JSF/Primefaces/Richfaces 的信息,但对我来说它不起作用。
我使用 Java 8、maven、Spring-boot 和带有 PrimeFaces 的 JSF。
我想拥有可执行的 jar 并通过 main 方法或从命令行 java -jar myApp.jar 运行我的应用程序。
问题 - JSF 注释(@ManagedBean、@ManagedProperty)被忽略。
Pom 文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.7</version>
</dependency>
...
</dependencies>
我也尝试添加/删除 javax.el-api/javax.el/jstl - 结果相同。对于 bean 初始化,我在 faces-config.xml 中添加了部分
当我将 spring-boot-starter-web 更改为 spring-boot-starter 并拥有 spring-web (根据解决方案来自提到赫里克的帖子)我得到了
java.io.FileNotFoundException:类路径资源 [org/springframework/web/servlet/config/annotation/WebMvcConfigurerAdapter.class] 无法打开,因为它不存在
我的配置类:
@Configuration
@EnableAutoConfiguration//(exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class})
@ComponentScan("hello")
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
}
@Bean
public FacesServlet facesServlet() {
return new FacesServlet();
}
@Bean
public ServletRegistrationBean facesServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(facesServlet(), "*.xhtml");
registration.setName("facesServlet");
return registration;
}
@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
}
}
使用 (exclude = {WebMvcAutoConfiguration.class, DispatcherServletAutoConfiguration.class}) web.xml 配置不起作用。
在提到的帖子中是:
@Bean
public ListenerRegistationBean jsfConfigureListener() {
return new ListenerRegistrationBean(new ConfigureListener());
}
ListenerRegistationBean 在我的 spring-boot 中不存在,我使用 ServletListenerRegistrationBean 代替。
我的 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_1.xsd"
version="3.1">
<display-name>Test</display-name>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<error-page>
<location>/error.xhtml</location>
</error-page>
</web-app>
还有 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 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>
<managed-bean>
<managed-bean-name>managedBeann</managed-bean-name>
<managed-bean-class>hello.ManagedBeann</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
因为使用了非工作注释。 顺便说一句,PrimeFaces 正在工作。
我的目的是强制 JSF 注释工作,因为在没有它们的实际项目中这是不可能的。
【问题讨论】:
-
你的 web.xml 没有做任何事情,所以从删除它开始。您不应该将
FacesServlet作为 bean,因为您已经拥有 facesServletRegistration。 servlet 的名称应为FacesServlet,因为它由ConfigureListener查找 -
谢谢!我希望,我理解你正确。关于 web.xml:我想在里面有 javax.faces.application.ViewExpiredException,文件上传过滤器,primefaces 主题配置。我不知道这个设置的另一个地方,如果 web.xml 将被删除。
-
关于 FacesServlet。我这样做: // '@Bean' public FacesServlet facesServlet() { return new FacesServlet(); } @Bean public ServletRegistrationBean facesServletRegistration() { ServletRegistrationBean 注册 = new ServletRegistrationBean(facesServlet(), "*.xhtml"); registration.setName("FacesServlet");退货登记; } 但没有任何改变。
-
Spring boot 对 web.xml 没有任何作用,这可能是 JSF 的东西解析了 web.xml 本身。对于 Spring Boot,它根本不存在。
-
你有没有办法解决它?我正在尝试创建相同的内容并遇到@ManagedBean 的问题。它没有初始化 bean
标签: spring jsf spring-boot