当前位置: 首页 > 知识库问答 >
问题:

Spring Boot和JSF/PrimeFaces/RichFaces

黄伟
2023-03-14

我与Spring的接触只有几个月,最近在浏览指南部分时遇到了Spring Boot。这些指南非常容易完成,并且能够很好地初步掌握项目的基本(也是很棒的)思想,即能够以最少的配置构建和部署企业级应用程序,同时坚持一系列Spring/JEE的良好实践。我真的对使用Spring Boot进行测试项目很感兴趣,因为有了它,设置和运行起来更容易、更快,而且仍然非常接近我的生产环境。我目前正在尝试用Spring Boot和Primefaces作为我的视图技术来构建一个项目,但是这个设置显然还没有像Thymeleaf那样准备就绪,对它来说,在类路径中有它的二进制文件就足够了。我试着包含了前面的Gradle/Maven工件,但没有成功:

  • PrimeFaces
  • JSF-API
  • JSF-IMPL
  • EL-API

Spring Boot的嵌入式Tomcat服务器启动时没有明显的错误,但在尝试在浏览器中打开/view之类的页面后,嵌入式服务器显示以下错误消息:HTTP Status 500-Circular view Path:would dispatch real to current handler URL。检查您的ViewResolver设置!(提示:这可能是由于生成默认视图名称而导致的未指定视图的结果。)

在几个不同的地方搜索后,我仍然没有找到关于如何建立这样一个项目的任何资源/教程。以下是我的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");
    }

}

这个问题也发布在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抛出异常

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;
    }
}

共有1个答案

居乐池
2023-03-14

您使用的JSF不能用于Spring MVC两者都是不同的技术。您必须正确设置JSF。为此,您需要添加FacesServlet和facesConfigureListener。这是正确设置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 }
 类似资料:
  • 问题内容: 使用JDK 1.6,JSF 2.1,PrimeFaces 2.2.1,POI 3.2和Apache Tomcat 7 我正在尝试设置一个servlet,以允许根据用户选择下载excel文件。excel文档是在运行时创建的。 没有错误,代码确实进入了servlet。 我单击按钮,没有任何反应。我没有使用PrimeFaces使用的数据表导出,因为我需要对Excel文档中的数据进行重新排序和

  • 并且也像这样(没有括号): 但不开火,请帮帮我....多谢

  • 我是Java新手,正在研究一些技术,我想知道是否有可能集成JSF、Spring和PrimeFaces。我正在寻找一些提示,但我只找到了JSF+Spring或Spring+Primefaces或Spring+JSF或JSF+Primefaces,但从来没有同时找到所有3个。有可能把它们都整合在一起? Att, 佩德罗·恩里克

  • 该场景是使用JSF和Primeface Mobile开发的移动页面。我想在多个页面之间的同一个xhtml页面中导航。当我单击主页中的按钮时,内容必须从第二页中的托管bean加载。我http://www.primefaces.org/showcase/mobile/navigation.xhtml遵循了这个示例,但我无法导航,我有以下消息“没有与viewIdmobile.xhtml匹配的导航用例,操

  • 我在我的UI上使用JSF的Primefaces。我对p:datatable上的ajax有一个特定的要求。这是我的项目的样本页面: 在这里,我有一个数据表,其中列出了创建的所有行。每行都有 id 和值。值是可编辑的。窗体中还有一个面板用于创建新行。后备 Bean 预先填充了所有创建的行,其中包含@PostConstruct。支持Bean有其他方法来编辑,取消和创建。此页面正在运行,下面给出的是新要求

  • JSF-2.0、Mojarra 2.1.19、PrimeFaces 3.4.1 问题摘要:在内部有一个和激发的inputText操作,该操作使用将dataTable行索引作为参数传递。但它总是传递dataTable的最后一行,而不是包含当前单击的的行的索引。 从我前面的问题中可以看出,我正在尝试使用作为一个状态的注释接受者,比如在Facebook等中。实现包括一个。它的行代表每种状态。好像是: 上