JSF bean 未在 *.xhtml 页面上解释
JSF bean is not interpreted on the *.xhtml page
我正在尝试在我的 Maven 项目中使用 Spring 引导配置 JSF/primefaces。我正在按照这个示例 https://github.com/stephanrauh/JSF-on-Spring-Boot 进行操作,效果很好。
问题:当应用程序 运行 时,显示的 JSF 视图没有来自后端的数据。
这是我的 *.java classes:
@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 class:
@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 上?
我终于在 HelloBean.class 中找到了 issue.The 问题的解决方案。我使用了不正确的导入 - import javax.annotation.ManagedBean 而不是 import javax.faces.bean.ManagedBean;
我对 运行 Spring Boot + JSF 进行了不同的配置。我认为这更简单。看看:
面孔-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
当然是托管 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>
我正在尝试在我的 Maven 项目中使用 Spring 引导配置 JSF/primefaces。我正在按照这个示例 https://github.com/stephanrauh/JSF-on-Spring-Boot 进行操作,效果很好。
问题:当应用程序 运行 时,显示的 JSF 视图没有来自后端的数据。
这是我的 *.java classes:
@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 class:
@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 上?
我终于在 HelloBean.class 中找到了 issue.The 问题的解决方案。我使用了不正确的导入 - import javax.annotation.ManagedBean 而不是 import javax.faces.bean.ManagedBean;
我对 运行 Spring Boot + JSF 进行了不同的配置。我认为这更简单。看看:
面孔-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
当然是托管 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>