Spring Boot+JoinFaces部署外部Tomcat出现重复admin_theme_fragment错误如何解决
适配Spring Boot + JoinFaces无web.xml环境的解决方案
问题根因
报错是因为外部Tomcat启动时会扫描classpath下所有jar包的web-fragment.xml,检测到两个名为admin_theme_fragment的片段声明触发冲突,IDE运行时使用的内嵌Tomcat默认不会做严格的唯一性校验所以运行正常。
解决方案按优先级选择
方案1:排除重复的Primefaces Admin主题依赖(最优)
由于你配置了joinfaces.primefaces.theme=admin,JoinFaces会自动引入Primefaces官方的admin主题依赖,若你的pom中手动添加了admin主题的依赖就会导致重复:
- 打开pom.xml,找到手动声明的
primefaces-theme-admin(或同类主题包)依赖直接删除即可 - 如果你需要保留自定义的admin主题,给joinfaces的primefaces starter排除自动引入的主题依赖:
<dependency> <groupId>org.joinfaces</groupId> <artifactId>primefaces-spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.primefaces.themes</groupId> <artifactId>admin</artifactId> </exclusion> </exclusions> </dependency>
方案2:配置跳过Tomcat Web Fragment名称校验
如果不想修改依赖,直接在application.properties中添加如下配置,适配外部Tomcat的严格校验规则:
# 禁用Tomcat对web fragment名称的唯一性强制校验 server.servlet.context-parameters.org.apache.catalina.STRICT_SERVLET_COMPLIANCE=false # 可选:指定跳过重复的主题fragment扫描 server.servlet.context-parameters.javax.faces.WEBAPP_RESOURCES_EXCLUDES=**/admin_theme_fragment/**
方案3:配置ServletContextInitializer禁用重复扫描(兜底)
如果上述配置不生效,新增一个配置类手动设置Servlet上下文参数:
import org.springframework.boot.web.servlet.ServletContextInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TomcatConfig { @Bean public ServletContextInitializer disableDuplicateFragmentCheck() { return servletContext -> { servletContext.setInitParameter("org.apache.catalina.STRICT_SERVLET_COMPLIANCE", "false"); servletContext.setInitParameter("org.apache.tomcat.util.scan.StandardJarScanFilter.jarsToSkip", "*admin-theme*.jar"); }; } }
验证方式
修改完成后重新打包为war包,部署到外部Tomcat即可正常启动,全程无需修改web.xml配置。
内容的提问来源于stack exchange,提问作者Nemuga Dev
相关产品推荐
相关产品推荐

