参考网址: https://www.cnblogs.com/litblank/p/7988689.html

一、简介

1、Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应用,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

2、Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

3、Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际

化等功能。

二、目的

为了修改样式的时候不需要启动服务器。直接打开html。

三、解析器

Thymeleaf模板视图解析器配置步骤:模板解析器->模板引擎->视图解析器,注释掉的代码为个人JSP、Tiles视图解析器的测试代码,与本例无关。

四、SpringMVC+Thymeleaf

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springthymeleaf</groupId>
<artifactId>springmvc-thymeleaf</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Spring Thymeleaf Example</name>
<url>http://maven.apache.org</url> <properties>
<spring.version>4.1.3.RELEASE</spring.version>
<thymeleaf.version>2.1.2.RELEASE</thymeleaf.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency> --> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency> </dependencies> <build>
<outputDirectory>src/main/webapp/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build> </project>

Web.xml

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>springmvc thymeleaf</display-name> <servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

mvc-dispatcher-servlet.xml

注:可以深入了解thymeleef 模板解析器的源码,生命周期等

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.springthymeleaf"/> <mvc:annotation-driven /> <mvc:resources location="/static/" mapping="/static/**" /> <!-- 模板解析器 -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/>
</bean> <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean> <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean> </beans>

hello.html

 <html  xmlns:th="http://www.thymeleaf.org" >
<head>
<meta charset="UTF-8" />
<script type="text/javascript" src="static/js/jquery-1.10.2.min.js" th:src="@{/static/js/jquery-1.10.2.min.js}" ></script> <script th:inline="javascript">
$(function(){
var _ctx = [[${application.ctx}]];
alert("Project ContextPath:"+_ctx);
alert("路径:"+$("#ctx").val());
});
</script>
<title>Spring MVC + Thymeleaf Example</title>
</head>
<body>
<!-- Project ContextPath -->
<input type="hidden" id="ctx" th:value="${application.ctx}" /> Hello,
<span th:text="${name}" />!
<br /> Hello,
<span th:text="${query}" />!
<br /> Hello,
<span th:text="${submit}" />!
<br />
<a th:href="@{/query?name=a_href}"> query</a>
<br />
<form th:action="@{/submit}">
<input type="text" name="name" />
<button type="submit">submit</button>
</form> </body>
</html>

HelloController.java

package com.springthymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; @Controller
@RequestMapping("/")
public class HelloController { @RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getMovie(@PathVariable String name, ModelMap model) {
model.addAttribute("name", name);
model.addAttribute("query", "");
model.addAttribute("submit", "");
return "hello";
} @RequestMapping(value = "/query", method = RequestMethod.GET)
public String query(@RequestParam("name") String name, ModelMap model) {
model.addAttribute("name", "");
model.addAttribute("query", name);
model.addAttribute("submit", "");
return "hello";
} @RequestMapping(value = "/submit", method = RequestMethod.GET)
public String submit(@RequestParam("name") String name, ModelMap model) {
model.addAttribute("name", "");
model.addAttribute("query", "");
model.addAttribute("submit", name);
return "hello";
} }

ApplicationContext.java

package com.springthymeleaf;

import javax.servlet.ServletContext;

import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware; /**
* 将ContextPath写入application中,给静态文件引用时用、及URL链接地址用
*/
@Component
public class ApplicationContext implements ServletContextAware { @Override
public void setServletContext(ServletContext context) {
String ctx = context.getContextPath();
System.out.println("ctx=" + ctx);
context.setAttribute("ctx", ctx);
} }

五、参考链接:

thymeleaf 模板语言简介 http://blog.csdn.net/mlin_123/article/details/51816533

org.thymeleaf.spring4.templateresolver模板视图解析器 http://http://blog.csdn.net/mayi92/article/details/77720663

