Spring Boot开发之模板引擎
1 视图解析
SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。
1 视图解析原理
1 目标方法处理的过程中,所有数据都会被放在 ModelAndViewContainer 里面。包括数据和视图地址
2 方法的参数是一个自定义类型对象(从请求参数中确定的),把他重新放在 ModelAndViewContainer
3 任何目标方法执行完成以后都会返回 ModelAndView(数据和视图地址)
4 processDispatchResult 处理派发结果(页面改如何响应)
- render(mv, request, response); 进行页面渲染逻辑
- 根据方法的String返回值得到 View 对象【定义了页面的渲染逻辑】
- 所有的视图解析器尝试是否能根据当前返回值得到View对象
- 得到了 redirect:/main.html --> Thymeleaf new RedirectView()
- ContentNegotiationViewResolver 里面包含了下面所有的视图解析器,内部还是利用下面所有视图解析器得到视图对象
- view.render(mv.getModelInternal(), request, response); 视图对象调用自定义的render进行页面渲染工作
- 视图解析
- **返回值以 forward: 开始: new InternalResourceView(forwardUrl); --> 转发 ** request.getRequestDispatcher(path).forward(request, response);
- 返回值以 redirect: 开始: new RedirectView() --》 render就是重定向
- 返回值是普通字符串: new ThymeleafView()—>
2 模板引擎-Thymeleaf
1 概述
Thymeleaf 是一个现代化、服务端Java模板引擎
2 基本语法
1 表达式
表达式名字 | 语法 |
---|---|
变量取值 | ${…} |
选择变量 | *{…} |
消息 | #{…} |
链接 | @{…} |
片段表达式 | ~{…} |
2 字面量
文本值: ‘one text’ , ‘Another one!’
数字: 0 , 34 , 3.0 , 12.3
布尔值: true , false
空值: null
变量: one,two,变量不能有空格
3 文本操作
字符串拼接: +
变量替换: |The name is ${name}|
4 数学运算
运算符: + , - , * , / , %
5 布尔运算
运算符: and , or
一元运算: ! , not
6 比较运算
比较: > , < , >= , <= ( gt , lt , ge , le )
等式: == , != ( eq , ne )
7 条件运算
f-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
8 特殊操作
误操作: _
3 设置属性值-th:attr
设置单个值
<form action="subscribe.html" th:attr="action=@{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/>
</fieldset>
</form>
设置多个值
<img src="../../images/ppt.png" th:attr="src=@{/images/ppt.png},title=#{logo},alt=#{logo}" />
上述代替写法 th:xxxx
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">
4 迭代
方式一
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Ons</td>
<td th:text="${prod.price}">2.55</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
方式二
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Ons</td>
<td th:text="${prod.price}">2.55</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
5 条件运算
方式一
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>
方式二
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
3 thymeleaf使用
1 引入Starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2 添加配置
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }
说明:
- 所有thymeleaf的配置值都在 ThymeleafProperties
- 配置好了 SpringTemplateEngine
- 配置好了 ThymeleafViewResolver
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html"; //xxx.html
3 页面开发
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}">哈哈</h1>
<h2>
<a href="www.baidu.com" th:href="${link}">去百度</a>
</h2>
</body>
</html>