【发布时间】:2016-01-29 08:49:26
【问题描述】:
我正在尝试让 Spring Boot 与 JSF 一起工作。 FacesServlet 已初始化,并且网站使用 Primefaces 正确呈现。但是在我调用 JSF- 或 Spring-Bean 时,什么都没有显示。
我知道这个问题被问过多次,但都没有解决我的问题。经过数小时的搜索,我无法正常工作。我错过了什么吗?
设置:
- Spring Boot 1.2.7
- Primefaces 5.2
- JSF 2.2
我尝试了什么:
- Integrate Jsf Spring Boot Tutorial
- Spring Boot and JSF/Primefaces/Richfaces
- Spring Boot with JSF; Could not find backup for factory javax.faces.context.FacesContextFactory
我有什么
Application.java
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public FacesServlet facesServlet() {
return new FacesServlet();
}
@Bean
public ServletRegistrationBean facesServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(facesServlet(), new String[] { "*.xhtml" });
registration.setName("FacesServlet");
registration.setLoadOnStartup(1);
return registration;
}
@Configuration
static class ConfigureJSFContextParameters implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", "true");
servletContext.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
servletContext.setInitParameter("encoding", "UTF-8");
}
}
@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
return new ServletListenerRegistrationBean<ConfigureListener>(new ConfigureListener());
}
}
JsfBean.java
@ManagedBean
public class JsfBean {
private String welcomeMessage = "Populated by JSF created bean";
public String getWelcomeMessage() {
return welcomeMessage;
}
}
SpringBean.java
@Component
public class SpringBean {
private String welcomeMessage = "Populated by spring created bean";
public String getWelcomeMessage() {
return welcomeMessage;
}
}
index.xhtml
<!DOCTYPE html>
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui" encoding="UTF-8">
<html>
<h:head>
</h:head>
<h:body>
<h1>Rechnung</h1>
<h3>#{springBean.welcomeMessage}</h3>
<h3>#{jsfBean.welcomeMessage}</h3>
</h:body>
</html>
</f:view>
输出
<h1>Rechnung</h1>
<h3></h3>
<h3></h3>
【问题讨论】:
-
将 JSF 之类的技术与任何 Spring 项目混合使用大多会导致混乱。我认为最好坚持使用完整的
JavaEE堆栈或完全切换到Spring。 -
@ThomasSchmidt 我以前听说过这个问题,但我会将这种方式视为最后一步。我仍然有一点希望像我上面描述的那样去做;-)
标签: jsf spring-boot