如何在一个可运行的jar中部署spring boot jsf应用程序

时间:2016-09-13 14:39:26

标签: jsf spring-boot

我有一个从Netbeans IDE中启动时正确运行的Spring Boot JSF应用程序。当我将maven项目打包在一个可运行的jar中并询问一个URL时,我遇到了以下错误[java.lang.IllegalStateException:找不到工厂javax.faces.context.FacesContextFactory的备份]。

那么在一个可运行的jar中部署spring boot jsf应用程序的正确方法是什么?我一直在网上扫描4个小时寻找解决方案。徒然。我找到了在Netbeans内部工作的解决方案,但当我从他们那里生成一个可运行的jar时,他们就崩溃了。

这是我的环境:

[web.xml中]

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
  <!-- la servlet FacesServlet -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
</web-app>

[faces-config.xml中]

    <?xml version='1.0' encoding='UTF-8'?>
<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">
  <application>
    <!-- pour que Spring puisse gérer les expression #{expression} dans les pages JSF -->
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    <!-- le fichier des messages -->
    <resource-bundle>
      <base-name>
        messages
      </base-name>
      <var>msg</var>
    </resource-bundle>
    <message-bundle>messages</message-bundle>
  </application>
</faces-config>

[引导课程]

    package web.boot;

import org.springframework.boot.SpringApplication;
import web.config.JsfConfig;

public class Boot {

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

}

[Config class]

    package web.config;

import config.MetierDaoJpaConfig;
import java.util.concurrent.TimeUnit;
import javax.faces.webapp.FacesServlet;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({MetierDaoJpaConfig.class})
@ComponentScan({"web.beans"})
// ******************************************************************************
// à utiliser conjointement avec [WEB-INF/faces-config.xml et WEB-INF/web.xml]
// ******************************************************************************
public class JsfConfig {

  @Bean
  public ServletRegistrationBean servletRegistrationBean() {
    FacesServlet servlet = new FacesServlet();
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(servlet,"*.xhtml");
    return servletRegistrationBean;
  }

  @Bean
  public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.setPort(8080);
    factory.setSessionTimeout(10, TimeUnit.MINUTES);
    return factory;
  }

}

[pom.xml中]

<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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>istia.st</groupId>
  <artifactId>mv-pam-primefaces-multipages-springboot-eclipselink-tomcat-02</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mv-pam-primefaces-multipages-springboot-eclipselink-tomcat-02</name>
  <url>http://maven.apache.org</url>


  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
  </parent>

  <dependencies>
    <!-- JSP API -->
    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!-- Spring web -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- JSF -->
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-api</artifactId>
      <version>2.2.13</version>
    </dependency>
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-impl</artifactId>
      <version>2.2.13</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.primefaces/primefaces -->
    <dependency>
      <groupId>org.primefaces</groupId>
      <artifactId>primefaces</artifactId>
      <version>6.0</version>
    </dependency>

    <!-- JSR-330 --> 
    <dependency>
      <groupId>javax.inject</groupId>
      <artifactId>javax.inject</artifactId>
      <version>1</version>
    </dependency>

    <!-- couches basses -->
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>mv-pam-springboot-eclipselink</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <mainClass>web.boot.Boot</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
</project>

0 个答案:

没有答案