项目demo     http://pan.baidu.com/s/1wg6PC

学习资料网址  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(二)的更多相关文章

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

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

  2. Thymeleaf模板引擎的使用

    Thymeleaf模板引擎的使用 1.模板引擎 JSP.Velocity.Freemarker.Thymeleaf 2.springboot推荐使用Thymeleaf模板引擎 特点:语法更简单,功能更 ...

  3. Thymeleaf3语法详解和实战

    Thymeleaf3语法详解 Thymeleaf是Spring boot推荐使用的模版引擎,除此之外常见的还有Freemarker和Jsp.Jsp应该是我们最早接触的模版引擎.而Freemarker工 ...

  4. Thymeleaf3语法详解

    每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code   Thymeleaf是Spring boot推荐使用的模版引擎,除此之外常见的还有F ...

  5. vfd-cloud——一个适合练习上手的云存储网盘springboot项目(开发中)

    vfd-cloud           ​ 一个基于SpringBoot的云存储网盘项目,适合练手学习SpringBoot,用到的技术栈列到了下面.支持用户的注册登陆及修改密码,利用邮箱进行验证.支持 ...

  6. (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  7. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  8. (二)SpringBoot2.0基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

    一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...

  9. Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面

    Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面 在<Spring Boot(一):快速开始>中介绍了如何使用 Spring Boot 构建一个工程,并且提 ...

随机推荐

  1. MYSQL的联合查询最好是少用,效能差异巨大

    同样的功能,不同的写法,时间和内存占用差了几千倍,不废话,直接上代码 第一种写法: 代码如下: $Rs=DB::get($_ENV['DB'],3,"SELECT * FROM _xiazh ...

  2. C# Task WaitAll和WaitAny

    Task 有静态方法WaitAll和WaitAny,主要用于等待其他Task完成后做一些事情,先看看其实现部分吧: public class Task : IThreadPoolWorkItem, I ...

  3. [Linux] - SVN忽略文件夹更新的命令与方法

    在服务器上有时需要忽略某个文件夹及内容的更新,可以使用命令: svn update --set-depth=exclude FOLDER_NAME 比如需要忽略static目录: svn update ...

  4. JAVA中的ZoneId常用值备注

    一.获取代码 @Test public void zonesTest() { for (String availableZoneId : ZoneId.getAvailableZoneIds()) { ...

  5. M600 Pro 安装问题解决

    1.遥控器版本为1.2.10 提示版本已是最新版本,那么Lightbridge2 必须是1.1.60 不能是1.1.70 2.卸载机翼的时候,尽量用飞机带的那把工具 3.机翼安装 135 逆时针 cc ...

  6. Sql Server 增加字段、修改字段、修改类型、修改默认值(转)

    转:http://www.cnblogs.com/pangpanghuan/p/6432331.html Sql Server 增加字段.修改字段.修改类型.修改默认值 1.修改字段名: alter ...

  7. c# 基于redis分布式锁

    在单进程的系统中,当存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步,使其在修改这种变量时能够线性执行消除并发修改变量. 而同步的本质是通过锁来实现的.为了实现多个线程在 ...

  8. 【20180111】【物流FM专访】贝业新兄弟李济宏:我们是如何做到大件家居B2C物流第一的?

    在2017年的双11中,贝业新兄弟承接了日日顺家装和卫浴行业的仓储和配送,上海仓和武汉仓双十一期间及时出库率为100%,KPI位列第一:此外,贝业新兄弟还是科勒18年以来中国区唯一的物流服务商以及宜家 ...

  9. Intel Fortran 调用Delphi编制的DLL

    module link_cont interface subroutine I_FileOpenCont (ncase,ndata,lpool,xfiles) integer(kind=) :: nc ...

  10. AI习惯的数学书籍、计算机经典书籍

    http://download.csdn.net/download/wz619899442/8405297 https://www.amazon.com/Introduction-Automata-T ...