12、视图解析器与模板引擎


【尚硅谷】SpringBoot2零基础入门教程-讲师:雷丰阳
笔记

路还在继续,梦还在期许

1、视图解析

视图解析:就是spring boot处理完请求,想要跳转到某个页面的过程,以前经常使用的方式有,转发或重定向到一个JSP页面,现在使用spring boot,因为spring boot打包方式是jar包,jar包是一个压缩包,JSP不支持在压缩包内编译的方式,所以spring boot默认不支持 JSP,想要使用页面渲染功能、页面跳转,需要引入第三方模板引擎技术实现页面渲染。

1.1 spring boot支持的第三方模板引擎技术

官网介绍

在场景启动器中,查看spring boot支持的模板引擎技术。

Name Description
spring-boot-starter-freemarker Starter for building MVC web applications using FreeMarker views
spring-boot-starter-groovy-templates Starter for building MVC web applications using Groovy Templates views
spring-boot-starter-thymeleaf Starter for building MVC web applications using Thymeleaf views

1.2、视图解析原理流程

在这里插入图片描述

  • 1、目标方法处理的过程中,所有数据都会被放在 ModelAndViewContainer 里面。包括数据和视图地址
  • 2、方法的参数是一个自定义类型对象(从请求参数中确定的),把他重新放在 ModelAndViewContainer
  • 3、任何目标方法执行完成以后都会返回 ModelAndView(数据和视图地址)。
  • 4、processDispatchResult 处理派发结果(页面改如何响应)
    • 1、render(mv, request, response); 进行页面渲染逻辑
      • 1、根据方法的String返回值得到 View 对象【定义了页面的渲染逻辑】
        • 1、所有的视图解析器尝试是否能根据当前返回值得到View对象
        • 2、得到了 redirect:/main.html --> Thymeleaf new RedirectView()
        • 3、ContentNegotiationViewResolver 里面包含了下面所有的视图解析器,内部还是利用下面所有视图解析器得到视图对象。
        • 4、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()—>

自定义视图解析器+自定义视图; 大厂学院。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

2、模板引擎-Thymeleaf

2.1、thymeleaf简介

thymeleaf官网

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.

现代化、服务端Java模板引擎

2.2、基本语法

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、特殊操作

无操作: _

2.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/gtvglogo.png"  th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值