javaee ssm框架项目整合thymeleaf2.0 更多thymeleaf标签用法 项目结构图

news2024/10/7 18:29:42

在这里插入图片描述

创建ssm+thymeleaf项目

创建ssm+thymeleaf项目参考此文

thymeleaf更多常用标签

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="top" th:fragment="topMenu" >
    导航条.......
</div>

</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${items.name}"></div>
<div th:text="${items.price}"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="static/css/test.css" th:href="@{/static/css/test.css}" rel="stylesheet" >
    <style>
        .even{ background-color: antiquewhite}

        .odd{background-color: pink}
    </style>
</head>
<body>

<div th:replace="index::topMenu">
    头部信息
</div>

<div th:text="${uname}" style="background-color: pink">这里是用来显示用户名的</div>
<input type="text" th:value="${uname}" value="默认值" />

<span th:if="${price > 5000}">精品</span>
<span th:unless="${price > 5000}">次品</span>

<table border="1" width="500">
    <tr th:each="items,itemsStat:${itemsList}"
    th:class="${itemsStat.odd}?'odd':'even'"
    >
        <th th:text="${items.id}"></th>
        <th  ><a th:href="@{/items/showItemsInfoById(id=${items.id})}" th:text="${items.name}"></a></th>
        <th  th:text="${items.price}"></th>
    </tr>
</table>



</body>
</html>
body{
    background-color: cadetblue;
}
<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 1. 配置  需要扫描的控制层在哪个包  -->
    <context:component-scan base-package="com.test.controller"></context:component-scan>

    <!-- 2 配置 视图解析器 中的 前缀和后缀  -->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
        <!--&lt;!&ndash; 设置前缀  &ndash;&gt;-->
        <!--<property name="prefix" value="/WEB-INF/"/>-->
        <!--&lt;!&ndash; 设置后缀 &ndash;&gt;-->
        <!--<property name="suffix" value=".jsp"/>-->
    <!--</bean>-->

    <!-- 3.配置的是thymeleaf模板 -->
    <!-- 使用thymeleaf解析 -->

    <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="HTML" />

        <property name="cacheable" value="false" />

        <property name="characterEncoding" value="UTF-8"/><!--不加会乱码-->

    </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>

    <!--放行静态资源 文件-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <mvc:resources mapping="/static/**" location="/static/" />

</beans>

概念

创建HTML

需要在html中添加:

<html xmlns:th="http://www.thymeleaf.org">

获取变量值${…}

 		<h1 th:text="${uname}"></h1>
  <input type="text" th:value="${uname}" />
<div th:object="${items}">
    <p th:text="*{name}" >产品名</p>
    <p th:text="*{detail}" >产品名</p>
</div>

至于p里面的原有的值只是为了给前端开发时做展示用的.这样的话很好的做到了前后端分离。

这也是Thymeleaf非常好的一个特性:在无网络的情况下也能运行,也就是完全可以前端先写出页面,模拟数据展现效果,后端人员再拿此模板修改即可!

链接表达式: @{…}

用来配合link src href使用的语法,类似的标签有:th:hrefth:src

链接表达式结构

无参:@{/xxx}

有参:@{/xxx(k1=v1,k2=v2)} 对应url结构:xxx?k1=v1&k2=v2
引入本地资源:@{/项目本地的资源路径}
引入外部资源:@{/webjars/资源在jar包中的路径}

<link th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<link th:href="@{/main/css/itdragon.css}" rel="stylesheet">
<form class="form-login" th:action="@{/user/login}" th:method="post" >
<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
<a href="details.html" th:href="@{/thymeleaf/showItemsById(id=${items.id})}">view</a>

${…}变量表达式

变量表达式有丰富的内置方法,使其更强大,更方便。

变量表达式功能

一、可以获取对象的属性和方法

二、可以使用ctx,vars,locale,request,response,session,servletContext内置对象

三、可以使用dates,numbers,strings,objects,arrays,lists,sets,maps等内置方法(重点介绍)

常用的内置对象

