From 03df5ac92fefad18f868380010f2fbd62bc270e7 Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Sun, 29 Mar 2015 23:41:19 +0800 Subject: [PATCH] Update How-to_ guides.md --- How-to_ guides.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/How-to_ guides.md b/How-to_ guides.md index 521bb9b..4beed06 100644 --- a/How-to_ guides.md +++ b/How-to_ guides.md @@ -581,11 +581,28 @@ Spring Boot采用Servlet 3 `javax.servlet.http.Part` API来支持文件上传。 * 关闭Spring MVC DispatcherServlet - +Spring Boot想要服务来自应用程序root `/`下的所有内容。如果你想将自己的servlet映射到该目录下也是可以的,但当然你可能失去一些Boot MVC特性。为了添加你自己的servlet,并将它映射到root资源,你只需声明一个Servlet类型的`@Bean`,并给它特定的bean名称`dispatcherServlet`(如果只想关闭但不替换它,你可以使用该名称创建不同类型的bean)。 * 关闭默认的MVC配置 + +完全控制MVC配置的最简单方式是提供你自己的被`@EnableWebMvc`注解的`@Configuration`。这样所有的MVC配置都逃不出你的掌心。 + * 自定义ViewResolvers +ViewResolver是Spring MVC的核心组件,它负责转换`@Controller`中的视图名称到实际的View实现。注意ViewResolvers主要用在UI应用中,而不是REST风格的服务(View不是用来渲染`@ResponseBody`的)。Spring有很多你可以选择的ViewResolver实现,并且Spring自己对如何选择相应实现也没发表意见。另一方面,Spring Boot会根据classpath上的依赖和应用上下文为你安装一或两个ViewResolver实现。DispatcherServlet使用所有在应用上下文中找到的解析器(resolvers),并依次尝试每一个直到它获取到结果,所以如果你正在添加自己的解析器,那就要小心顺序和你的解析器添加的位置。 + +WebMvcAutoConfiguration将会为你的上下文添加以下ViewResolvers: + +- bean id为'defaultViewResolver'的InternalResourceViewResolver。这个会定位可以使用DefaultServlet渲染的物理资源(比如,静态资源和JSP页面)。它在视图(view name)上应用了一个前缀和后缀(默认都为空,但你可以通过`spring.view.prefix`和`spring.view.suffix`外部配置设置),然后查找在servlet上下文中具有该路径的物理资源。可以通过提供相同类型的bean覆盖它。 +- id为'beanNameViewResolver'的BeanNameViewResolver。这是视图解析器链的一个非常有用的成员,它可以在View被解析时收集任何具有相同名称的beans。 +- id为'viewResolver'的ContentNegotiatingViewResolver只会在实际View类型的beans出现时添加。这是一个'主'解析器,它的职责会代理给其他解析器,它会尝试找到客户端发送的一个匹配'Accept'的HTTP头部。这有一篇有用的,关于你需要更多了解的[ContentNegotiatingViewResolver](https://spring.io/blog/2013/06/03/content-negotiation-using-views)的博客,也要具体查看下源码。通过定义一个名叫'viewResolver'的bean,你可以关闭自动配置的ContentNegotiatingViewResolver。 +- 如果使用Thymeleaf,你将有一个id为'thymeleafViewResolver'的ThymeleafViewResolver。它会通过加前缀和后缀的视图名来查找资源(外部配置为`spring.thymeleaf.prefix`和`spring.thymeleaf.suffix`,对应的默认为'classpath:/templates/'和'.html')。你可以通过提供相同名称的bean来覆盖它。 +- 如果使用FreeMarker,你将有一个id为'freeMarkerViewResolver'的FreeMarkerViewResolver。它会使用加前缀和后缀(外部配置为`spring.freemarker.prefix`和`spring.freemarker.suffix`,对应的默认值为空和'.ftl')的视图名从加载路径(外部配置为`spring.freemarker.templateLoaderPath`,默认为'classpath:/templates/')下查找资源。你可以通过提供一个相同名称的bean来覆盖它。 +- 如果使用Groovy模板(实际上只要你把groovy-templates添加到classpath下),你将有一个id为'groovyTemplateViewResolver'的`Groovy TemplateViewResolver`。它会使用加前缀和后缀(外部属性为`spring.groovy.template.prefix`和`spring.groovy.template.suffix`,对应的默认值为'classpath:/templates/'和'.tpl')的视图名从加载路径下查找资源。你可以通过提供一个相同名称的bean来覆盖它。 +- 如果使用Velocity,你将有一个id为'velocityViewResolver'的VelocityViewResolver。它会使用加前缀和后缀(外部属性为`spring.velocity.prefix`和`spring.velocity.suffix`,对应的默认值为空和'.vm')的视图名从加载路径(外部属性为`spring.velocity.resourceLoaderPath`,默认为'classpath:/templates/')下查找资源。你可以通过提供一个相同名称的bean来覆盖它。 + +具体参考[WebMvcAutoConfiguration](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java),[ThymeleafAutoConfiguration](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java),[FreeMarkerAutoConfiguration](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java),[GroovyTemplateAutoConfiguration](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java),[VelocityAutoConfiguration](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java)。 + ### 日志 * 配置Logback @@ -595,3 +612,5 @@ Spring Boot采用Servlet 3 `javax.servlet.http.Part` API来支持文件上传。 ### 数据访问 + +