Spring引用嵌入tomcat - /pages/index.xhtml在ExternalContext中找不到作为资源

时间:2016-08-18 08:16:54

标签: jsf tomcat primefaces spring-boot

我正在尝试开发一个控制台,使得JSF2端的Primefaces在Spring引导的嵌入式tomcat上运行。这是我的项目配置:

的pom.xml

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

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <main.basedir>${basedir}/../..</main.basedir>
    <java.version>1.8</java.version>
    <tomcat.version>7.0.59</tomcat.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-juli</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>com.sun.el</groupId>
        <artifactId>el-ri</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.14</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>6.0</version>
    </dependency>
    <dependency>
        <groupId>org.python</groupId>
        <artifactId>jython-standalone</artifactId>
        <version>2.7.1b3</version>
    </dependency>
</dependencies>

<build>
    <finalName>ConfigurationConsole</finalName>
    <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

faces-config.xml中

<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>

的web.xml

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/fases/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/javax.faces.resource/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>
<context-param>
    <description>
        If comments are enabled, the copyright header will prevent the login form
        from working on Internet Explorer. Alternatively, you can remove the copyright
        header.
    </description>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
    <param-value>com.igt.icc.configurationConsole.FaceletsResourceResolver</param-value>
</context-param>
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>

Spring boot主类:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class ApplicationStarter extends SpringBootServletInitializer implements ServletContextAware
 {

/**
 *
 * @param args
 */
public static void main(String[] args) {
    SpringApplication.run(ApplicationStarter.class, args);
    try{
        //JSONPropertiesManager.getInstance().loadProperties();
        JSONPropertiesManager.getInstance().loadJSONProperties();
    }catch(Exception ex){
        ex.printStackTrace();
    }
}

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


@Bean
public FacesServlet facesServlet() {
    return new FacesServlet();
}


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


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

@Override
public void setServletContext(ServletContext servletContext) {
    servletContext.setInitParameter("com.sun.faces.forceLoadConfiguration", Boolean.TRUE.toString());       
}

}

facesResourceResolver类,调用web.xml:

public class FaceletsResourceResolver extends ResourceResolver {

private ResourceResolver parent;
private String basePath;

public FaceletsResourceResolver(ResourceResolver parent) {
    this.parent = parent;
    this.basePath = "/META-INF/"; // TODO: Make configureable?
}

@Override
public URL resolveUrl(String path) {
    URL url = parent.resolveUrl(path); // Resolves from WAR.

    if (url == null) {
        url = getClass().getResource(basePath + path); // Resolves from JAR.
    }

    return url;
}

这是我的项目结构:

projectFolder


-src
       -main
            -java
            -resources
                - here i have copyed into META-INF the *.class and /pages/*.xhtml
            -webapp

在Netbeans和eclipse中,应用程序运行正常。但是,当我构建它并尝试启动jar时,应用程序正确启动但是当我尝试打开页面时,我收到的404未找到,我做错了什么?

1 个答案:

答案 0 :(得分:1)

<强>解决方案

请按照以下文件夹结构进行操作,如下图所示。

FolderStructure