一、ctx :上下文对象。

二、vars :上下文变量。

三、locale:上下文的语言环境。

四、request:(仅在web上下文)的 HttpServletRequest 对象。

五、response:(仅在web上下文)的 HttpServletResponse 对象。

六、session:(仅在web上下文)的 HttpSession 对象。

七、servletContext:(仅在web上下文)的 ServletContext 对象

这里以常用的Session举例,用户刊登成功后,会把用户信息放在Session中,Thymeleaf通过内置对象将值从session中获取。

// java 代码将用户名放在session中
session.setAttribute("userinfo",username);
// Thymeleaf通过内置对象直接获取
th:text="${session.userinfo}"

常用的内置方法

一、strings:字符串格式化方法,常用的Java方法它都有。比如:equals,equalsIgnoreCase,length,trim,toUpperCase,toLowerCase,indexOf,substring,replace,startsWith,endsWith,contains,containsIgnoreCase等

二、numbers:数值格式化方法,常用的方法有:formatDecimal等

三、bools:布尔方法,常用的方法有:isTrue,isFalse等

四、arrays:数组方法,常用的方法有:toArray,length,isEmpty,contains,containsAll等

五、listssets:集合方法,常用的方法有:toList,size,isEmpty,contains,containsAll,sort等

六、maps:对象方法,常用的方法有:size,isEmpty,containsKey,containsValue等

七、dates:日期方法,常用的方法有:format,year,month,hour,createNow等

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>ITDragon Thymeleaf 内置方法</title>
</head>
<body>
    <h2>ITDragon Thymeleaf 内置方法</h2>
    <h3>#strings </h3>
    <div th:if="${not #strings.isEmpty(itdragonStr)}" >
        <p>Old Str : <span th:text="${itdragonStr}"/></p>
        <p>toUpperCase : <span th:text="${#strings.toUpperCase(itdragonStr)}"/></p>
        <p>toLowerCase : <span th:text="${#strings.toLowerCase(itdragonStr)}"/></p>
        <p>equals : <span th:text="${#strings.equals(itdragonStr, 'itdragonblog')}"/></p>
        <p>equalsIgnoreCase : <span th:text="${#strings.equalsIgnoreCase(itdragonStr, 'itdragonblog')}"/></p>
        <p>indexOf : <span th:text="${#strings.indexOf(itdragonStr, 'r')}"/></p>
        <p>substring : <span th:text="${#strings.substring(itdragonStr, 2, 8)}"/></p>
        <p>replace : <span th:text="${#strings.replace(itdragonStr, 'it', 'IT')}"/></p>
        <p>startsWith : <span th:text="${#strings.startsWith(itdragonStr, 'it')}"/></p>
        <p>contains : <span th:text="${#strings.contains(itdragonStr, 'IT')}"/></p>
    </div>

    <h3>#numbers </h3>
    <div>
        <p>formatDecimal 整数部分随意,小数点后保留两位,四舍五入: <span th:text="${#numbers.formatDecimal(itdragonNum, 0, 2)}"/></p>
        <p>formatDecimal 整数部分保留五位数,小数点后保留两位,四舍五入: <span th:text="${#numbers.formatDecimal(itdragonNum, 5, 2)}"/></p>
    </div>

    <h3>#bools </h3>
    <div th:if="${#bools.isTrue(itdragonBool)}">
        <p th:text="${itdragonBool}"></p>
    </div>

    <h3>#arrays </h3>
    <div th:if="${not #arrays.isEmpty(itdragonArray)}">
        <p>length : <span th:text="${#arrays.length(itdragonArray)}"/></p>
        <p>contains : <span th:text="${#arrays.contains(itdragonArray, 5)}"/></p>
        <p>containsAll : <span th:text="${#arrays.containsAll(itdragonArray, itdragonArray)}"/></p>
    </div>
    <h3>#lists </h3>
    <div th:if="${not #lists.isEmpty(itdragonList)}">
        <p>size : <span th:text="${#lists.size(itdragonList)}"/></p>
        <p>contains : <span th:text="${#lists.contains(itdragonList, 0)}"/></p>
        <p>sort : <span th:text="${#lists.sort(itdragonList)}"/></p>
    </div>
    <h3>#maps </h3>
    <div th:if="${not #maps.isEmpty(itdragonMap)}">
        <p>size : <span th:text="${#maps.size(itdragonMap)}"/></p>
        <p>containsKey : <span th:text="${#maps.containsKey(itdragonMap, 'thName')}"/></p>
        <p>containsValue : <span th:text="${#maps.containsValue(itdragonMap, '#maps')}"/></p>
    </div>
    <h3>#dates </h3>
    <div>
        <p>format : <span th:text="${#dates.format(itdragonDate)}"/></p>
        <p>custom format : <span th:text="${#dates.format(itdragonDate, 'yyyy-MM-dd HH:mm:ss')}"/></p>
        <p>day : <span th:text="${#dates.day(itdragonDate)}"/></p>
        <p>month : <span th:text="${#dates.month(itdragonDate)}"/></p>
        <p>monthName : <span th:text="${#dates.monthName(itdragonDate)}"/></p>
        <p>year : <span th:text="${#dates.year(itdragonDate)}"/></p>
        <p>dayOfWeekName : <span th:text="${#dates.dayOfWeekName(itdragonDate)}"/></p>
        <p>hour : <span th:text="${#dates.hour(itdragonDate)}"/></p>
        <p>minute : <span th:text="${#dates.minute(itdragonDate)}"/></p>
        <p>second : <span th:text="${#dates.second(itdragonDate)}"/></p>
        <p>createNow : <span th:text="${#dates.createNow()}"/></p>
    </div>
