我正在尝试在我的maven项目中使用Spring启动配置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上?
答案 0 :(得分:1)
最后我找到了解决问题的方法。问题出在HelloBean.class中。我使用了错误的导入 - 导入javax.annotation.ManagedBean而不是import javax.faces.bean.ManagedBean;
答案 1 :(得分:1)
我做了一个不同的配置来运行Spring Boot + JSF。我认为这更简单。看看:
faces-config.xml中
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<lifecycle>
<phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener>
</lifecyle>
MyApp.java
@SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApp.class);
}
@Bean
public ServletRegistrationBean servletRegistrationBean() {
FacesServlet servlet = new FacesServlet();
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet, "*.jsf");
return servletRegistrationBean;
}
}
从Spring Boot 1.2及更高版本开始,您可以在application.properties文件中设置JSF参数,如下所示:
### JSF CONFIGS ###
server.context_parameters.com.sun.faces.forceLoadConfiguration=true
Managed Bean当然。切记不要将javax.faces.bean包用于bean注释
MyBean.java
import javax.annotation.ManagedBean;
import javax.faces.view.ViewScoped;
@ManagedBean
@ViewScoped
public class MyBean {
public String getHello() {
return "hello";
}
}
最后,观点:
<ui:composition
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui"
template="views/templates/basic_template.xhtml"
>
<ui:define name="body_content">
<p:panel header="JSF 2.2 Bean Access">
<h:outputText value="#{myBean.hello}" />
</p:panel>
</ui:define>
</ui:composition>