视图解析与模板引擎

本文详细介绍了Spring MVC中视图解析的过程,包括ModelAndViewContainer的使用、视图地址的处理和页面渲染。同时,深入探讨了Thymeleaf模板引擎的基本语法,如变量取值、条件运算、文本操作等,并展示了Thymeleaf在实际页面中的应用。此外,还提及了Spring Boot中Thymeleaf的自动配置和使用。
摘要由CSDN通过智能技术生成

视图解析与模板引擎

视图解析

1、目标方法处理的过程中,所有数据都会被放在 ModelAndViewContainer 里面。包括数据和视图地址
2、方法的参数是一个自定义类型对象(从请求参数中确定的),把他重新放在 ModelAndViewContainer
3、任何目标方法执行完成以后都会返回 ModelAndView(数据和视图地址)。
4、processDispatchResult 处理派发结果(页面改如何响应)

1、render(mv, request, response); 进行页面渲染逻辑
2、根据方法的String返回值得到 View 对象【定义了页面的渲染逻辑】
3、所有的视图解析器尝试是否能根据当前返回值得到View对象
4、得到了 redirect:/main.html –> Thymeleaf new RedirectView()
5、ContentNegotiationViewResolver 里面包含了下面所有的视图解析器,内部还是利用下面所有视图解析器得到视图对象。
6、view.render(mv.getModelInternal(), request, response); 视图对象调用自定义的render进行页面渲染工作
RedirectView 如何渲染【重定向到一个页面】
1、获取目标url地址
2、response.sendRedirect(encodedURL);
视图解析
返回值以 forward: 开始: new InternalResourceView(forwardUrl); –> 转发request.getRequestDispatcher(path).forward(request, response);
返回值以 redirect: 开始: new RedirectView() –》 render就是重定向
返回值是普通字符串: new ThymeleafView()—>

模板引擎-Thymeleaf

现代化、服务端Java模板引擎
1、基本语法

表达式名字语法用途
变量取值${…}获取请求域、session域、对象等值
选择变量*{…}获取上下文对象值
消息#{…}获取国际化等值
链接@{…}生成链接
片段表达式~{…}jsp:include 作用,引入公共页面片段

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、条件运算
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)

8、特殊操作
无操作: _

<tbody>
<tr th:each="emp:${emps}">
	<td th:text="${emp.id}"></td>
		<td th:text="${emp.lastName}"></td>
		<td th:text="${emp.email}"></td>
		<td th:text="${emp.gender}==0?'':''"></td>
		<td th:text="${emp.department.departmentName}"></td>
		<td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
		<td><a th:href="@{/emp/}+${emp.id}" class="btn btn-sm btn-primary">编辑</a>
		<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">
											删除</button>
	</td>
</tr>
</tbody>

<div th:replace="~{commons/bar::#sidebar(activeUri='emps')}"></div>
<h2><a th:href="@{/emp}"><button class="btn bth-sm btn-success">添加员工</button></a></h2>
<link th:href="@{/asserts/css/dashboard.css}" rel="stylesheet">

thymeleaf使用

引入starter

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

自动配置好了thymeleaf

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }



public static final String DEFAULT_PREFIX = "classpath:/templates/";

public static final String DEFAULT_SUFFIX = ".html";  //xxx.html

自动配好的策略

1、所有thymeleaf的配置值都在 ThymeleafProperties
2、配置好了 SpringTemplateEngine
3、配好了 ThymeleafViewResolver
4、我们只需要直接开发页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZSECode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值