【发布时间】:2015-11-25 15:10:37
【问题描述】:
我正在尝试在我的 Maven 项目中使用 Spring Boot 配置 JSF/primefaces。我正在关注这个例子https://github.com/stephanrauh/JSF-on-Spring-Boot,效果很好。
问题:应用程序运行时,JSF 视图显示时没有来自后端的数据。
这是我的 *.java 类:
@Configuration
public class WebInitializer extends SpringBootServletInitializer implements ServletContextAware {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Bean
public DispatcherServlet dispatcherServlet() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
return dispatcherServlet;
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.xhtml");
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}
@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
}
@Override
public void setServletContext(ServletContext servletContext) {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
}
}
HelloBean 类:
@ManagedBean(value = "helloBean")
@ViewScoped
public class HelloBean implements Serializable {
private String hello = "Hello from PrimeFaces and Spring Boot!";
public String getHello() {
return hello;
}
@PostConstruct
public void init() {
System.out.println("---------");
System.out.println(hello);
}
}
index.xhtml 文件:
<f:view xmlns="http://www.w3c.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:ng="http://xmlns.jcp.org/jsf/passthrough"
xmlns:prime="http://primefaces.org/ui">
<h:head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>#{menuController.title}</title>
<style>
body {font-size:12px; }
</style>
</h:head>
<h:body>
<h:form>
<prime:panel header="What do you see here?" >
<div style="height:50px">
This is a simple PrimeFaces 5 project running on Spring Boot 1.1.4.
</div>
</prime:panel>
<prime:panel header="JSF 2.2 Bean Access">
#{helloBean.hello}
</prime:panel>
</h:form>
</h:body>
</f:view>
谁能告诉我为什么 helloBean 没有显示在 index.xhtml 上?
【问题讨论】:
-
查看配置,您正在尝试注册
org.springframework.web.servlet.DispatcherServlet这是一个 Spring MVC 工件(接收传入请求并将这些请求的处理委托给一个匹配的处理程序,该处理程序应该存在于所说的位置)。使用 JSF 时不能使用它。我在 GitHub 上的项目中没有看到这样的推荐。 -
正如@Tiny 所说,在纯JSF 应用程序中不需要
Dispatcherservlet。此外,使用 Spring boot 时存在一些问题,您需要在 web.xml 和 java config 中声明两次 JSF servlet 才能设置它。这是我在所有 JSF + Spring boot 项目中都遇到过的问题...... -
终于我找到了解决问题的方法。问题出在 HelloBean.class 中。我使用了不正确的导入 - 导入 javax.annotation.ManagedBean 而不是 import javax.faces.bean.ManagedBean;
标签: spring jsf jsf-2 primefaces spring-boot