简体   繁体   English

如何将 Spring 引导 3 与 JSF 一起使用?

[英]How to use Spring boot 3 with JSF?

I'm trying to create a JSF project managed by Spring boot 3.0.我正在尝试创建一个由 Spring boot 3.0 管理的 JSF 项目。 But it doesn't work no matter how I do it.但不管我怎么做都行不通。 I can't find any example on the web.我在 web 上找不到任何示例。

My dependencies:我的依赖项:

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'

My MainApplication class:我的主要应用程序 class:

@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");
    }
}

My config webapp/WEB-INF/faces-config.xml:我的配置 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>

A test XHTML file:测试 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>

Logs:日志:

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]

Links I looked at:我看过的链接:

How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat? 如何在 Maven pom.xml 中为 Tomcat 正确配置 Jakarta EE 库? Spring Boot JSF Integration Spring 引导 JSF 集成

Spring Boot JSF Integration Spring 引导 JSF 集成

The project with Spring boot 2 and Javax that I tried to reproduce:我尝试重现的带有 Spring boot 2 和 Javax 的项目:

https://github.com/xtremebiker/jsf-spring-boot https://github.com/xtremebiker/jsf-spring-boot

I don't understand what I'm doing wrong.我不明白我做错了什么。 I can't find anything on the web. Any help would be welcome.我在 web 上找不到任何内容。欢迎提供任何帮助。

Thank you all谢谢你们

As written on Mojarra , you need more dependencies.正如Mojarra上所写,您需要更多依赖项。

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

Don't create FacesServlet yourself.不要自己创建 FacesServlet。 ServletContainerInitializer does that. ServletContainerInitializer就是这样做的。

But if you use embedded server, you should run two of them.但是如果你使用嵌入式服务器,你应该运行其中两个。 Because Spring Boot doesn't run the initializers.因为Spring 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);
    }
}

Try:尝试:

implementation 'org.glassfish:jakarta.faces:3.0.4'

I did not figure out the root cause (conflict?) yet but, I have noticed that any versions above 3.0.4 cause the exception you are getting.我还没有找出根本原因(冲突?)但是,我注意到 3.0.4 以上的任何版本都会导致您遇到异常。 I got the same.我也一样。 This worked for me:这对我有用:

implementation  'org.springframework.boot:spring-boot-starter-web:3.0.1'
implementation  'org.glassfish:jakarta.faces:3.0.4' 
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper:11.0.0-M6'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM