【问题标题】:Using Spring Boot with JSF Beans将 Spring Boot 与 JSF Bean 一起使用
【发布时间】: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

我尝试了什么:

我有什么

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


【解决方案1】:

我真的让它工作了。我在链接教程中忽略了,我必须将 faces-config.xml 放在资源文件夹 src/main/resources/META-INF/ 而不是 src/main/webapp/META-INF/ 中。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2016-11-12
    • 1970-01-01
    • 2012-12-30
    • 2021-01-02
    • 2020-03-09
    • 2021-08-07
    • 1970-01-01
    相关资源
    最近更新 更多