Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置
使用Thymeleaf的屬性來設(shè)置HTML屬性。
(1)使用th:attr屬性可以修改原來HTML節(jié)點(diǎn)的屬性;
(2)th:attr屬性可以同時(shí)設(shè)置多個(gè)屬性;
(3)每一個(gè)HTML屬性都有對應(yīng)的Thymeleaf屬性,如th:attr="value='值'"可換為th:value="值"
(4)HTML的type為checkbox、readonly、required、disabled的,Thymeleaf屬性可寫為th:checked="true/false"形式;
(5)使用th:attrappend和th:attrprepend分別在HTML屬性的后面或前面加入數(shù)據(jù);
(6)使用th:styleappend和th:classappend分別向原有style、class屬性添加樣式;
(7)HTML5自定義屬性以“data-”作為前綴,Thymeleaf同樣支持自定義屬性,例如可以使用“data-th-text”代替 “th:text”,使用“data-th-each”代替“th:each”;
開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一個(gè)名稱為demo的Spring Boot項(xiàng)目。
1、pom.xml
加入Thymeleaf依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、src/main/java/com/example/demo/TestController.java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/")
public String test(){
return "test";
}
}
3、src/main/resources/templates/test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:id="form1" th:attr="method='post',action=@{/user/save}">
<input type="text" value="值1" th:value="值2" />
<input type="text" th:readonly="true" />
<input type="text" th:disabled="true" />
<input type="checkbox" th:checked="true" />
<input type="checkbox" th:checked="false" />
<div id="div1" th:attrappend="id='-data'" style="text-align: center;" th:styleappend="'color:#ccc'"></div>
<div id="div2" th:attrprepend="id='data-'" class="class1" th:classappend="class2"></div>
<input id="user" type="text" data-person-name="lc" data-age="30"/>
<div data-th-text="hello"></div>
<script>
var obj = document.getElementById("user");
//獲取HTML5屬性值的2種方式,用dataset方式時(shí),如果名稱帶連字符則使用時(shí)需駝峰化
var s = obj.dataset.personName + "," + obj.getAttribute("data-age");
alert(s);
</script>
</form>
</body>
</html>
瀏覽器訪問:http://localhost:8080
頁面彈出:lc,30
右鍵查看網(wǎng)頁源代碼,生成的HTML源碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form id="form1" method="post" action="/user/save">
<input type="text" value="值2" />
<input type="text" readonly="readonly" />
<input type="text" disabled="disabled" />
<input type="checkbox" checked="checked" />
<input type="checkbox" />
<div id="div1-data" style="text-align: center; color:#ccc"></div>
<div id="data-div2" class="class1 class2"></div>
<input id="user" type="text" data-person-name="lc" data-age="30"/>
<div>hello</div>
<script>
var obj = document.getElementById("user");
//獲取HTML5屬性值的2種方式,用dataset方式時(shí),如果名稱帶連字符則使用時(shí)需駝峰化
var s = obj.dataset.personName + "," + obj.getAttribute("data-age");
alert(s);
</script>
</form>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot與mybatis整合實(shí)例詳解(完美融合)
- 在SpringBoot下讀取自定義properties配置文件的方法
- Spring Boot 日志配置方法(超詳細(xì))
- SpringBoot + Spring Security 基本使用及個(gè)性化登錄配置詳解
- 解決springboot MultipartFile文件上傳遇到的問題
- springboot @ConditionalOnMissingBean注解的作用詳解
- springboot項(xiàng)目打成war包部署到tomcat遇到的一些問題
- springboot如何讀取配置文件(application.yml)中的屬性值
- 詳解eclipse下創(chuàng)建第一個(gè)spring boot項(xiàng)目
- Spring?Boot?4.0對于Java開發(fā)的影響和前景
相關(guān)文章
Java畢業(yè)設(shè)計(jì)之多用戶宿舍管理系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了基于Java實(shí)現(xiàn)的多用戶宿舍管理系統(tǒng),本文采用了jsp、servlet、jdbc等技術(shù),文中示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
spring cloud 之 客戶端負(fù)載均衡Ribbon深入理解
下面小編就為大家?guī)硪黄猻pring cloud 之 客戶端負(fù)載均衡Ribbon深入理解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Java 信號量Semaphore的實(shí)現(xiàn)
這篇文章主要介紹了Java 信號量Semaphore的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringBoot一個(gè)非常蛋疼的無法啟動(dòng)的問題解決
這篇文章主要介紹了SpringBoot一個(gè)非常蛋疼的無法啟動(dòng)的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

