gpt4 book ai didi

spring - 如何将 Spring Boot 与带有嵌入式 Tomcat 的 JSF 2.2 集成

转载 作者:行者123 更新时间:2023-12-02 05:25:00 25 4
gpt4 key购买 nike

我正在尝试将 Spring Boot 与 JSF 集成到嵌入式 Tomcat 中,但没有成功,我尝试了两种方法,一种使用 web.xml,另一种使用 Java 配置。在这两种情况下,xhtml 页面都不会被渲染,而是会渲染空白页面。以下是配置。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.kp.swasthik</groupId>
<artifactId>kp-sb-jsf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>kp-sb-jsf</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.M5</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<primefaces.version>5.2</primefaces.version>
<!-- <jsf.version>2.2.8</jsf.version> -->
<jsf.version>2.2.12</jsf.version>
<jsf.extension.version>3.2.0</jsf.extension.version>
<jsf.themes.version>1.0.10</jsf.themes.version>
<spring.webflow.version>2.4.2.RELEASE</spring.webflow.version>
<omnifaces.version>2.1</omnifaces.version>
<wield.version>2.2.15.Final</wield.version>
</properties>

<dependencies>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>

<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>

<!--
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>${omnifaces.version}</version>
</dependency>
-->

<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>${wield.version}</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>${primefaces.version}</version>
</dependency>

<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>${jsf.extension.version}</version>
</dependency>

<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>${jsf.themes.version}</version>
</dependency>

<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>resources-optimizer-maven-plugin</artifactId>
<version>2.0.0</version>
</dependency>


<!-- <dependency> <groupId>org.apache.myfaces.core</groupId> <artifactId>myfaces-impl</artifactId>
<version>${jsf.version}</version> </dependency> -->

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
</dependency>

<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-faces</artifactId>
<version>${spring.webflow.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>${spring.webflow.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-binding</artifactId>
<version>${spring.webflow.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</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-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>

faces.config位于src/main/resources/META-INF目录

    <?xml version="1.0"?>
<faces-config version="2.2" 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 http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" metadata-complete="false" >

<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<!-- <resource-handler>org.omnifaces.resourcehandler.CombinedResourceHandler</resource-handler> -->
<resource-handler>org.omnifaces.resourcehandler.UnmappedResourceHandler</resource-handler>


</application>

<lifecycle>
<phase-listener>org.springframework.web.jsf.DelegatingPhaseListenerMulticaster</phase-listener>
</lifecycle>

</faces-config>

src/main/webapp/META-INF 中使用的 context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="BeanManager" auth="Container"
type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>

Index.html 移至 src/main/resources/META-INF/resourcesindex.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">

<h:head>
</h:head>
<h:body>
<p:panel>
#{kpDisplay.hello}
</p:panel>
</h:body>
</html>

@SpringBootApplication
@ComponentScan(basePackages={"com.kp.swasthik.bjsf"})
public class KpSbBjsfApplication extedns SpringBootServletInitializer{


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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(KpSbBjsfApplication.class);
}

@Bean
public ServletRegistrationBean facesServletRegistration() {

ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new FacesServlet(), "*.xhtml");
servletRegistrationBean.addUrlMappings("/faces/*");
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}

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

@Bean
@ConditionalOnMissingBean
public RequestContextListener getRequestContextListener() {
return new RequestContextListener();
}
}

以及 servlet 上下文初始化类。

public class ServletCustmContextInitializer implements ServletContextInitializer {

@Override
public void onStartup(ServletContext jsfServlet) throws ServletException {
jsfServlet.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());
jsfServlet.setInitParameter("javax.faces.PARTIAL_STATE_SAVING_METHOD", "true");
jsfServlet.setInitParameter("javax.faces.PROJECT_STAGE", "Development");
jsfServlet.setInitParameter("facelets.DEVELOPMENT", "true");
jsfServlet.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "1");
jsfServlet.setInitParameter("primefaces.CLIENT_SIDE_VALIDATION", "true");
jsfServlet.setInitParameter("javax.faces.DEFAULT_SUFFIX", ".xhtml");
jsfServlet.setInitParameter("primefaces.THEME", "bootstrap");

}

}

我设法启动 jsf-spring 启动配置,但现在 JSF 注释不起作用。如果我手动将托管注释添加到 faces-config.xml 中,托管注释就会起作用。我什至添加了metadata-complete=“false”,因此 jsf 会扫描注释,遗憾的是它不起作用

最佳答案

碰巧我写了一篇关于如何做到这一点的教程。您可以find it here以及 GitHub 上功能齐全的 Maven 演示项目。

这里的关键问题是 Spring Boot 固执己见,更喜欢 java-config,因此配置是从 java 代码注入(inject)的,而 Tomcat 需要 web.xml 只是作为安慰毯子存在;它没有被使用。

不要使用 Omnifaces,它需要 CDI,在我的尝试中,让它在嵌入式 Tomcat 中运行似乎是不可能的。您还可以使用其他效果很好的工具,在我的教程中,我已经配置了 Rewrite,因为如何做到这一点并不明显。

关于spring - 如何将 Spring Boot 与带有嵌入式 Tomcat 的 JSF 2.2 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32422180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com