</body>
</html>

运算符

数学运算

  • 二元操作:+, - , * , / , %
  • 一元操作: - (负)

逻辑运算

  • 一元 : and or
  • 二元 : !,not

比较运算(为避免转义尴尬,可以使用括号中的英文进行比较运算!)

  • 比较:> , < , >= , <= ( gt , lt , ge , le )
  • 等于:== , != ( eq , ne )

条件运算

  • If-then: (if) ? (then)
  • If-then-else: (if) ? (then) : (else)
  • Default: (value) ?: (defaultvalue)

选择

if/unless

使用th:if和th:unless属性进行条件判断,th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。

 <td ><span th:if="${items.price gt 1000}" >精品</span></td>
 
 <td ><span th:unless="${items.price gt 1000}" >次品</span></td>

switch

<div th:switch="${items.name}">
  <p th:case="'aa'">aaaaaaaaa</p>
  <p th:case="'bb'">bbbbbbb</p>
  <p th:case="'cc'">cccccccc</p>
</div>

循环

th:each

thymeleaf的th:each常见用法
一.th:eath迭代集合用法:
<table border="1" id="stuTable">
    <tr>
        <td>是否选中</td>
        <td>编号</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    <tr th:each="stu,userStat:${studentList}" >
        <td><input th:type="checkbox" th:name="id" th:value="${stu.id}"></td>
        <td th:text="${stu.id}">编号</td>
        <td th:text="${stu.name}">姓名</td>
        <td th:text="${stu.age}">年龄</td>
    </tr>
</table>
二.迭代下标变量用法:
状态变量定义在一个th:每个属性和包含以下数据:

1.当前迭代索引,从0开始。这是索引属性。index

2.当前迭代索引,从1开始。这是统计属性。count

3.元素的总量迭代变量。这是大小属性。 size

4.iter变量为每个迭代。这是目前的财产。 current

5.是否当前迭代是奇数还是偶数。这些even/odd的布尔属性。

6.是否第一个当前迭代。这是first布尔属性。

7.是否最后一个当前迭代。这是last布尔属性。

用法实例:

