Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

发布时间:2020-08-14 19:14:18 作者:hky87
来源:ITPUB博客 阅读:171

Spring Boot中,

       Groovy

       Thymeleaf

        Mustache

Spring boot不建议使用jsp开发web。


本文使用Thymeleaf作为模板引擎来开发web项目。

一、在application.properties中设置Thymeleaf相关属性

# Check that the template exists before rendering it.
spring.thymeleaf.check-template=true
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value written to HTTP responses.
spring.thymeleaf.content-type=text/html
# Enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.enable=true
# Template files encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.exclude-view-names=index
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5
# Disable template caching.
spring.thymeleaf.cache=false


二、在pom.xml添加依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>


三、在resources目录下创建templates文件夹,并添加模板html

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
姓名:<span th:text="${user.username}"></span></br>
邮箱:<span th:text="${user.email}"></span></br>
昵称:<span th:text="${user.nickname}"></span>
</body>
</html>


四、增加Controller

@Controller
public class TemplatesController {
	
	@Autowired
	private UserService userService;	
	@RequestMapping("/index")
	String test(ModelMap map) {
		map.addAttribute("key", "thymeleaf");
		return "index";
	}  
	
	@RequestMapping("/test")
	public String testThymeleaf(ModelMap map) {
		map.addAttribute("user", userService.selectByPrimaryKey(1));
		return "test";
    }
}


五、运行结果

Spring Boot + Mybatis + Spring MVC环境配置(五):templates模板使用

到这里,Spring Boot + Mybatis +Spring MVC环境就搭建完成了


完整环境下载地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC  

推荐阅读:
  1. Java秋招面试复习大纲(一):Spring全家桶+MyBatis+MongDB+微服务题目详解
  2. Spring Boot Mybatis Thymeleaf项目构建

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

boot 使用 mvc

上一篇:medini analyze — 符合ISO 26262 的功能安全平台工具

下一篇:使用RMAN备份对Non-CDB中的表按时间点进行恢复

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》