spring 如何在JSF中使用Sping Boot 3?

pkwftd7m  于 2023-05-21  发布在  Spring
关注(0)|答案(1)|浏览(106)

我正在尝试创建一个由Sping Boot 3.0管理的JSF项目。但不管我怎么做都没用。我在网上找不到任何例子。
我的依赖项:

implementation 'org.springframework.boot:spring-boot-starter-web:3.0.2'
implementation 'org.apache.tomcat.embed:tomcat-embed-jasper:10.1.5'
implementation 'org.glassfish:jakarta.faces:4.0.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test:3.0.2'

我的MainApplication类:

@SpringBootApplication
public class ManagementClientApplication implements ServletContextAware {

    public static void main(String[] args) {
        SpringApplication.run(ManagementClientApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean<FacesServlet> servletRegistrationBean() {
        ServletRegistrationBean<FacesServlet> servletRegistrationBean = new ServletRegistrationBean<>(new FacesServlet(), "*.xhtml");
        servletRegistrationBean.setLoadOnStartup(1);
        return servletRegistrationBean;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
        servletContext.setInitParameter("jakarta.faces.FACELETS_SKIP_COMMENTS", "true");
    }
}

我的配置webapp/WEB-INF/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>

</faces-config>

测试XHTML文件:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>

<h:body>
    <h:form>
        <h:outputText id="greeting" value="Hello world !" />
    </h:form>
</h:body>
</html>

日志:

2023-02-02T17:21:10.844+01:00  INFO 6312 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-02-02T17:21:10.845+01:00  INFO 6312 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 738 ms
2023-02-02T17:21:11.133+01:00 ERROR 6312 --- [           main] jakarta.faces                            : Unable to obtain InjectionProvider from init time FacesContext. Does this container implement the Mojarra Injection SPI?
2023-02-02T17:21:11.149+01:00 ERROR 6312 --- [           main] jakarta.faces                            : L’application n’a pas été initialisée correctement au démarrage. Impossible de localiser la Fabrique : jakarta.faces.context.FacesContextFactory. Attempting to find backup.
2023-02-02T17:21:11.150+01:00 ERROR 6312 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Servlet.init() for servlet [facesServlet] threw exception

java.lang.IllegalStateException: Could not find backup for factory jakarta.faces.context.FacesContextFactory. 
    at jakarta.faces.FactoryFinderInstance.notNullFactory(FactoryFinderInstance.java:497) ~[jakarta.faces-4.0.1.jar:4.0.1]
    at jakarta.faces.FactoryFinderInstance.getFactory(FactoryFinderInstance.java:190) ~[jakarta.faces-4.0.1.jar:4.0.1]
    at jakarta.faces.FactoryFinder.getFactory(FactoryFinder.java:263) ~[jakarta.faces-4.0.1.jar:4.0.1]
    at jakarta.faces.webapp.FacesServlet.acquireFacesContextFactory(FacesServlet.java:493) ~[jakarta.faces-4.0.1.jar:4.0.1]

我看过的链接:
如何在Tomcat的Maven pom.xml中正确配置Jakarta EE库?Spring Boot JSF Integration
Spring Boot JSF Integration
我尝试复制的Sping Boot 2和Javax项目:
https://github.com/xtremebiker/jsf-spring-boot
我不明白我做错了什么。我在网上找不到任何东西。任何帮助都是受欢迎的。
谢谢大家

vd2z7a6w

vd2z7a6w1#

正如在Mojarra上编写的那样,您需要更多的依赖项。

implementation group: 'org.jboss.weld.servlet', name: 'weld-servlet-shaded', version: '5.1.0.Final'

不要自己创建FacesServlet。ServletContainerInitializer就是这么做的。
但是如果你使用嵌入式服务器,你应该运行其中的两个。因为Sping Boot 不运行初始化器。

@Bean
public ServletContextInitializer facesInitializer() {
    return new JsfInitializer();
}
import org.jboss.weld.environment.servlet.EnhancedListener;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import jakarta.servlet.ServletContainerInitializer;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import com.sun.faces.config.FacesInitializer;

public class JsfInitializer implements ServletContextInitializer {

    @Override
    public void onStartup(ServletContext context) throws ServletException {
        EnhancedListener cdiInitializer = new EnhancedListener();
        cdiInitializer.onStartup(null, context);

        ServletContainerInitializer facesInitializer = new FacesInitializer();
        facesInitializer.onStartup(null, context);
    }
}

相关问题