【问题标题】:JSF bean is not interpreted on the *.xhtml pageJSF bean 未在 *.xhtml 页面上解释
【发布时间】: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 应用程序中不需要Dispatcher servlet。此外,使用 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


【解决方案1】:

我终于找到了解决问题的方法。问题出在 HelloBean.class 中。我使用了不正确的导入 - 导入 javax.annotation.ManagedBean 而不是 import javax.faces.bean.ManagedBean;

【讨论】:

  • 如果这已经解决了您的问题,请将此标记为选定的答案
【解决方案2】:

我做了一个不同的配置来运行 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

当然是托管 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2013-01-03
    • 2016-02-21
    • 1970-01-01
    相关资源
    最近更新 更多