Spring Boot 和 JSF/Primefaces/Richfaces

标签 spring jsf primefaces spring-boot

我接触 Spring 才几个月,最近在浏览指南部分时遇到了 Spring Boot。这些指南非常容易完成,并且可以很好地初步掌握项目的基本(和很棒的)想法,即能够以最少的配置构建和部署企业级应用程序,同时支持广泛的 Spring/JEE良好做法。我真的对使用 Spring Boot 进行测试项目很感兴趣,因为有了它,它们的设置和运行变得更加容易和快速,并且仍然非常接近我的生产环境。
我目前正在尝试使用 Spring Boot 和 Primefaces 作为我选择的 View 技术来构建一个项目,但是这个设置显然没有像 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
    }
    

    我的主要类(class):
    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;
        }
    }
    

    最佳答案

    您使用的 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 }
    

    关于Spring Boot 和 JSF/Primefaces/Richfaces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22544214/

    相关文章:

    java - 如何使用 p :tabMenu with Ajax

    java - 考虑在您的配置中定义一个类型为 'org.hibernate.SessionFactory' 的 bean

    spring - Elasticsearch Spring数据-文档注释中的动态indexName

    jsf - 整个应用程序的单个用户?

    java - 动态 Web 项目的资源包的推荐位置

    java - 如何设置 Primefaces 图表中轴标签的格式

    java - 如何在 Facelets 中使用循环

    spring - 使用带有库的 Proguard 有一个 @Service bean,它应该 Autowiring

    spring - 在 Hybris 控制台中处理警告

    jsf - 警告 : This page calls for XML namespace http://primefaces. prime.com.tr/ui 用前缀 p 声明,但该命名空间不存在标签库