分享

thymeleaf模板的使用

 太极混元天尊 2018-04-13

  Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:

  • 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。

  • 2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果。

  • 3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

    说了这些优势是不是心动了 一起来写写代码吧。

1
简单搭建一个springmvc+thymeleaf的运行环境


  1. 创建一个maven项目,引入以下的依赖

   properties>
       project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
       maven.compiler.source>1.8maven.compiler.source>
       maven.compiler.target>1.8maven.compiler.target>  
   properties>
   dependencies>
       dependency>
           groupId>org.springframeworkgroupId>
           artifactId>spring-webmvcartifactId>
           version>4.2.4.RELEASEversion>
       dependency>
       dependency>
           groupId>javax.servletgroupId>
           artifactId>javax.servlet-apiartifactId>
           version>3.1.0version>
           scope>providedscope>
       dependency>
       dependency>
           groupId>org.thymeleafgroupId>
           artifactId>thymeleaf-spring4artifactId>
           version>2.1.4.RELEASEversion>
       dependency>
   dependencies>


2.web.xml配置dispaterServlet,加载mvc的配置文件,下面列出mvc的配置文件

context:component-scan base-package='com.dp.ssm.demo.controller'/>  
   
 mvc:annotation-driven/>  
 
   
 bean id='templateResolver'  
       class='org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver'>
 
   property name='prefix' value='/WEB-INF/views/' />  
   property name='suffix' value='.html' />  
   property name='templateMode' value='HTML5' />  
   property name='cacheable' value='false' />  
 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>

     这两部一个简单的springmvc+thymeleaf的简单架构就搭配好了,下面我们开始来玩玩呢thymeleaf的简单语法吧。

2
thymeleaf的基本使用过程


1)先创建一个简单的实体类,用于页面的数据遍历


 private Integer id;
 private String name;
 //set/get省略
 public User() {
 }
 public User(Integer id, String name) {
   this.id = id;
   this.name = name;
 }


2)建立一个controller,插入如下的代码,注意要在类上加controller注解


@RequestMapping('/')
 public String home(Model model) {
   List users = new ArrayList<>();
   users.add(new User(1, '张三'));
   users.add(new User(2, '李四'));
   users.add(new User(3, '王五'));
   model.addAttribute('users', users);
   return 'index';
 }


3)在WEB-INF目录下新建一个views文件夹,再见一个index.html的文件 注意不要忘记了引入xmlns:th='http://www.



html xmlns='http://www./1999/xhtml'
 xmlns:th='http://www.'>

head>
meta charset='utf-8' />
title>Hometitle>
head>
body>
 table>
   thead>
     tr>
       th>Idth>
       th>Nameth>
     tr>
   thead>
   tbody>
     tr th:each='user : ${users}'>
       td th:text='${user.id}'>Idtd>
       td th:text='${user.name}'>Nametd>
     tr>
   tbody>
 table>
body>
html>


4页面效果  th:text='${user.id} 就是用这个内容替换后面td里面的内容,如果在静态页面下 将会显示Id,name

    今天的文章就到这里啦,有兴趣的朋友可以先尝试一下thymeleaf的其他语法,下一篇给大家介绍.


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约