美文网首页
thymeleaf的基本使用

thymeleaf的基本使用

作者: 李祥祥_19强化班 | 来源:发表于2020-08-10 13:57 被阅读0次

thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎

thymeleaf的特点

动静结合:
Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

开箱即用:
它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

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

与SpringBoot完美整合,SpringBoot提供了Thymeleaf的默认配置,并且为Thymeleaf设置了视图解析器,我们可以像以前操作jsp一样来操作Thymeleaf。代码几乎没有任何区别,就是在模板语法上有区别。

引自:https://www.jianshu.com/p/0b09eb61a3b8

thymeleaf 与 ssm整合

一、pom.xml中引入thymeleaf所需的依赖jar包

在原来Spring SpringMVC Mybatis框架的基础上引入thymeleaf-spring依赖,因为我的spring版本是5的版本所以这里引入了hymeleaf-spring5,具体可以去maven仓库网站(https://mvnrepository.com/
)自行搜索相应版本

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.11.RELEASE</version>
        </dependency>

因为引入了thymeleaf-spring5之后maven工具会自动下载以下相应依赖 所以此处不必再单独引入thymeleaf依赖

image

二、修改原来spring-mvc.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:jee="http://www.springframework.org/schema/jee"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/jee 
            http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

       <!-- 扫描Controller所在的包 -->
       <context:component-scan base-package="com.ways.app.common.web.controller"/>

       <!-- 注解驱动 -->
       <mvc:annotation-driven></mvc:annotation-driven>

        <!-- 视图解析器:简化在Controller类编写的视图路径 -->
        <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            前缀
            <property name="prefix" value="/WEB-INF/pages/"/>
            后缀
            <property name="suffix" value=".jsp"/>
        </bean> -->

        <!-- 模板解析器  -->
        <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
            <property name="prefix" value="/WEB-INF/templates/" />
            <property name="suffix" value=".html" />
            <property name="templateMode" value="HTML5" />
            <property name="cacheable" value="false" />
            <property name="characterEncoding" value="UTF-8"/>
        </bean>

        <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
            <property name="templateResolver" ref="templateResolver" />
        </bean>

        <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
            <property name="templateEngine" ref="templateEngine" />
            <property name="characterEncoding"  value="UTF-8" />
        </bean>        
</beans>

注释部分是原来配置jsp的部分
引用:作者:haiyong6 链接:https://www.jianshu.com/p/b791d7dc28f7 来源:简书

语法

相关文章

网友评论

      本文标题:thymeleaf的基本使用

      本文链接:https://www.haomeiwen.com/subject/kncldktx.html