Spring MVC + Thymeleaf的更多相关文章

  1. Spring - MVC - thymeleaf 缓存关闭

    1. 概述 spring 配合 thymeleaf 关闭页面缓存 2. 背景 最近复习 spring 找了本书叫 spring in action 5th 本人水平有限 书还写得那么难 调试中遇到了问 ...

  2. A real ROCA using Bootstrap, jQuery, Thymeleaf, Spring HATEOAS and Spring MVC

    http://www.tuicool.com/articles/ENfe2u https://github.com/tobiasflohre/movie-database What is the be ...

  3. Spring MVC视图层:thymeleaf vs. JSP

    本文对比了同一Spring MVC工程中相同页面(一个订阅表单)分别采用Thymeleaf和JSP(包括JSP.JSTL.Spring tag lib)两种方式的实现. 本文的所有代码来自一个可运行的 ...

  4. Thymeleaf 3与Spring MVC 4 整合配置

    Thymeleaf 3与Spring MVC 4 整合配置 Maven 依赖配置 Spring 相关依赖就不说了 <dependency> <groupId>org.thyme ...

  5. Spring MVC 5 + Thymeleaf 基于Java配置和注解配置

    Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...

  6. 在Spring MVC和Spring Boot中使用thymeleaf模板

    Spring MVC: POM: <!-- thymeleaf模板 --> <!-- https://mvnrepository.com/artifact/org.thymeleaf ...

  7. Spring MVC : Java模板引擎 Thymeleaf (二)

    本文原计划直接介绍Thymeleaf的视图解析,但考虑到学习的方便,决定先构建一个spring-mvc. 以下的全部过程仅仅要一个记事本和JDK就够了. 第一步,使用maven构建一个web app. ...

  8. Spring Boot 2.X(三):使用 Spring MVC + MyBatis + Thymeleaf 开发 web 应用

    前言 Spring MVC 是构建在 Servlet API 上的原生框架,并从一开始就包含在 Spring 框架中.本文主要通过简述 Spring MVC 的架构及分析,并用 Spring Boot ...

  9. springboot Serving Web Content with Spring MVC

    Serving Web Content with Spring MVC This guide walks you through the process of creating a "hel ...

随机推荐

  1. mahout 实现canopy

    环境: mahout-0.8 hadoop-1.1.2 ubuntu-12.04 理论这里就不说了,直接上实例: 下面举一个例子. 数据准备: canopy.dat文件,COPY到HDFS上,文件内容 ...

  2. 我对PageRank的理解及R语言实现

    PageRank,网页排名,又称网页级别.Google左侧排名或佩奇排名,是一种由搜索引擎根据网页之间相互的超链接计算的技术,而作为网页排名的要素之一,以Google公司创办人拉里·佩奇(Larry ...

  3. 关于C/C++中的“auto”关键字

    C/C++ 98标准 C++03标准 早在C++98标准中就存在了auto关键字,那时的auto用于声明变量为自动变量,自动变量意为拥有自动的生命期.此用法是多余的,因为即使定义变量时不加" ...

  4. 27.【转载】挖洞技巧:如何绕过URL限制

    大家对URL任意跳转都肯定了解,也知道他的危害,这里我就不细说了,过~ 大家遇到的肯定都是很多基于这样的跳转格式:http://www.xxx.xxx/xxx?xxx=http://www.xxx.x ...

  5. oracle中文乱码解决方法笔记

    我刚刚在linux下安装oracle时就碰到这个问题,查了半天知道问题是oracle的字符集不对. 解决方法是dbca进入图形界面,删去现有的库,重建新库. 建库时注意: 1,dbca创建, 选第一项 ...

  6. hdu1053

    #include<iostream> #include<algorithm> using namespace std; bool cmp(int a,int b) { retu ...

  7. 基本图形的绘制(基于skimage)

    图形包括线条.圆形.椭圆形.多边形等.在skimage包中,绘制图形用的是draw模块,不要和绘制图像搞混了. 一  线条 函数调用格式:     skimage.draw.line(r1,c1,r2 ...

  8. 【leetcode 239. 滑动窗口最大值】解题报告

    思路:滑动窗口的思想,只要是求连续子序列或者子串问题,都可用滑动窗口的思想 方法一: vector<int> maxSlidingWindow(vector<int>& ...

  9. Educational Codeforces Round 53C(二分,思维|构造)

    #include<bits/stdc++.h>using namespace std;const int N=1e6+6;int x[N],y[N];int sx,sy,n;char s[ ...

  10. 洛谷P1164 小A点菜(01背包求方案数)

    P1164 小A点菜 题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过u ...