![](https://csdnimg.cn/release/download_crawler_static/9957749/bg7.jpg)
@Bean
public ThymeleafViewResolver viewResolver(){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
// NOTE 'order' and 'viewNames' are optional
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[] {".html", ".xhtml"});
return viewResolver;
}
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<!-- NOTE 'order' and 'viewNames' are optional -->
<property name="order" value="1" />
<property name="viewNames" value="*.html,*.xhtml" />
</bean>
The templateEngine parameter is, of course, the SpringTemplateEngine object we defined in the previous chapter. The
other two ( order and viewNames ) are both optional, and have the same meaning as in the JSP ViewResolver we saw
before.
Note that we do not need prefix or suffix parameters, because these are already specified at the Template Resolver
(which in turn is passed to the Template Engine).
And what if we wanted to define a View bean and add some static variables to it? Easy, just define a prototype bean for it:
@Bean
@Scope("prototype")
public ThymeleafView mainView() {
ThymeleafView view = new ThymeleafView("main"); // templateName = 'main'
view.setStaticVariables(
Collections.singletonMap("footer", "The ACME Fruit Company"));
return view;
}
By doing this, you will be able to execute specifically this view bean selecting it by bean name ( mainView , in this case).
Page 7 of 34