为了验证Spring中RestController与Controller 与 Model,ModelMap以及ModelAndView之间的关系,创建一个spring boot项目,整合thymeleaf。
一、spring-boot2.x 与thymeleaf的整合
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、添加配置
###ThymeLeaf配置
spring:
thymeleaf:
#模板的模式,支持 HTML, XML TEXT JAVASCRIPT
mode: HTML5
#编码 可不用配置
encoding: UTF-8
#内容类别,可不用配置
#开发配置为false,避免修改模板还要重启服务器
cache: false
#配置模板路径,默认是templates,可以不用配置
prefix: classpath:/templates/
#名称的后缀
suffix: .html
content-type: text/html
注意:thymeleaf的模板文件位置prefix的值是 /templat
Spring RestController与Controller的区别及Model家族解析

本文探讨了Spring中@RestController与@Controller在处理HTTP请求时的区别,并详细阐述了Model, ModelMap以及ModelAndView之间的关系。在Spring Boot 2.x与Thymeleaf的整合中,通过实例展示了如何使用这些组件。测试结果显示,使用@RestController注解时,Model和ModelMap无法解析视图,而ModelAndView可以。相反,@Controller注解下,Model和ModelMap不能与@ResponseBody结合使用。"
76087743,560704,PaxCompiler字符串编码问题及解决方案,"['PaxCompiler', '游戏开发', '脚本语言', '字符串处理']
最低0.47元/天 解锁文章
4万+

被折叠的 条评论
为什么被折叠?



