参考网址: 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

最新文章

  1. mount报错: you must specify the filesystem type
  2. tomcat详情
  3. Eclipse不给提示no default proposals
  4. Dynamic Invok Webservice
  5. android导入项目出错处理
  6. Jar包转成Dll的方式(带嵌套的jar也能做) (转)
  7. 数据库关于group by 两个或以上条件的分析
  8. oracle读写文件--利用utl_file包对磁盘文件的读写操作
  9. Java面试准备
  10. Java学习之类的构建方法(函数)
  11. mysql列约束
  12. Shell执行*.sql
  13. java发送soapui格式的报文
  14. 008 RestFul API 拦截器
  15. win10 store 无法连接网络(原创)
  16. 关于css的一些特别用法
  17. protocol buffer c#
  18. Xamarin.Android,Xamarin.iOS, Linking
  19. Oracle Data Integrator 12c----包(Package)
  20. windows下nodejs express安装及入门网站,视频资料,开源项目介绍

热门文章

  1. 基础篇---memcache
  2. java学习笔记——基于Robot类的屏幕分享
  3. 前端页面使用ace插件优化脚本
  4. oracle odbc连接sqlserver 无法查询字段
  5. 如何在html文件中导入header、footer等
  6. Apache服务器配置虚拟域名
  7. rabbitMq创建和获取消息
  8. 软件测试的生命周期&amp;测试流程
  9. VS文档自动生成
  10. LeetCode 136 Single Number 数组中除一个数外其他数都出现两次,找出只出现一次的数