使用Spring Boot和JSF Beans

时间:2015-10-29 09:30:12

标签: jsf spring-boot

我正在尝试让Spring Boot与JSF一起工作。 FacesServlet已初始化,网站使用Primefaces正确呈现。但是在我调用JSF或Spring-Bean时,没有显示任何内容。

我知道这个问题多次被问过,但没有一个问题能解决我的问题。经过几个小时的搜索,我没有得到它的工作。我错过了什么吗?

设定:

  • Spring Boot 1.2.7
  • Primefaces 5.2
  • JSF 2.2

我尝试了什么:

我有什么

Application.java

form_valid

JsfBean.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());
}
}

SpringBean.java

@ManagedBean
public class JsfBean {

private String welcomeMessage = "Populated by JSF created bean";

public String getWelcomeMessage() {
    return welcomeMessage;
}
}

的index.xhtml

@Component
public class SpringBean {

private String welcomeMessage = "Populated by spring created bean";

public String getWelcomeMessage() {
    return welcomeMessage;
}
}

输出

<!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>

1 个答案:

答案 0 :(得分:0)

我实际上已经开始工作了。我在链接教程中忽略了,我必须将faces-config.xml放在resources-folder src/main/resources/META-INF/而不是src/main/webapp/META-INF/

这导致了el-resolver从未使用过的问题,这解释了为什么#{springBean.welcomeMessage}没有显示任何内容。

面-config.xml中

<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
          version="2.2">

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application>

<lifecycle>
    <phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener>
</lifecycle>