【问题标题】:Spring Boot and JSF/Primefaces/RichfacesSpring Boot 和 JSF/Primefaces/Richfaces
【发布时间】:2014-04-27 23:04:59
【问题描述】:

我接触 Spring 才几个月,最近在浏览指南部分时发现了 Spring Boot。这些指南非常容易完成,并且很好地初步掌握了项目的基本(和令人敬畏的)想法,即能够以最少的配置构建和部署企业级应用程序,同时支持各种 Spring/JEE良好做法。我对使用 Spring Boot 进行测试项目非常感兴趣,因为有了它,它们的设置和运行变得更加容易和快速,并且仍然非常接近我的生产环境。 我目前正在尝试使用 Spring Boot 和 Primefaces 作为我选择的视图技术来构建一个项目,但是这个设置显然还没有像 Thymeleaf 那样开箱即用,因为它的二进制文件在类路径中足够的。我尝试包含以下 gradle/maven 工件,但没有成功:

  • 素面
  • jsf-api
  • jsf-impl
  • el-api

Spring Boot 的嵌入式 Tomcat 服务器启动时没有明显错误,但尝试在浏览器中打开 /view 等页面后,嵌入式服务器显示以下错误消息:HTTP Status 500 - Circular view path: would dispatch back to the current handler URL again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)

在几个不同的地方搜索后,我仍然找不到任何关于如何设置这样一个项目的资源/教程。 以下是我的 build.gradle 文件的内容:

buildscript {
    repositories {
        maven { url "http://repo.spring.io/libs-snapshot" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

idea {
    module {
        downloadJavadoc = true
    }
}

group = 'com.hello'
version = '0.0.1-SNAPSHOT'

repositories {
    mavenCentral()
    mavenLocal()
    maven { url "http://repository.primefaces.org" }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
//    compile ("org.thymeleaf:thymeleaf-spring4")
    compile group: 'org.primefaces', name: 'primefaces', version: '4.0'

    compile group: 'com.sun.faces', name: 'jsf-api', version: '2.2.2'
    compile group: 'com.sun.faces', name: 'jsf-impl', version: '2.2.2'
    compile group: 'javax.el', name: 'el-api', version: '1.0'
}

task wrapper(type: Wrapper) {
    gradleVersion=1.10
}

我的主要课程:

package com.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@EnableAutoConfiguration
@ComponentScan
@Configuration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

和我的 WebMvcConfigurerAdapter 实现:

package com.hello;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("view");
        registry.addViewController("/view").setViewName("view");
    }

}

连同我的 view.html 文件(我也尝试过 view.xhtml),这些是我为这个项目创建的所有文件,因为我正在尝试进行最小设置。

如果有人能看出我做错了什么(或根本没有做),我们将不胜感激。 在这种情况下,我是否需要额外的文件/bean 来进行 JSF 配置?如果是,应该将这些文件放在哪里以及如何放置?

这个问题也已经发布在 Spring 官方论坛上: http://forum.spring.io/forum/spring-projects/boot/746552-spring-boot-and-jsf-primefaces-richfaces


编辑: 在包含 M. Deinum 的 JSF 配置 bean 并删除 WebMvcConfigurerAdapter 定义(与 Spring MVC 相关)之后,Spring Boot 似乎将请求映射到 FacesServlet 实例,现在我收到以下错误消息: HTTP Status 500 - Servlet.init() for servlet FacesServlet threw exception

根本原因:
java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory

新 JsfConfig bean 的代码:

package com.hello;

import com.sun.faces.config.ConfigureListener;
import org.springframework.boot.context.embedded.ServletListenerRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.faces.webapp.FacesServlet;

@Configuration
public class JsfConfig {

    @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());
    }

    @Bean
    public ViewResolver getViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/templates/");
        resolver.setSuffix(".xhtml");
        return resolver;
    }
}

【问题讨论】:

  • 您的 view.html 文件在哪里?也许它没有找到那个文件?您是否尝试过将文件和视图名称更改为其他名称,例如 index?
  • @andersschuller 我复制了文件并尝试在main、main/resources 和main/resources/templates(Thymeleaf 的默认值)目录中将其命名为 index.htm,如果是这种情况我可能会缺少一些配置。

标签: spring jsf primefaces spring-boot


【解决方案1】:

