Thymeleaf 学习笔记(demo)

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

学习资料网址  http://www.blogjava.net/bjwulin/archive/2013/02/07/395234.html (不做浮躁的人)博文

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包下载地址

http://www.thymeleaf.org/download.html 官网

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的内容

时间: 2024-07-30 01:43:21

Thymeleaf 学习笔记(demo)的相关文章

Thymeleaf 学习笔记-实例demo(中文教程)

项目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 

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

转自: http://www.cnblogs.com/vinphy/p/4674247.html (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果.这是由于它支持 html 原型,然后在 h

thymeleaf 学习笔记3

十.属性优先级 十一.注释 11.1  <!-- ... -->  同HTML/XML的注释 <!-- User info follows --> <div th:text="${...}"> ... </div> 11.2 thymeleaf解析器注释 thymeleaf解析的时候会被注释,静态打开时会显示 单行   <!--/*  ...   */--> <!--/* This code will be remove

thymeleaf 学习笔记

(一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果.这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略未定义的标签

thymeleaf 学习笔记(基础)

(一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果.这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略未定义的标签

thymeleaf 学习笔记4

十二. thymeleaf内联语法 内联:th:inline,值有三种:text,javascript,none 12.1 th:inline="text"文本内联 <p th:inline="text">Hello, [[${session.user.name}]]!</p> 11.2 th:inline="javascript"脚本内联 <script th:inline="javascript&quo

thymeleaf 学习笔记2

模板布局 8.1 包含模板片段(Including template fragments) 定义和引用片段 我们通常想要从别的模板文件中调用一些模板片段,例如 页面的头部.底部和菜单...等 th:fragment 定义一个文件 /WEBINF/templates/footer.html <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"> <html x

scala学习笔记-Demo存档

class Thermomenter{ var celsius :Float = _; //将变量设置为缺省值'_',这个符号指定了变量的初始化值 //对数值类型来说是0,布尔类型是false,引用类型是null //Scala中不能随便省略"=_"初始化器,若写成:var celsius:Float //将定义为抽象变量,而不是初始化变量. def fahrenheit = celsius*9/5+32; def fahrenheit_(f:Float)={ celsius = (f

cocos2dx学习笔记&mdash;&mdash;demo学习(一)&mdash;&mdash;ActionTest

         在此我会将一步步看到的小白我认为疑难和重点记下,并自解,若是在无法解答,便以红色标记出来,希望各位大神帮忙解惑. 一.onEnter()还有OnExit()是什么       因为小白在使用cocos2dx这款的时候,没有了解他,现在知道,他是在结点处便定义的一个接口,在每次场景开始的时候调用onEnter(),结束的时候调用onExit(),在这里可以进行资源的内存管理.       相应的还有onEnterTranstionDidFinish()以及onEixtTranst