问题描述
我接触 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 实现
- el-api
Spring Boot 的内嵌 Tomcat 服务器启动时没有明显错误,但是尝试在浏览器中打开/view 等页面后,内嵌服务器显示以下错误信息:HTTP Status 500 - Circular view path: would再次调度回当前处理程序 URL.检查您的 ViewResolver 设置!(提示:由于默认视图名称生成,这可能是未指定视图的结果.)
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.
如果有人能看到我做错了什么(或根本没有做),将不胜感激.在这种情况下,我是否需要额外的文件/bean 来进行 JSF 配置?如果是这样,应该将这些文件放在何处以及如何放置?
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?
这个问题也已经在Spring官方论坛发过了: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
在包含 M. Deinum 的 JSF 配置 bean 并删除 WebMvcConfigurerAdapter 定义(与 Spring MVC 相关)后,Spring Boot 似乎将请求映射到 FacesServlet 实例,现在我收到以下错误消息:HTTP 状态 500 - servlet FacesServlet 的 Servlet.init() 抛出异常
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;
}
}
推荐答案
看到这个问题后 by 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
- 显式包含 tomcat 嵌入的依赖项,因为
spring-boot-starter-web
不再存在.
- Removing
spring-boot-starter-web
, which also includes Spring MVC as pointed out by M. Deinum - Including
spring-boot-starter
(easier startup) andspring-web
- Explicitly including the tomcat-embed dependencies since
spring-boot-starter-web
is not here anymore.
这是我的主类的新内容:
Here are the new contents of my main class:
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>
和 web.xml 文件也在 webapp/WEB-INF 中:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!