您使用的 JSF 不能与 Spring MVC 一起使用,两者都是不同的技术。您必须正确设置 JSF。为此,您需要添加FacesServlet 和面孔ConfigureListener。这是正确设置JSF 所必需的,通常会自动检测到ConfigureListener,但嵌入式版本似乎并没有这样做。

@Configuration
public class JsfConfig {

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

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

    @Bean
    public ListenerRegistationBean jsfConfigureListener() {
        return new ListenerRegistrationBean(new ConfigureListener());           
    }       
}

类似的东西。我可能会建议您禁用 DispatcherServlet 等的自动配置,因为您使用的是 JSF 而不是 Spring MVC。

@EnableAutoConfiguration(exclude={WebMvcAutoConfiguration.class,DispatcherServletAutoConfiguration }

【讨论】:

  • @m-deinum 感谢您抽出宝贵时间。我读过 Spring MVC 是 Spring Boot 的默认设置,并且不与 JSF 一起工作,因为它们都试图解决相同的问题,但我不知道如何使用它设置 Spring Boot。您提议的 bean 似乎这样做了,只是现在我得到了一个不同的错误:java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory。对这个新错误的其他问题的回答或多或少将它与类路径中的多个 JSF 实现相关联。删除我的 JSF 工件没有用,而且我没有
  • 没有找到任何证据支持我的怀疑,即 Tomcat 嵌入可能带有自己的 JSF 版本,相反,我想在 Spring MVC 时将 Spring Boot 与 JSF 二进制文件捆绑是没有意义的是默认值。
  • 由于嵌入式/自定义类加载器,可能是类加载问题。也许您可以发出基于 JSF 的 Spring Boot 示例的请求。没有默认值,如果你使用 web 那么它是默认值但它不是 Spring Boot 默认值,你可以将 Spring Boot 用于不同的技术堆栈(有或没有 web 技术)。
【解决方案2】:

在看到 this question by louvelg 关于另一个问题在使用 Spring Boot 设置 FacesServlet 之后,我通过添加几个配置文件和来自 spring 的 ServletRegistrationBean 让它与 JSF/primefaces 一起工作-网络。 web.xml 和 faces-config.xml 文件有点伤害 Spring Boot 没有大量 xml 文件的最小设置的想法,但这是我能够用 JSF 找到的最小设置,如果有人知道更好/更清洁如何做到这一点,请告诉我。

这是我的 gradle.build 文件中的依赖项:

compile group: "org.springframework.boot", name: "spring-boot-starter"
compile group: "org.springframework", name: "spring-web", version: "4.0.2.RELEASE"

compile group: "org.apache.tomcat.embed", name: "tomcat-embed-core", version: tomcatVersion
compile group: "org.apache.tomcat.embed", name: "tomcat-embed-logging-juli", version: tomcatVersion
compile group: "org.apache.tomcat.embed", name: "tomcat-embed-jasper", version: tomcatVersion

compile group: "org.primefaces", name: "primefaces", version: "4.0"
compile group: "com.sun.faces", name: "jsf-api", version: "2.1.21"
compile group: "com.sun.faces", name: "jsf-impl", version: "2.1.21"

其中tomcatVersion 是“7.0.34”。这里的主要变化是:

  • 删除 spring-boot-starter-web,其中还包括 M. Deinum 指出的 Spring MVC
  • 包括spring-boot-starter(更容易启动)和spring-web
  • 由于spring-boot-starter-web 不再存在,因此显式包含 tomcat 嵌入的依赖项。

这是我主课的新内容:

package com.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.faces.webapp.FacesServlet;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

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

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

@EnableAutoConfiguration 如果您希望 Spring Boot 自动设置 Tomcat,并且 ServletRegistrationBean 将 xhtml 请求映射到您的 FacesServlet。

我的 webapp 目录中的 WEB-INF/faces-config.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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/web-facesconfig_2_2.xsd"
              version="2.2">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

web.xml 文件也在 webapp/WEB-INF 中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <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>*.xhtml</url-pattern>
    </servlet-mapping>
</web-app>

servlet 定义似乎是多余的,但由于某种原因,如果我从 web.xml 文件或 servlet 注册 bean 中删除它们,xhtml 页面的呈现将无法按预期工作或根本无法工作。编辑:原来 web.xml 文件中不需要 servlet 映射,这只是一个 IDE 问题,在我的情况下,Intellij Idea 不知道在 servlet 注册 bean 中定义的 servlet 映射,所以它在抱怨关于缺少预期 servlet 的映射,但应用程序仍然可以正常运行。

感谢所有做出贡献的人。

【讨论】:

  • 一个小补充:您必须将 IDE 的构建路径设置为 src/main/webapp/WEB-INF/classes(如果您想使用 IDE 调试器)。 Eclipse 默认将类放到 /bin 中。不幸的是,JSF 没有扫描这个文件夹,所以它没有找到任何@ManagedBean。我想您必须以类似的方式配置其他 IDE。
【解决方案3】:

我关注了这篇文章,但遇到了同样的错误。这有点骇人听闻,但目前这是我能得到的最好的。

将此添加到 web.xml 文件中的 src/main/webapp/WEB-INF 文件夹中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
</web-app>

这应该可以解决找不到备份工厂的问题。

收到此错误后,我遇到了更多问题

It appears the JSP version of the container is older than 2.1 and unable to locate the EL RI expression factory, com.sun.el.ExpressionFactoryImpl.  If not using JSP or the EL RI, make sure the context initialization parameter, com.sun.faces.expressionFactory, is properly set.

所以我添加了这个依赖

compile "org.glassfish.web:el-impl:2.2"

这就是我开始工作的时候。

我仍在努力使其不再需要 web.xml。因为它确实是对 Spring Boot 应该如何工作的一种破解。我在这方面取得的任何进一步进展,我都会尽量记住回复这个答案,但如果我不这样做,我会添加到我创建的这个示例应用程序中。

https://github.com/Zergleb/Spring-Boot-JSF-Example

【讨论】:

    【解决方案4】:

    我也遇到了同样的问题。我发现没有 [@EnableAutoConfiguration] 的解决方案略有不同:

    [pom.xml]

        <?xml version="1.0" encoding="UTF-8"?>
    <project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        ...
        <!-- http://stackoverflow.com/questions/25479986/spring-boot-with-jsf-could-not-find-backup-for-factory-javax-faces-context-face/25509937#25509937 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.2.4.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!-- JSF -->
            <dependency>
                <groupId>org.glassfish</groupId>
                <artifactId>javax.faces</artifactId>
                <version>2.2.11</version>
            </dependency>
            <!-- JSR-330 -->
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>
            <!-- JSP API -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
            </dependency>
            <!-- Spring web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>
            ...
        </dependencies>
        <build>
            <outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
        </build>    
    </project>
    

    和 Java 配置类:

    ...
    
    @Configuration
     public class JsfConfig extends SpringBootServletInitializer {
    
        @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;
        }
    
    }
    

    [WEB-INF/web.xml]

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">
    
        <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        </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>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
      </application>
    </faces-config>
    

    【讨论】:

      【解决方案5】:

      代码中的这一行正在注册一个控制器,当接收到/view 的请求时,该控制器将重定向到逻辑名称为view 的视图。

      registry.addViewController("/view").setViewName("view");
      

      然后视图解析器将获取逻辑名称,应用前缀和后缀并找到视图定义,例如view.xhtml。

      这里的问题似乎是正在添加默认视图解析器,它没有添加后缀或前缀。

      有关定义默认视图解析器的代码,请参阅WebMvcAutoConfiguration.defaultViewResolver()。

      默认视图解析器尝试加载一个名为 /view 的视图,这会再次触发同一个控制器,从而创建一个循环。

      尝试评论 addViewController 行或定义您自己的视图解析器,例如这样:

      @Bean
      public ViewResolver getViewResolver(){
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
          resolver.setPrefix("/WEB-INF/views/");
          resolver.setSuffix(".xhtml");
          return resolver;
      }
      

      【讨论】:

      • 感谢您为我回答并澄清了一些视图解析器管道。我尝试了自定义视图解析器,但没有任何改变,我仍然收到java.lang.IllegalStateException: Could not find backup for factory javax.faces.context.FacesContextFactory(在包含 M. Deinum 的 JSF 配置 bean 之后,它现在将 xhtml 文件映射到 Faces servlet)。
      猜你喜欢
      • 2011-04-21
      • 2015-01-10
      • 2014-08-12
      • 2012-11-07
      • 2021-01-26
      • 2012-07-21
      • 2013-06-30
      • 2014-04-27
      • 1970-01-01
      相关资源
      最近更新 更多