第6章—渲染web视图—使用Thymeleaf
使用Thymeleaf
长期以来,jsp在视图领域有非常重要的地位,随着时间的变迁,出现了一位新的挑战者:Thymeleaf,Thymeleaf是原生的,不依赖于标签库.它能够在接受原始HTML的地方进行编辑和渲染.因为它没有与Servelet规范耦合,因此Thymeleaf模板能进入jsp所无法涉足的领域.现在我们来看下如何使用Thymeleaf!
1.引入pom依赖:
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf --><dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf-spring4</artifactId><version>3.0.9.RELEASE</version></dependency>
2.配置thymeleaf的视图解析器
在原有的SpringMVC的基础上修改我们的application.xml文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 开启注解. 扫描 --><context:annotation-config></context:annotation-config><context:component-scan base-package="controller"></context:component-scan><!-- 过滤掉js, jpg, png, css, 静态文件 --><mvc:default-servlet-handler/><!-- 开启mvc --><mvc:annotation-driven /><!-- 地址解析器 --><!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">--><!--<property name="prefix" value="/WEB-INF/views/"></property>--><!--<property name="suffix" value=".jsp"></property>--><!--</bean>--><!--viewResolver--><bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver"><property name="order" value="1"/><property name="characterEncoding" value="UTF-8"/><property name="templateEngine" ref="templateEngine"/></bean><!-- templateEngine --><bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine"><property name="templateResolver" ref="templateResolver"/></bean><bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"><property name="prefix" value="/WEB-INF/templates/"/><property name="suffix" value=".html"/><property name="templateMode" value="HTML5"/></bean>
</beans>
主要修改跳转的路劲和Thymeleaf相关的配置类
3.在WEB-INF下面建一个templates文件件,放入几个HTML
shouye.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h2>this is Thymeleaf</h2><br/><br/><a th:href="@{/thym/login}">go login</a><br/><br/>
<a th:href="@{/thym/register}">go register</a></body>
</html>
login.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>login page
</body>
</html>
register.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
register page
</body>
</html>
4.编辑Controller层的类文件
ThymController:
package controller;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/thym")
public class ThymController {@RequestMapping("/index")public String index(){return "shouye";}@RequestMapping("/login")public String login(){return "login";}@RequestMapping("register")public String register(){return "register";}
}
5.启动项目:路径:http://localhost:8080/thym/index
显示的页面如下:
我们可以点击链接文字进行相应的跳转,此时已经完成了一个Thymeleaf页面的编写.
转载于:https://www.cnblogs.com/charlypage/p/9393068.html
第6章—渲染web视图—使用Thymeleaf相关推荐
- 《Spring实战》读书笔记-第6章 渲染Web视图,java基础入门第二版pdf百度云
Spring提供了两种支持JSP视图的方式: InternalResourceViewResolver会将视图名解析为JSP文件.另外,如果在你的JSP页面中使用了JSP标准标签库(JavaServe ...
- spring mvc 渲染html,在Spring MVC中使用Thymeleaf模板渲染Web视图
Thymeleaf模板是原生的,不依赖于标签库.它能在接受原始HTML的地方进行编辑和渲染.由于没有与Servlet规范耦合,因此Thymeleaf模板能够进入JSP所无法涉及的领域 如果想要在Spr ...
- spring(6) 渲染web视图
[0]README 1)本文部分文字描述转自:"Spring In Action(中/英文版)",旨在review "spring(6) 渲染web视图" 的 ...
- 《Spring In Action(第4版)》阅读总结(四)渲染Web视图
渲染Web视图 将模型数据渲染为HTML 使用JSP视图 通过tiles定义视图布局 使用Thymealeaf视图 理解视图渲染 控制器只负责处理请求并返回模型数据和视图名 将控制器中请求处理逻辑与视 ...
- SpringBoot (三) :SpringBoot使用Freemarker模板引擎渲染web视图
什么是Freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而 ...
- 一头扎进springboot之使用Freemarker模板引擎渲染web视图
在springboot的官方文档中是不建议在项目中使用jsp这样的技术的,取而代之的是freemark.velocity这样的模板引擎. 首先和大家来说一下这个模板引擎的概念,这里特指用于web开发的 ...
- Spring Boot:(三)开发Web应用之Thymeleaf篇
Spring Boot:(三)开发Web应用之Thymeleaf篇 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot ...
- Spring Boot(19)---开发Web应用之Thymeleaf篇
Spring Boot(19)---开发Web应用之Thymeleaf篇 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring B ...
- 软件测试之第九章 搭建Web服务器
第九章 搭建 Web 服务器 一. Web 简介 1 网页 通过浏览器打开的任意一个页面(窗口). 本质上是服务器中的一个文件. 是使用如 html.asp.aspx.php.jsp.cgi 等语言编 ...
最新文章
- 多个文件中的数据处理,输出自己想要是数据
- java8 多个字段分组_MySQL基础之分组查询
- 如何将 Pycharm 打造得更称手
- 安卓学习 之 概述(一)
- IOS手机关于音乐自动播放问题的解决办法
- javascript mysql php_HTML、CSS、JavaScript、PHP、 MySQL 的学习顺序是什么?
- scoket多线程例子
- 窗口闪退_今天只讲一件事,精雕软件的BUG(进浮雕闪退,笔刷变圈圈等)
- HDR关键技术:色度学,颜色空间及转换
- 作为开发者你必须要知道的五大浏览器,记得收藏
- 团队管理_第一期干部训练营心得
- Spring Security定义多个过滤器链(10)
- 自动提取全部EXCEL sheet 名称
- 计算机专业如何自己增加项目经验?
- 中国黑客档案:识别黑客犯罪的蛛丝马迹
- 2.Linux下程序代码的编译
- linux下设置定时执行脚本
- 诛仙更新服务器正在维护,【正式服】3月7日例行更新维护公告
- tensorflow英语怎么读_tensorflow怎么读
- 坚果云 android 操作历史,#坚果云每日小技巧分享# 你也该学会使用文件历史版本了...