您的位置:首页 > 编程语言 > Java开发

微服务,微架构[十一]springboot模板页面thymeleaf

2017-05-30 21:05 465 查看
springboot可以集成很多模板文件来实现访问页面,今天我们主要介绍thymeleaf的集成方式,很多集成模板页面都是大同小异,也是springboot推荐使用的页面方式
一、加入依赖pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、控制器访问页面
package com.didispace.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @author e生态
* @version 1.0.0
* @blog http://blog.csdn.net/ysl_228 *
*/
@Controller
public class HelloController {

@ResponseBody
@RequestMapping("/hello")
public String hello() {
return "Hello World";
}

@RequestMapping("/")
public String index(ModelMap map) {
map.addAttribute("host", "http://blog.csdn.net/ysl_228");
map.addAttribute("test", "1234");
return "index";
}

@RequestMapping("/mvc")
public String index(Model model) {
model.addAttribute("host", "http://blog.csdn.net/ysl_228");
model.addAttribute("test", "1234");
return "mvc";
}

}

三、访问页面,默认资源访问目录resource/template/index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>

</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