Spring Boot和JSF/Primefaces/Richfaces [英] Spring Boot and JSF/Primefaces/Richfaces

查看:106
本文介绍了Spring Boot和JSF/Primefaces/Richfaces的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与Spring接触了仅仅几个月,最近在浏览指南部分时遇到了Spring Boot.这些指南非常容易完成,并且可以很好地初步理解项目的基本(很棒)构想,即能够以最少的配置构建和部署企业级应用程序,同时支持各种Spring/JEE.好的做法.我对将Spring Boot用于测试项目真的很感兴趣,因为有了它,它们的设置和运行变得非常容易和快捷,并且仍然非常接近我的生产环境. 我目前正在尝试使用Spring Boot和Primefaces作为我选择的视图技术来构建一个项目,但是这种设置显然还不能像Thymeleaf那样直接使用,因为二进制文件在类路径中足够的.我尝试包括下面的gradle/maven工件,但没有成功:

I have been in contact with Spring for just a few months now and recently came upon Spring Boot while browsing the guides section. The guides were very easy to complete and made for a good initial grasp of the projects's basic (and awesome) idea, which is to be able to build and deploy enterprise-level applications with minimal configuration while upholding to a wide array of Spring's/JEE's good practices. I am really interested in using Spring Boot for test projects since with it they are so much easier and faster to set up and run and still be very close to my production environment. I am currently trying to build a project with Spring Boot and Primefaces as my view technology of choice, but this setup apparently isn't ready out-of-the-box as is the case with Thymeleaf, for which having its binary in the classpath is enough. I tried including the folowing gradle/maven artifacts, with no success:

  • 素面
  • 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.)

Spring Boot's embedded Tomcat server starts with no apparent errors, but after trying to open a page such as /view in a browser, the embedded server displays the following error message: 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文件的内容:

After searching in several different places, I still fail to locate any resources/tutorials on how to set up such a project. Here are the contents of my build.gradle file:

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实现:

and my implementation of 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),这些都是我为最小化设置为该项目创建的所有文件.

Along with my view.html file (I also tried view.xhtml), these are all the files I created for this project as I'm trying for the minimal setup.

如果任何人都能看到我做错了(或根本没有做),将不胜感激. 在这种情况下,是否需要用于JSF配置的其他文件/bean?如果是这样,应该在哪里以及如何放置这些文件?

If anyone can see what I'm doing wrong (or not doing at all), help would be much appreciated. Do I need extra files/beans for JSF configuration in this case? If so, where and how should put such files?

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

This question has also been posted in the official Spring forums: http://forum.spring.io/forum/spring-projects/boot/746552-spring-boot-and-jsf-primefaces-richfaces

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

After including M. Deinum's configuration bean for JSF and removing the WebMvcConfigurerAdapter definition (related to Spring MVC), Spring Boot seems to map requests to the FacesServlet instance, now I get the following error message: 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的代码:

The code for the new 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;
    }
}

推荐答案

看到此问题通过louvelg关于另一个问题的问题 ,在使用Spring Boot设置FacesServlet之后,我通过添加几个配置文件以及来自spring-web的ServletRegistrationBean使其与JSF/primefaces一起使用. web.xml和faces-config.xml文件对Spring Boot的最小化设置(无大量xml文件)的想法有点伤害,但是如果有人知道更好/更干净的方法,这就是我在JSF上能够找到的最小化设置这样的方法,请让我知道.

After seeing this question by louvelg about a different problem after setting up the FacesServlet with Spring Boot, I got it working with JSF/primefaces by adding a couple configuration files and also a ServletRegistrationBean from spring-web. The web.xml and faces-config.xml files kind of hurt Spring Boot's idea of minimal set up without tons of xml files, but this is the minimal set up I was able to find with JSF, if anyone knows of a better/cleaner way to do this, please let me know.

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

Here are the dependencies in my gradle.build file:

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".这里的主要更改是:

where tomcatVersion is "7.0.34". The key changes here are:

  • 删除spring-boot-starter-web,其中还包括M. Deinum指出的Spring MVC
  • 包括spring-boot-starter(更容易启动)和spring-web
  • 由于spring-boot-starter-web不再存在,因此明确包括了tomcat嵌入的依赖项.
  • Removing spring-boot-starter-web, which also includes Spring MVC as pointed out by M. Deinum
  • Including spring-boot-starter (easier startup) and spring-web
  • Explicitly including the tomcat-embed dependencies since spring-boot-starter-web is not here anymore.

这是我主班的新内容:

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.

@EnableAutoConfiguration if you want Spring Boot to automatically set up Tomcat, and the ServletRegistrationBean mapping xhtml requests to your FacesServlet.

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

The WEB-INF/faces-config.xml file in my webapp directory:

<?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>

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

and the web.xml file also in 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的映射的信息,但是应用程序仍然可以正常运行.

The servlet definitions seem redundant, but for some reason if I remove them either from the web.xml file or the servlet registration bean, rendering of xhtml pages doesn't work as expected or not at all. turns out the servlet mapping isn't necessary in the web.xml file, it was just an IDE problem, in my case Intellij Idea doesn't know about the servlet mapping being defined in the servlet registration bean, so it was complaining about missing mappings for the expected servlet, but the application runs without problems nevertheless.

感谢所有贡献者.

这篇关于Spring Boot和JSF/Primefaces/Richfaces的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