1. Overview
1.概述
All MVC frameworks provide a way of working with views.
所有的MVC框架都提供了一种处理视图的方法。
Spring does that via the view resolvers, which enable you to render models in the browser without tying the implementation to a specific view technology.
Spring通过视图解析器做到了这一点,它使你能够在浏览器中渲染模型,而无需将实现与特定的视图技术捆绑在一起。
The ViewResolver maps view names to actual views.
ViewResolver将视图名称映射到实际视图。
And the Spring framework comes with quite a few view resolvers e.g. InternalResourceViewResolver, BeanNameViewResolver, and a few others.
而Spring框架带有相当多的视图解析器,例如InternalResourceViewResolver、BeanNameViewResolver,和其他一些。
This is a simple tutorial showing how to set up the most common view resolvers and how to use multiple ViewResolver in the same configuration.
这是一个简单的教程,展示了如何设置最常见的视图解析器,以及如何在同一配置中使用多个ViewResolver。
2. The Spring Web Configuration
2.Spring Web的配置
Let’s start with the web configuration; we’ll annotate it with @EnableWebMvc, @Configuration and @ComponentScan:
让我们从Web配置开始;我们将用@EnableWebMvc、@Configuration和@ComponentScan来注解它。
@EnableWebMvc
@Configuration
@ComponentScan("com.baeldung.web")
public class WebConfig implements WebMvcConfigurer {
// All web configuration will go here
}
It’s here that we’ll set up our view resolver in the configuration.
在这里,我们将在配置中设置我们的视图解析器。
3. Add an InternalResourceViewResolver
3.添加一个InternalResourceViewResolver
This ViewResolver allows us to set properties such as prefix or suffix to the view name to generate the final view page URL:
这个ViewResolver允许我们为视图名称设置前缀或后缀等属性,以生成最终的视图页面URL。
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
For such simplicity of the example, we don’t need a controller to process the request.
对于 这种简单性的例子,我们不需要控制器来处理请求。
We only need a simple jsp page, placed in the /WEB-INF/view folder as defined in the configuration:
我们只需要一个简单的jsp页面,放在配置中定义的/WEB-INF/view文件夹中。
<html>
<head></head>
<body>
<h1>This is the body of the sample view</h1>
</body>
</html>
4. Add a BeanNameViewResolver
4.添加一个BeanNameViewResolver
This is an implementation of ViewResovler that interprets a view name as a bean name in the current application context. Each such View
can be defined as a bean in XML or Java configurations.
这是ViewResovler的一个实现,它将视图名称解释为当前应用程序上下文中的bean名称。每个这样的View
可以被定义为XML或Java配置中的bean。
First, we add the BeanNameViewResolver to the previous configuration:
首先,我们将BeanNameViewResolver添加到之前的配置中。
@Bean
public BeanNameViewResolver beanNameViewResolver(){
return new BeanNameViewResolver();
}
Once the ViewResolver is defined we need to define beans of the type View so that it can be executed by DispatcherServlet to render the view:
一旦定义了ViewResolver,我们就需要定义View类型的Bean,这样它就能被DispatcherServlet执行,以渲染视图。
@Bean
public View sample() {
return new JstlView("/WEB-INF/view/sample.jsp");
}
Here is the corresponding handler method from the controller class:
下面是控制器类中相应的处理方法。
@GetMapping("/sample")
public String showForm() {
return "sample";
}
From the controller method, the view name is returned as “sample” which means the view from this handler method resolves to JstlView class with /WEB-INF/view/sample.jsp
URL.
从控制器方法中,视图名称被返回为”sample” ,这意味着从这个处理方法中的视图被解析为JstlView类的/WEB-INF/view/sample.jsp
URL。
5. Chaining ViewResolvers and Define an Order Priority
5.串联ViewResolvers和定义订单优先级
Spring MVC also supports multiple view resolvers.
Spring MVC还支持多个视图解析器。
This allow you to override specific views in some circumstances. We can simply chain view resolvers by adding more than one resolver to the configuration.
这允许你在某些情况下覆盖特定的视图。我们可以通过向配置中添加一个以上的解析器来简单地连锁视图解析器。
Once we’ve done that, we’ll need to define an order for these resolvers. The order property is used to define which is the order of invocations in the chain. The higher the order property (largest order number), the later the view resolver is positioned in the chain.
一旦我们完成了这些工作,我们就需要为这些解析器定义一个顺序。order属性用于定义链中调用的顺序。顺序属性越高(最大的顺序号),视图解析器在链中的位置就越靠后。
To define the order we can add the follow line of code to the configuration of the our view resolvers:
为了定义顺序,我们可以在我们的视图解析器的配置中添加下面这行代码。
bean.setOrder(0);
Be careful on the order priority as the InternalResourceViewResolver should have a higher order – because it’s intended to represent a very explicit mapping. And if other resolvers have a higher order, then the InternalResourceViewResolver might never be invoked.
请注意顺序优先级,因为 InternalResourceViewResolver 应该有更高的顺序–因为它的目的是代表一个非常明确的映射。如果其他解析器有更高的顺序,那么InternalResourceViewResolver可能永远不会被调用。
6. Using Spring Boot
6.使用Spring Boot
When working with Spring Boot, the WebMvcAutoConfiguration automatically configures InternalResourceViewResolver and BeanNameViewResolver beans in our application context.
在使用Spring Boot时,WebMvcAutoConfiguration会自动配置InternalResourceViewResolver和BeanNameViewResolverbeans在我们的应用上下文。
Also, adding the corresponding starter for the templating engine takes away much of the manual configuration we have to do otherwise.
另外,为模板引擎添加相应的启动器,可以消除我们必须做的许多手动配置。
For example, by adding spring-boot-starter-thymeleaf dependency to our pom.xml, Thymeleaf gets enabled, and no extra configuration is necessary:
例如,将spring-boot-starter-thymeleaf依赖性添加到我们的pom.xml中,Thymeleaf就会被启用,而不需要额外的配置。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>${spring-boot-starter-thymeleaf.version}</version>
</dependency>
This starter dependency configures ThymeleafViewResolver bean with the name thymeleafViewResolver in our application context. We can override the auto-configured ThymeleafViewResolver by providing a bean of the same name.
这个启动依赖在我们的应用程序上下文中配置了ThymeleafViewResolverbean,其名称为thymeleafViewResolver。我们可以通过提供一个同名的bean来覆盖自动配置的ThymeleafViewResolver。
Thymeleaf view resolver works by surrounding the view name with a prefix and suffix. The default values of prefix and suffix are ‘classpath:/templates/’ and ‘.html’, respectively.
Thymeleaf视图解析器通过用前缀和后缀包围视图名称来工作。前缀和后缀的默认值分别为 “classpath:/templates/”和”.html”。
Spring Boot also provides an option to change the default value of prefix and suffix by setting spring.thymeleaf.prefix
and spring.thymeleaf.suffix
properties respectively.
Spring Boot还提供了一个选项,通过分别设置spring.thymeleaf.prefix
和spring.thymeleaf.suffix
属性来改变前缀和后缀的默认值。
Similarly, we have starter dependencies for groovy-templates, freemarker, and mustache template engines which we can use to get the corresponding view resolvers auto-configured using Spring Boot.
同样地,我们有groovy-templates、freemarker和mustache模板引擎的启动依赖,我们可以使用Spring Boot获得相应的视图解析器自动配置。
DispatcherServlet uses all the view resolvers it finds in the application context and tries each one until it gets a result and hence the ordering of these view resolvers becomes very important if we plan to add our own.
DispatcherServlet使用它在应用程序上下文中发现的所有视图解析器,并尝试每一个,直到得到一个结果,因此,如果我们计划添加我们自己的视图解析器,这些视图解析器的排序变得非常重要。
7. Conclusion
7.结论
In this tutorial we configured a chain of view resolvers using Java configuration. By playing with the order priority we can set the order of their invocation.
在本教程中,我们使用Java配置配置了一个视图解析器链。通过玩弄顺序优先级,我们可以设置其调用的顺序。
The implementation of this simple tutorial can be found in the github project.
这个简单教程的实现可以在github项目中找到。