带有 spring 引导 jspFactory 的 PrimeFaces 10 丢失

PrimeFaces 10 with spring boot jspFactory is missing

我想设置一个 spring boot 2.5.5 war 应用程序和 primefaces 10 以拥有 UI 框架。

我已经开始使用joinfaces,但我无法满足所有需求。然后我删除了 joinfaces 依赖项,并想逐步设置。不幸的是,似乎仍然缺少某些东西,我自己无法真正弄清楚。

这是 pom.xml 依赖项

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-ldap</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>10.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.20</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.20</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

这是主要的class

    @SpringBootApplication
    public class CcmtApplication {

      protected CcmtApplication() { }

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

      @Bean
  ServletRegistrationBean jsfServletRegistration(ServletContext servletContext) {
      //spring boot only works if this is set
      servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());

      //registration
      ServletRegistrationBean srb = new ServletRegistrationBean();
      srb.setServlet(new FacesServlet());
      srb.setUrlMappings(Arrays.asList("*.xhtml"));
      srb.setLoadOnStartup(1);
      return srb;
  }
  
  @Bean
  public ConfigureListener mojarraConfigureListener() {
    return new ConfigureListener();
  }
}

这是错误堆栈

2021-10-11 18:19:22.202  INFO 14726 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8443 (https)
2021-10-11 18:19:22.222  INFO 14726 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-10-11 18:19:22.223  INFO 14726 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.53]
2021-10-11 18:19:22.316  INFO 14726 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-10-11 18:19:22.316  INFO 14726 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2779 ms
2021-10-11 18:19:22.611  INFO 14726 --- [  restartedMain] j.e.resource.webcontainer.jsf.config     : Initializing Mojarra 2.2.20 ( 20190731-0757 59754ac80c05d61848a08939ddd11a324f2345ac) for context ''
2021-10-11 18:19:22.981  INFO 14726 --- [  restartedMain] j.e.r.webcontainer.jsf.application       : JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
2021-10-11 18:19:24.252 ERROR 14726 --- [  restartedMain] j.e.resource.webcontainer.jsf.config     : Critical error during deployment: 

java.lang.NoClassDefFoundError: javax/servlet/jsp/JspFactory
    at com.sun.faces.config.ConfigureListener.isJspTwoOne(ConfigureListener.java:671) ~[jsf-impl-2.2.20.jar:2.2.20]
    at com.sun.faces.config.ConfigureListener.registerELResolverAndListenerWithJsp(ConfigureListener.java:691) ~[jsf-impl-2.2.20.jar:2.2.20]
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:243) ~[jsf-impl-2.2.20.jar:2.2.20]
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4766) [tomcat-embed-core-9.0.53.jar:9.0.53]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230) [tomcat-embed-core-9.0.53.jar:9.0.53]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) [tomcat-embed-core-9.0.53.jar:9.0.53]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1396) [tomcat-embed-core-9.0.53.jar:9.0.53]

我错过了什么?

实际上,您将不得不做很多 Joinfaces 人已经为您完成的工作。

试试这两个模板之一:

  1. Joinfaces Gradle War Example
  2. Joinfaces Maven War Example

如果以上不符合你的需求,那么试试这样设置:

@SpringBootApplication
public class Application {

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

/*
 * Below sets up the Faces Servlet for Spring Boot
 */
@Bean
public FacesServlet facesServlet() {
    return new FacesServlet();
}

@Bean
public ServletRegistrationBean<Servlet> facesServletRegistration() {
    ServletRegistrationBean<Servlet> registration = new ServletRegistrationBean<>(facesServlet(), "*.xhtml");
    registration.setName("FacesServlet");
    return registration;
}

@Bean
public ServletListenerRegistrationBean<ConfigureListener> jsfConfigureListener() {
    return new ServletListenerRegistrationBean<>(new ConfigureListener());
}}

感谢您的提示,但最终我在不使用 joinfaces 的情况下解决了我的问题。

实际上我更改了 pom,删除了 tomcat

的 sprint-boot starter
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

添加添加

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
</dependency>