【发布时间】: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