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>

SpringBoot-视图解析与模板引擎相关推荐

  1. Springboot视图解析与模板引擎

    1.视图解析 视图解析:SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染. 视图解析原理流程 1.目标方法处理的过程中,所有数据都会被放在 ModelAndViewCon ...

  2. springboot视图解析器配置

    Springboot视图解析器配置 #spring.thymeleaf.cache = true #启用模板缓存. #spring.thymeleaf.check-template = true #在 ...

  3. 一头扎进springboot之使用Freemarker模板引擎渲染web视图

    在springboot的官方文档中是不建议在项目中使用jsp这样的技术的,取而代之的是freemark.velocity这样的模板引擎. 首先和大家来说一下这个模板引擎的概念,这里特指用于web开发的 ...

  4. 【SpringBoot】3、SpringBoot中整合Thymeleaf模板引擎

    SpringBoot 为我们提供了 Thymeleaf 自动化配置解决方案,所以我们在 SpringBoot 中使用 Thymeleaf 非常方便 一.简介 Thymeleaf是一个流行的模板引擎,该 ...

  5. springboot 整合 freemarker前端模板引擎实现数据展示

    一.freemaker 1.简介 FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户 ...

  6. springboot视图解析器

    视图解析器:简单来说,将用户请求转到对应页面 方式一:application.properties #配置视图解析器 spring.mvc.view.prefix=/WEB-INF/pages/ sp ...

  7. springboot项目使用beetl模板引擎生成word文件

    一.详情可查看官网 1.官方网址:beetl官网 一.简单测试beetl生成word文件 1.在springboot项目中添加beetl的依赖 <dependency><groupI ...

  8. springboot 设置ico_Spring Boot--Thymeleaf模板引擎/静态页面

    点关注,不迷路:持续更新Java相关技术及资讯!!! 1.Spring Boot对静态资源的映射规则 如果静态资源文件夹下有 index.html的话,直接访问localhost:8080的话,ind ...

  9. SpringBoot (二) 整合前端模板引擎FreeMarker、thymeleaf

    哈喽,大家好,我是有勇气的牛排(全网同名)

最新文章

  1. java多线程发布订阅,多线程实现发布订阅升级版---遗留问题
  2. 拖动去掉边框的JFrame
  3. html本地载入缓存文件,Flutter中如何加载并预览本地的html文件的方法
  4. Register code
  5. Gstreamer加载插件原理(二十五)
  6. 字节流Stream(Output 、Input)、字符流(Reader、Writer)
  7. 5. 用 PHP 源码操作 memcache(2)
  8. centos7安装便捷小工具命令
  9. JS学习之表格的排序
  10. mysql 系统工程师_数据库系统工程师难考吗?
  11. web前端开发面试题(五)
  12. ssm基于javaweb的医疗健康知识管理系统设计与实现毕业设计源码
  13. uefi装完系统后无法引导_uefi模式怎么重装系统|uefi重装系统教程
  14. 关于在MacBook上查找序列号你知道吗
  15. 五年企稳上升的阿里,还能再涨吗?
  16. mysql中关于表的删除和表中数据的删除
  17. SparkSQL函数定义——UDF函数,窗口函数
  18. 人生得意马蹄急,成长的痛,坚持痛并快乐
  19. SQL-建库、建表实例
  20. 51单片机之外设——74HC138、74HC02、74HC573讲解(一)

热门文章

  1. celery-04-操作b-执行人的一方
  2. dj鲜生-09-商品应用-首页的显示
  3. 巴西教育科技公司Arco Educacao上市 融资1.95亿美元
  4. ACM团队周赛题解(2)
  5. 潘在亮:给业务开发提供黑科技装备的“测试Q博士”
  6. 新手教程——在Linux Mint 16中找到保存的WiFi密码
  7. 令Django 视图有默认 login_required
  8. 利用dbms_metadata.get_ddl查看DDL语句
  9. Revit Family API 找到实体某一方向上的面。
  10. centOS 6.0无法运行dump指令的解决方法