学习资料网址  http://www.blogjava.net/bjwulin/archive/2013/02/07/395234.html (不做浮躁的人)博文
        http://www.blogjava.net/bjwulin/archive/2014/02/11/409734.html (不做浮躁的人)博文<与spring整合>
                   http://www.tuicool.com/articles/yYZbIrB 基本知识
                   http://itutorial.thymeleaf.org/  官方演示
                   http://www.cnblogs.com/vinphy/p/4673918.html 本文地址
 
springMVC + thymeleaf demo步骤:
1.建一个springMVC项目
2.加jar包
3.在web.xml中配置servlet
4.在配置的servlet.xml文件夹里配置thymeleaf相关配置
5.在cotroller中定义入口控制
6.将静态页面加入项目中,并添加thymeleaf标签
7.部署访问
 
--系统 Windows7
--开发工具 intelliJ idea
--项目管理工具 maven
--自动化构建工具 gradle
--模板 thymeleaf
--框架 springMVC

1.建一个springMVC项目
  用intelliJ 新建一个springMVC项目 参见http://note.youdao.com/share/?id=89349b4e4f6f57ae603c2c43bad1bb62&type=note
 
2.加jar包
    1)gradle加jar包

compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    2)jar包下载地址
 
3.在web.xml中配置servlet
  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!--配置WEB-INF下的servlet-context.xml文件-->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.在配置的servlet.xml文件夹里配置thymeleaf相关配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="com.test.thymeleaf.controller" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!--Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<!--springMVC+jsp的跳转页面配置-->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--<property name="prefix" value="/WEB-INF/views/" />-->
<!--<property name="suffix" value=".jsp" />-->
<!--</bean>-->
<!--springMVC+thymeleaf的跳转页面配置-->
<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</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" />
</bean>
</beans>
5.在cotroller中定义入口控制
package com.test.thymeleaf.controller;
import com.test.thymeleaf.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; @Controller
public class HomeController {
User user = new User();
//入口
@RequestMapping(value = "/home")
public String home(Model model) {
model.addAttribute("user",user);
return "aa";
}

  //提交表单后进行数据读取,并将数据传出
@RequestMapping(value = "/bb",method = RequestMethod.POST)
public String bb(User user,Model model) {
model.addAttribute("user", user);
model.addAttribute("message", ",welcome");
return "bb";
}
}
6.将静态页面加入项目中,并添加thymeleaf标签
  注意头文件
  

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
 
 aa.html(用th:object定义表单数据提交对象,用th:field定义表单数据属性,用*{}锁定上级定义的对象,{}内填写对象属性,提交表单时自动降属性值封住到对象中)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"> <head>
<title>Home</title>
</head>
<body>
<form th:action="@{/bb}" th:object="${user}" th:method="post"> <input type="text" th:field="*{name}"/>
<input type="text" th:field="*{msg}"/> <input type="submit"/>
</form> </body>
</html>

 bb.html(用${}读取后台传出的数据动态替换静态数据“vinphy,”和"welcome!")

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"> <head>
<meta charset="utf-8"/>
<title>Home</title>
</head>
<body>
<div>
<sapn th:text="${user.name}">vinphy,</sapn>
<sapn th:text="${message}">welcome!</sapn>
</div>
</body>
</html>

7.部署访问

部署后访问http://localhost:8080/home进行访问,出现aa.html的内容

  

Thymeleaf 学习笔记-实例demo(中文教程)的更多相关文章

  1. thymeleaf 学习笔记-基础篇(中文教程)

    (一)Thymeleaf 是个什么?      简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下 ...

  2. 学习笔记《简明python教程》

    学习笔记<简明python教程> 体会:言简意赅,很适合新手入门 2018年3月14日21:45:59 1.global 语句 在不使用 global 语句的情况下,不可能为一个定义于函数 ...

  3. CUBRID学习笔记 1 简介 cubrid教程

    CUBRID 是一个全面开源,且完全免费的关系数据库管理系统.CUBRID为高效执行Web应用进行了高度优化,特别是需要处理大数据量和高并发请求的复杂商务服务.通过提供独特的最优化特性,CUBRID可 ...

  4. 【Microsoft Azure学习之旅】消息服务Service Bus的学习笔记及Demo示例

    今年项目组做的是Cloud产品,有幸接触到了云计算的知识,也了解并使用了当今流行的云计算平台Amazon AWS与Microsoft Azure.我们的产品最初只部署在AWS平台上,现在产品决定同时支 ...

  5. 学习笔记之Python 3 教程

    Python 3 教程 http://www.runoob.com/python3/python3-tutorial.html Python的3.0版本,常被称为Python 3000,或简称Py3k ...

  6. Android(java)学习笔记212:中文乱码的问题处理(qq登录案例)

    1.我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed&q ...

  7. Android(java)学习笔记155:中文乱码的问题处理(qq登录案例)

    1. 我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed& ...

  8. 学习笔记:yaml语言教程

    目录 1.YAML基本概念 1.1 简介 1.2 基本语法 1.3 支持的数据结构: 1.4 注意点 2.数据结构 2.1 字典 2.2 数组 2.3 纯量 2.4 强制类型转换,双! 2.5 字符串 ...

  9. JavaScript学习笔记-实例详解-类(二)

    实例详解-类(二)   //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...

随机推荐

  1. iOS开发-模板方法模式

    模板方法模式定义一个操作中的算法的骨架,而将步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤.模板方法模式是由子类决定实现算法中的步骤,工厂方法由子类决定实现哪一 ...

  2. EditText自动换行显示内容

    默认的EditText是不会自动换行的,通过代码来实现: [java] view plain copy EditText editText = new EditText(this); //设置Edit ...

  3. EditText控件设置只读

    android的文本框输入控件(EditText),有时需要设置控件为只读,最简单的方法就是在layout xml文件中设置EditText的android:editable属性值为false即可,但 ...

  4. 启动vmware虚拟机报错:“无法获得VMCI驱动程序的版本:句柄无效”

    启动vmware虚拟机的时候报错: 找到对应虚拟机的文件夹路径: 用记事本打开CentOS 64位.vmx,找到这么一行: vmci0.present = "TRUE" 把TRUE ...

  5. Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  6. python3用BeautifulSoup用limit来获取指定数量的a标签

    # -*- coding:utf-8 -*- #python 2.7 #XiaoDeng #http://tieba.baidu.com/p/2460150866 #标签操作 from bs4 imp ...

  7. MongoDB(1)--简单介绍以及安装

    前段时间接触了NoSql类型的数据库redis,当时是作为缓存server使用的.那么从这篇博客開始学习还有一个非常出名的NoSql数据库:MongoDb.只是眼下还没有在开发其中使用.一步一步来吧. ...

  8. 使用ExpandableListView以及如何优化view的显示减少内存占用

    上篇博客讲到如何获取手机中所有歌曲的信息.本文就把上篇获取到的歌曲按照歌手名字分类.用一个ExpandableListView显示出来. MainActivity .java   public cla ...

  9. [svc]find+xargs/sed&sed后向引用+awk多匹配符+过滤行绝招总结&&产生随机数

    30天内的文件打包 find ./test_log -type f -mtime -30|xargs tar -cvf test_log.tar.gz find,文件+超过7天+超过1M的+按日期为文 ...

  10. android 监听动画对象后不能播放动画

    采用监听  AnimationListener 发现不能播放动画了. 解决办法: 将动画的启动方式:animation.startnow去掉,改为如下即可 view.startAnimation(an ...