# 76.8 自定义ViewResolvers

`ViewResolver`是Spring MVC的核心组件，它负责转换`@Controller`中的视图名称到实际的`View`实现。注意`ViewResolvers`主要用在UI应用中，而不是REST风格的服务（`View`不是用来渲染`@ResponseBody`的）。Spring有很多你可以选择的`ViewResolver`实现，并且Spring自己对如何选择相应实现也没发表意见。另一方面，Spring Boot会根据classpath上的依赖和应用上下文为你安装一或两个`ViewResolver`实现。`DispatcherServlet`使用所有在应用上下文中找到的解析器（resolvers），并依次尝试每一个直到它获取到结果，所以如果你正在添加自己的解析器，那就要小心顺序和你的解析器添加的位置。

`WebMvcAutoConfiguration`将会为你的上下文添加以下`ViewResolvers`：

* 名为`defaultViewResolver`的`InternalResourceViewResolver`，它会定位可以使用`DefaultServlet`渲染的物理资源（比如静态资源和JSP页面）。它在视图名上应用了一个前缀和后缀（默认都为空，但你可以通过`spring.view.prefix`和`spring.view.suffix`设置），然后查找在servlet上下文中具有该路径的物理资源，可以通过提供相同类型的bean覆盖它。
* 名为`beanNameViewResolver`的`BeanNameViewResolver`，它是视图解析器链的一个非常有用的成员，可以在`View`解析时收集任何具有相同名称的beans，没必要覆盖或替换它。
* 名为`viewResolver`的`ContentNegotiatingViewResolver`，它只会在实际`View`类型的beans出现时添加。这是一个'master'解析器，它的职责会代理给其他解析器，它会尝试找到客户端发送的一个匹配'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，你将有一个名为`freeMarkerViewResolver`的`FreeMarkerViewResolver`，它会使用加前缀和后缀。它通过在视图名周围加上前缀和后缀来查找加载器路径中的资源(加载器路径外化为`spring.freemarker.templateLoaderPath`，默认值为`classpath:/templates/`)。前缀外部化为`spring.freemarker.prefix`，后缀外部化为`spring.freemarker.suffix`。前缀和后缀的默认值为空和`.ftl`。你可以通过提供同名的bean来覆盖`FreeMarkerViewResolver`。
* 如果使用Groovy模板（实际上只要你把groovy-templates添加到classpath下），你将有一个名为`groovyTemplateViewResolver`的`Groovy TemplateViewResolver`。它会使用加前缀和后缀（外部属性为`spring.groovy.template.prefix`和`spring.groovy.template.suffix`，对应的默认值为`classpath:/templates/`和`.tpl`）的视图名从加载路径下查找资源。你可以通过提供同名的bean来覆盖它。

更多详情，可查看下列章节：

* [WebMvcAutoConfiguration](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java)
* [ThymeleafAutoConfiguration](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java)
* [FreeMarkerAutoConfiguration](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfiguration.java)
* [GroovyTemplateAutoConfiguration](https://github.com/spring-projects/spring-boot/tree/v2.0.0.RELEASE/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jack80342.gitbook.io/spring-boot/ix.-how-to-guides/76.-spring-mvc/76.8-customize-viewresolvers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
