SpringBoot-视图解析与模板引擎

1、视图解析

视图解析:SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。
在这里插入图片描述

1.1、视图解析原理流程

  • 目标方法处理的过程中,所有数据都会被放在 ModelAndViewContainer 里面。包括数据和视图地址

  • 方法的参数是一个自定义类型对象(从请求参数中确定的),把把重新放在 ModelAndViewContainer

  • 任何目标方法执行完成以后都会返回 ModelAndView(数据和视图地址)

  • processDispatchResult 处理派发结果(页面如何响应)

  • render(mv, request, response); 进行页面渲染逻辑

  • 根据方法的String返回值得到 View 对象(定义了页面的渲染逻辑)

  • 所有的视图解析器尝试是否能根据当前返回值得到View对象

  • 得到了 redirect:/index.html --> Thymeleaf new RedirectView()

  • ContentNegotiationViewResolver 里面包含了下面所有的视图解析器,内部还是利用下面所有视图解析器得到视图对象。

  • 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

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.1 基本语法

1、设置属性值-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}" /

所有h5兼容的标签写法:添加链接描述

2、迭代

<tr th:each="prod : ${prods}">
        <td th:text="${prod.name}">Onions</td>
        <td th:text="${prod.price}">2.41</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}">Onions</td>
  <td th:text="${prod.price}">2.41</td>
  <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>

3、条件运算

<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>

2.2 thymeleaf的使用

1、引入Starter

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

2、自动配置thymeleaf

@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.atguigu.com" th:href="${link}">去百度</a>  <br/>
    <a href="www.atguigu.com" th:href="@{link}">去谷歌</a>
</h2>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值