<table border="1" id="stuTable">
    <tr>
        <td>是否选中</td>
        <td>编号</td>
        <td>姓名</td>
        <td>年龄</td>
    </tr>
    <tr th:each="stu,userStat:${studentList}" th:class="${userStat.odd}?'odd':'even'">
        <td th:text="${userStat.index}"></td>
        <td><input th:type="checkbox" th:name="id" th:value="${stu.id}"></td>
        <td th:text="${stu.id}">编号</td>
        <td th:text="${stu.name}">姓名</td>
        <td th:text="${stu.age}">年龄</td>
    </tr>
</table>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1065841.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

练[WUSTCTF2020]朴实无华

[WUSTCTF2020]朴实无华 文章目录 [WUSTCTF2020]朴实无华掌握知识解题思路代码分析 关键paylaod 掌握知识 ​ 目录扫描&#xff0c;抓包放包&#xff0c;代码审计&#xff0c;php函数特性的了解&#xff1a;intval函数&#xff0c;md5特性绕过&#xff0c;RCE一些bypass方法 解…

新代仿真软件的使用方法

1、安装 必须解压到C盘根目录下面 2、按键功能说明 3、新代仿真密码 密码&#xff1a;520 4、繁体改简体 3209号参数&#xff1a;值改成119&#xff0c;重启后即可正常识别中文。 5、修改坐标 先进入【程序编辑】-【图形仿真】-按【F10】键-【仿真参数设定】 把绘图模式改…

泛微OA e-office平台uploadify.php任意文件上传漏洞

泛微OA e-office平台uploadify.php任意文件上传漏洞复现 0x01 前言 免责声明&#xff1a;请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息或者工具而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者本人负责&#xff0c;所产生的…

横向AlGaN/GaN基SBD结构及物理模型数据库的开发

GaN基功率器件凭借其临界电场高、电子饱和漂移速度大、热导率高等优良性能在大功率快充、充电桩、新能源汽车等领域具备广泛应用空间。为进一步助推半导体高频、高功率微电子器件的发展进程&#xff0c;天津赛米卡尔科技有限公司技术团队依托先进的半导体TCAD仿真平台成功开发出…

给 Linux0.11 添加网络通信功能 (Day4: 完成 MIT6.S081 最终实验 网卡驱动(2. 启动 xv6 net 分支))

url: https://pdos.csail.mit.edu/6.S081/2020/labs/guidance.html lab guidance 介绍了调试技巧。 这种玩意儿可得好好看看啊&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01; 我们先把 xv6 跑起来吧&#xff0c;待会儿…

基于象群优化的BP神经网络(分类应用) - 附代码

基于象群优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码 文章目录 基于象群优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码1.鸢尾花iris数据介绍2.数据集整理3.象群优化BP神经网络3.1 BP神经网络参数设置3.2 象群算法应用 4.测试结果&#xff1a;5.M…

基于纵横交叉优化的BP神经网络(分类应用) - 附代码

基于纵横交叉优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码 文章目录 基于纵横交叉优化的BP神经网络&#xff08;分类应用&#xff09; - 附代码1.鸢尾花iris数据介绍2.数据集整理3.纵横交叉优化BP神经网络3.1 BP神经网络参数设置3.2 纵横交叉算法应用 4.测试结果…

css记录写一个奇怪的按钮

完成作业的时候发现一个很有意思的按钮&#xff0c;记录一下记录一下 看看界面 可以看出是一个奇形怪状的按钮&#xff0c;而且在按下的时候&#xff0c;图片和文字的颜色会改变 尝试解决 <!DOCTYPE html> <html lang"zh"> <head><meta chars…

Zabbix监控系统 第一部分:zabbix服务部署+自定义监控项+自动发现与自动注册(附详细部署实例)

这里是目录 一、Zabbix概述1.1 简介1.2 zabbix组件1.2.1 zabbix server1.2.2 zabbix agent1.2.3 zabbix proxy1.2.4 zabbix get1.2.5 zabbix sender 1.3 工作原理1.4 端口号1.5 zabbix中预设的键值1.6 自定义监控项1.7 邮件报警的思路1.8 Zabbix自动发现和自动注册1.8.1 zabbix…

