SpringBoot入門系列(三)---檢視解析
阿新 • • 發佈:2019-02-03
SpirngBoot – html
靜態訪問
springboot 預設靜態資源路徑 src/main/resource/static 在static下新增 a.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>a</title>
</head>
<body>
<h1>this is a.html</h1>
</body>
</html>
@Controller
@RequestMapping("html")
public class HtmlController {
private static Logger log = LoggerFactory.getLogger(HtmlController.class);
@GetMapping("a")
public String a() {
log.info("http req HtmlController a");
return "/a.html";
}
}
預設靜態資源路徑可以配置多個
#HTTP埠,預設8080
server:
port: 8081
#spring-boot 定義靜態資源的對映
spring:
resources:
static-locations:
- classpath:/static/
- classpath:/html/
動態(模板)訪問
pom引入模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId >
</dependency>
預設動態模板路徑 src/main/resource/templates
可以修改
#HTTP埠,預設8080
server:
port: 8081
#spring-boot 定義靜態資源的對映
spring:
resources:
static-locations:
- classpath:/static/
# thymeleaf動態模板屬性
thymeleaf:
prefix: classpath:/html/#路徑
suffix: .html
cache: false
html 動態取值
<!DOCTYPE html>
<!-- 這行不可少 -->
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8">
<title>b</title>
</head>
<body>
<h1>this is b.html</h1>
<!-- 這行不可少 取值 th:text -->
<p th:text="${msg}"></p>
</body>
</html>