通过教程(
https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#integrating-thymeleaf-with-spring)学习,创建的项目。
thymeleaf配置:
<?
xml version
="1.0"
encoding
="UTF-8"
?>
<beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:
xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi
:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
<!-- SpringResourceTemplateResolver automatically integrates with Spring's own -->
<!-- resource resolution infrastructure, which is highly recommended. -->
<bean
id
="templateResolver"
class
="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"
>
<property
name
="prefix"
value
="/WEB-INF/templates/"
/>
<property
name
="suffix"
value
=".html"
/>
<!-- HTML is the default value, added here for the sake of clarity. -->
<property
name
="templateMode"
value
="HTML"
/>
<!-- Template cache is true by default. Set to false if you want -->
<!-- templates to be automatically updated when modified. -->
<property
name
="cacheable"
value
="false"
/>
</bean>
<!-- SpringTemplateEngine automatically applies SpringStandardDialect and -->
<!-- enables Spring's own MessageSource message resolution mechanisms. -->
<bean
id
="templateEngine"
class
="org.thymeleaf.spring4.SpringTemplateEngine"
>
<property
name
="templateResolver"
ref
="templateResolver"
/>
<!-- Enabling the SpringEL compiler with Spring 4.2.4 or newer can speed up -->
<!-- execution in most scenarios, but might be incompatible with specific -->
<!-- cases when expressions in one template are reused across different data -->
<!-- ypes, so this flag is "false" by default for safer backwards -->
<!-- compatibility. -->
<property
name
="enableSpringELCompiler"
value
="true"
/>
</bean>
<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>
</beans>
Controller里面的配置:
@Controller
@RequestMapping
(
"schedule"
)
public class
TimeScheduleController {
@RequestMapping
(
"list2"
)
public
String
list2
(){
return
"list2"
;
}
}
启动后访问url:http://localhost:8080/schedule/list2报错,Could not resolve view with name 'list2' in servlet with name 'SpringMvc'
问题出现在配置文件中的viewNames配置:
<property
name
="viewNames"
value
="*.html,*.xhtml"
/>
这是一个特殊设置,如果你项目里面全部使用的thymeleaf模板,这个配置可以去掉;如果你的项目里面还有jsp、freemarker等模板,这个的作用就是当你返回特定格式时(以.html或者.xhtml结尾的字符串)才会走thymeleaf模板配置。
这种情况改造有两种方式,
一、去掉viewName配置;
二、controller里面的return内容由“list2”改成“list2.html”。
参考: