Thymeleaf3与Spring5集成报错Could not resolve view with name 'list2' in servlet

通过教程( https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html#integrating-thymeleaf-with-spring)学习,创建的项目。

thymeleaf配置:
xml version ="1.0" encoding ="UTF-8" ?>
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" >

id ="templateResolver" class ="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver" >
name ="prefix" value ="/WEB-INF/templates/" />
name ="suffix" value =".html" />
name ="templateMode" value ="HTML" />
name ="cacheable" value ="false" />

id ="templateEngine" class ="org.thymeleaf.spring4.SpringTemplateEngine" >
name ="templateResolver" ref ="templateResolver" />
name ="enableSpringELCompiler" value ="true" />

class ="org.thymeleaf.spring4.view.ThymeleafViewResolver" >
name ="templateEngine" ref ="templateEngine" />
name ="order" value ="1" />
name ="viewNames" value ="*.html,*.xhtml" />
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配置: name ="viewNames" value ="*.html,*.xhtml" />
这是一个特殊设置,如果你项目里面全部使用的thymeleaf模板,这个配置可以去掉;如果你的项目里面还有jsp、freemarker等模板,这个的作用就是当你返回特定格式时(以.html或者.xhtml结尾的字符串)才会走thymeleaf模板配置。
这种情况改造有两种方式,
一、去掉viewName配置;
二、controller里面的return内容由“list2”改成“list2.html”。

参考:
https://stackoverflow.com/questions/13105048/spring-with-thymeleaf-view-integration
http://forum.thymeleaf.org/Issue-with-my-Thymeleaf-Spring-configuration-td4024996.html

你可能感兴趣的:(spring,Thymeleaf)