SpringBoot之thymeleaf页面(资源文件css、js的引入,赋值)
1.目录结构:
pom引入
<!–thymeleaf –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.yml
spring:
thymeleaf:
#缓冲的配置
cache: false
check-template: true
check-template-location: true
#开启MVC thymeleaf 视图解析
enabled: true
encoding: utf-8
mode: HTML5
prefix: classpath:/templates/
suffix: .html
控制器
//页面跳转不能使用 @RestController 而是使用@Controller
@Controller
@RequestMapping(“/home”)
public class HomeController {
@RequestMapping(value=”/index”, method=RequestMethod.GET)
//加了@ResponseBody 就是返回字符串了
public String index(ModelMap map) {
//返回值给页面
map.addAttribute(“name”, “银众网”);
return “index”;
}
}
很简单的css
style.css:
.login-title{
font-size: 30px;
color: red;
}
很简单的js
home.js:
alert(“1″);
页面
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Index thymeleaf</title>
<link rel=”stylesheet” href=”/css/style.css”/>
<script src=”/js/home.js”></script>
</head>
<body>
<h1>HELLO INDEX THYMELEAF</h1>
你好,<h2 th:text=”${name}”></h2>
<p class=”login-title”>哈哈 NBA</p>
</body>
</html>