项目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. ASP.NET WebApi 基于OAuth2.0实现Token签名认证

    一.课程介绍 明人不说暗话,跟着阿笨一起玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将是我们需要思考的问题.为了保护我们的WebApi数 ...

  2. WSL(Windows Subsystem for Linux)的安装与使用及 mongodb安装

    有关WSL的介绍这里就不做解释了.另外,本文仅适用于win10 build 16215以及之后的版本,之前的版本可参考官方链接. (可使用“winver”命令查看windows版本号) 安装:1.  ...

  3. C# SemaphoreSlim 实现

    当多个任务或线程并行运行时,难以避免的对某些有限的资源进行并发的访问.可以考虑使用信号量来进行这方面的控制(System.Threading.Semaphore)是表示一个Windows内核的信号量对 ...

  4. 上海线下技术交流(AA制)

    标签: 上海线下技术交流会 作者:王清培(Plen wang) 沪江Java资深架构师 .营销云平台负责人 上海地区技术线下交流,本次聚会AA制,要的就是热爱技术,交流技术,不是凑热闹.特此留念. 活 ...

  5. Elasticsearch集群运维

    一.索引管理 1. 创建索引 PUT test-2019-03 { "settings": { "index": { "number_of_shard ...

  6. 禅道docker

    64位电脑安装禅道,满足发送邮件功能 第一步: docker ps 查看docker中的容器是否有禅道(docker ps -a    这个指令看的是所有容器,包括未运行的)ps:登录服务器这个步骤没 ...

  7. 【ASP.NET Core】EF Core - “影子属性”

    有朋友说老周近来博客更新较慢,确实有些慢,因为有些 bug 要研究,另外就是老周把部分内容转到直播上面,所以写博客的内容减少了一点. 老周觉得,视频直播可能会好一些,虽然我的水平一般,不过直播时,老周 ...

  8. go微服务框架go-micro深度学习(三) Registry服务的注册和发现

    服务的注册与发现是微服务必不可少的功能,这样系统才能有更高的性能,更高的可用性.go-micro框架的服务发现有自己能用的接口Registry.只要实现这个接口就可以定制自己的服务注册和发现. go- ...

  9. Python import语句导入模块语法[转]

    Python import语句导入模块语法 社区推荐:掘金是国内最活跃的技术社区,我们每日有优质Python开发实例分享,海量python开源库推送.来掘金,和更多懂技术的小伙伴交流.   pytho ...

  10. C#设计模式 ---- 总结汇总

    一.引言 C#版本的23种设计模式已经写完了,现在也到了一个该总结的时候了.说起设计模式,我的话就比较多了.刚开始写代码的时候,有需求就写代码来解决需求,如果有新的需求,或者需求变了,我就想当然的修改 ...