从 Greenplum 到 YMatrix,某头部动力电池厂商核心业务数据的迁移实践

前言 随着数字化浪潮的不断深入&#xff0c;近年来企业对于数据库的功能诉求不断多元化&#xff0c;同时数据量大幅增长&#xff0c;包括 Greenplum 在内的许多原有的数据库技术应对起来日渐捉襟见肘&#xff0c;一些大型企业替换和升级数据库的需求愈发迫切。 本文将为大家完…

五.docker+jenkins自动部署项目

一.敏捷开发相关概念 1.微服务的痛点 再来看一下我们的微服务架构 &#xff0c; 每个组件都需要服务器去部署&#xff0c;加起来可能需要几十个甚至上百个服务器。这样的微服务项目在部署上会遇到什么问题&#xff1f; 需要很多很多的服务器&#xff0c;服务器的采购安装&am…

picodet onnx转其它芯片支持格式时遇到

文章目录 报错信息解决方法两模型精度对比 报错信息 报错信息为&#xff1a; Upsample(resize) Resize_0 not support attribute coordinate_transformation_mode:half_pixel. 解决方法 整个模型转换过程是&#xff1a;paddle 动态模型转成静态&#xff0c;再用paddle2onnx…

open62541交叉编译

好久没有做嵌入式Arm Linux 的开发了。最近要将open62541 的应用程序移植到i.mx6u 嵌入式控制器。网络上讲解i.mx6 交叉编译的文章太多了。但是都过于复杂&#xff0c;大多数使用虚拟机实现。其实在ubuntu OS 下&#xff0c;开发ARM 嵌入式应用软件相对是相当简单的。这里记录了…

日期相关工具类

日期相关工具类 【一】介绍【1】SimpleDateFormat 为什么是线程不安全【2】解决 SimpleDateFormat 线程不安全的方法 【二】LocalDate API【三】LocalTime API【四】LocalDateTime API【五】转换关系【1】LocalDateTime 与 LocalDate 之间的转换【2】LocalDateTime 与 Date 之间…

chrome浏览器如何多开

在网上寻找关于Chrome浏览器多开的教程时&#xff0c;你可能会发现操作相对复杂。然而&#xff0c;最近我发现了一个名为EasyBR浏览器的工具&#xff0c;作者使用程序将繁琐的步骤简化了。 主要功能 EasyBR浏览器具有以下主要功能&#xff1a; 批量账号管理&#xff1a;可以…

网站安全维护:守护您的数字领土

在这个数字时代&#xff0c;网站已成为企业和个人展示自己的重要平台。然而&#xff0c;随着互联网的高速发展&#xff0c;网站安全问题也日益严峻。黑客和入侵软件等威胁不断涌现&#xff0c;因此&#xff0c;保护网站免受这些威胁的影响变得至关重要。本文将探讨网站安全维护…

华为OD机试 - 5键键盘的输出(Java 2023 B卷 100分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷&#…

2023年中国短租公寓主要类型、品牌及行业市场规模分析[图]

短租是一种以24小时为计量单位、按天计费的房屋租赁形式&#xff0c;短租又称日租。短租房有高性价比、特色、浓厚居家感的特点&#xff0c;比起传统酒店的客房更具竞争优势。当前&#xff0c;短租房已经成为人们出行住宿的新选择。短租公寓主要类型有合租公寓、月租公寓、服务…

一个月软考信息安全工程师考前攻略!

一、考试报名时间 信安考试一年就一次&#xff0c;11月4日考试。千万别错过考试哦&#xff01;也别太紧张&#xff01; 二、考试科目设置 (1)网络信息安全基础知识和技术&#xff0c;考试时间为150分钟&#xff0c;笔试&#xff0c;选择题&#xff1b; (2)网络信息安全工程与…

UE5修改导航网格的参数

Unreal Engine 4 - Recast NavMesh Size, how to Change Agent Radius / Tutorial - YouTubehttps://www.youtube.com/watch?vf3hF6xdmCTk 修改当前的 代理半径就是一般贴边的长度 修改编辑器的