如何将Spring Boot与JSF 2.2与嵌入式tomcat集成

时间:2015-09-06 09:48:30

标签: spring spring-mvc jsf spring-boot jsf-2.2

我试图在嵌入式tomcat中将spring boot与JSF集成,但没有运气,我尝试了两种方法,一种是使用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>
src / main / resources / META-INF目录中的

faces.config

    <?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 / resources 的的index.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上下文init类。

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 =&#34; false&#34;所以jsf可悲地扫描注释它不起作用

1 个答案:

答案 0 :(得分:1)

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

这里的关键问题是Spring Boot是自以为是并且更喜欢java-config,因此配置是从java代码注入的,而Tomcat要求web.xml只是作为一个舒适的毯子存在;它没用过。

不要使用Omnifaces,它需要CDI并且在我的尝试中似乎不可能在嵌入式Tomcat中表现。还有其他可以使用的工具,它们运行良好,在我的教程中,我已经配置了Rewrite,如何做到这一点并不明显。