我已经与Spring接触了几个月,最近在浏览指南部分时遇到了Spring Boot。这些指南非常容易完成,并且可以很好地初步理解项目的基本(很棒)思想,即能够以最少的配置来构建和部署企业级应用程序,同时支持各种Spring / JEE。好的做法。我真的很想将Spring Boot用于测试项目,因为有了它,它们的设置和运行变得非常容易和快捷,并且仍然非常接近我的生产环境。我目前正在尝试使用Spring Boot和Primefaces作为我选择的视图技术来构建一个项目,但是这种设置显然还不能像Thymeleaf那样立即可用,因为它的二进制文件位于类路径中足够。
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.)
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)外,这些都是我为最小化设置尝试为此项目创建的所有文件。
如果有人看到我做错了(或根本没有做),将不胜感激。在这种情况下,是否需要用于JSF配置的其他文件/ bean?如果是这样,应该在哪里以及如何放置这些文件?
这个问题也已经发布在Spring的官方论坛上: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
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; } }
在用Spring Boot设置FacesServlet 之后 ,大惊小怪地看到了这个问题 之后 ,我通过添加几个配置文件以及来自spring-web的ServletRegistrationBean使其与JSF / primefaces一起使用。web.xml和faces-config.xml文件对Spring Boot的最小化设置(无大量xml文件)的想法有点伤害,但这是我可以用JSF找到的最小化设置,如果有人知道更好/更干净的话方式,请让我知道。
这是我的gradle.build文件中的依赖项:
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”。这里的主要更改是:
tomcatVersion
spring-boot-starter-web
spring-boot-starter
spring-web
这是我的主班的新内容:
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
我的webapp目录中的WEB-INF / faces-config.xml文件:
<?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中:
<?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的映射的信息,但是该应用程序仍然可以正常运行。
感谢所有贡献者。