欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Springboot Thymeleaf实现HTML属性设置

时间:2021-02-15 10:19:23|栏目:JAVA代码|点击:

使用Thymeleaf的属性来设置HTML属性。

(1)使用th:attr属性可以修改原来HTML节点的属性;

(2)th:attr属性可以同时设置多个属性;

(3)每一个HTML属性都有对应的Thymeleaf属性,如th:attr="value='值'"可换为th:value="值"

(4)HTML的type为checkbox、readonly、required、disabled的,Thymeleaf属性可写为th:checked="true/false"形式;

(5)使用th:attrappend和th:attrprepend分别在HTML属性的后面或前面加入数据;

(6)使用th:styleappend和th:classappend分别向原有style、class属性添加样式;

(7)HTML5自定义属性以“data-”作为前缀,Thymeleaf同样支持自定义属性,例如可以使用“data-th-text”代替 “th:text”,使用“data-th-each”代替“th:each”;

开发环境:IntelliJ IDEA 2019.2.2

Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

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方式时,如果名称带连字符则使用时需驼峰化
  var s = obj.dataset.personName + "," + obj.getAttribute("data-age");
  alert(s);
 </script>

</form>

</body>
</html>

浏览器访问:http://localhost:8080

页面弹出:lc,30

右键查看网页源代码,生成的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方式时,如果名称带连字符则使用时需驼峰化
  var s = obj.dataset.personName + "," + obj.getAttribute("data-age");
  alert(s);
 </script>

</form>

</body>
</html>

上一篇:linux配置java环境变量详细过程

栏    目:JAVA代码

下一篇:springboot 配置DRUID数据源的方法实例分析

本文标题:Springboot Thymeleaf实现HTML属性设置

本文地址:http://www.codeinn.net/misctech/63132.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有