thymeleaf语法总结

文件参考:https://www.jianshu.com/p/f79a98173677

1.基本表达式

1.1:变量表达式${}

<span th:text="${information}"></span>

1.2:选择变量表达式*{}:一般配合@ModelAttribute使用,注解用法

//java的controller
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}

//thymeleaf的html页面
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...
<input type="text" value="" th:field="*{username}"></input>
<input type="text" value="" th:field="*{user[0].username}"></input>
</form>

1.3:信息表达式#{}

<p th: text=" #{home. welcome}" >This text will not be show! </p>

home.welcome=this messages is from home.properties!

1.4:链接URL表达式@{}

<link th:src="@{/resources/css/bootstrap.min.css}" />

<li><a th:href="@{/forIndex}">首页</a></li>

1.5:工具类表达式 ${#map} (详情参考

<div th:if="${#maps.size(stu)!=0}">
...do something...
</div>

2.常用属性

th:action      表单提交的地址              <form action="subscribe.html" th:action="@{/subscribe}">
th:each        属性赋值                <tr th:each="user,userStat:${users}">
th:field       常用于表单字段绑定             <input type="text" value="" th:field="*{username}"></input>
th:href        链接地址                <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:id         替换id                    <input th:id="'xxx' + ${collect.id}"/>
th:if          判断条件                   <a th:if="${userId == collect.userId}" >
th:include     布局标签,替换内容到引入的文件     <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:fragment    布局标签,定义代码片段,其它引用   <div th:fragment="alert">
th:object      替换对象                     <div th:object="${session.user}">
th:src         图片类地址引入                 ![](@{/img/logo.png})
th:replace     布局标签,替换整个标签到引入的文件  <div th:replace="fragments/header :: title"></div>
th:text       文本替换                      <p th:text="${collect.description}">description</p>
th:value       属性赋值                      <input th:value="${user.name}" />
th:inline     js脚本中可以使用thymeleaf变量           <script type="text/javascript" th:inline="javascript">
th:remove      删除某个属性                 <tr th:remove="all"> 
th:style       设置样式                  th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick     点击事件                   th:onclick="'getCollect()'"

3.标准表达式

3.1:普通字面

<span th:text="'working web application'">template file</span>	//working web application
<div th:if="${user.isAdmin()} == false">						//true
<div th:if="${variable.something} == null">  					//null

3.2:文字操作

<span th:text="'Welcome to our application, ' + ${user.name} + '!'"> //Welcome to our application,liming
<span th:text="|Welcome to our application, ${user.name}!|">//Welcome to our application,liming

3.3:算数运算
3.3.1:二元运算 +,-,*,/,%

<span th:text="1+1">1+1</span>

3.3.2:布尔运算 and ,or, !, not

<span  th:if="${!#lists.isEmpty(list)} and ${#lists.isEmpty(list)}" >and</span>
<span  th:if="${!#lists.isEmpty(list)} or ${#lists.isEmpty(list)}" >or</span>
<span  th:if="${!#lists.isEmpty(list)}">not</span>

3.3.3:比较运算 (>,<,<=,>=,==,!=(gt,lt,le,ge,eq,ne))

<ol>   
<li>>(gt)<span th:text="1+1" th:if="${#lists.size(list)} > 1">大于></span>else</li>   
<li>小于lt:<span th:if="${#lists.size(list)} lt 1">小于</span>else</li>   
<li>>=(ge)<span  th:if="${#lists.size(list)} >= 1">大于等于>=</span>else</li>   
<li>小于等于(le)<span  th:if="${#lists.size(list)} le 1">小于等于</span>else</li>   
<li>!(not)<span  th:if="${!#lists.isEmpty(list)}">!(not)</span>else</li>   
<li>==(eq)<span th:text="'Execution mode is ' + ( (${execMode} == 'dev')? 'Development' : 'Production')">等于==</span></li>   
<li>!=(ne/neq):size:<span th:text="${#lists.size(list)}" th:if="${#lists.size(list)} != 1"></span></li>
</ol>

3.3.4:条件运算 if ?then , if? then:else , if ? :default

<span th:class="${title1} ? 'green'">样例</span>
<span th:class="${title} ? 'green' :' red'">样例一</span>
 <span th:text="*{age}?: '(no age specified)'">20</span>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot是一个开源的Java框架,它简化了基于Spring框架的Java应用程序的开发过程。Thymeleaf是一个用于构建Web应用程序的Java模板引擎,它可以与Spring Boot框架无缝集成。下面将使用Spring Boot和Thymeleaf完成增删改查的操作。 首先,我们需要创建一个Spring Boot项目并添加Thymeleaf依赖。在pom.xml文件中添加以下依赖项: ```xml <dependencies> <!-- 添加Thymeleaf依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 接下来,我们需要创建一个实体类来代表数据库中的表。例如,我们创建一个名为User的实体类,包含id、name和age字段。 ```java public class User { private long id; private String name; private int age; // 添加getter和setter方法 // ... } ``` 然后,我们需要创建一个Controller类来处理HTTP请求。在Controller类中,我们将使用Thymeleaf模板引擎来渲染视图并处理用户输入。 ```java @Controller public class UserController { // 假设我们已经拥有一个User的数据访问层接口UserRepository @Autowired private UserRepository userRepository; // 处理GET请求,显示用户列表 @GetMapping("/users") public String listUsers(Model model) { List<User> users = userRepository.findAll(); model.addAttribute("users", users); return "userList"; } // 处理GET请求,显示创建用户表单 @GetMapping("/users/create") public String createUserForm(Model model) { model.addAttribute("user", new User()); return "userForm"; } // 处理POST请求,创建用户 @PostMapping("/users/create") public String createUser(User user) { userRepository.save(user); return "redirect:/users"; } // 处理GET请求,显示更新用户表单 @GetMapping("/users/{id}/update") public String updateUserForm(@PathVariable("id") long id, Model model) { User user = userRepository.findById(id); model.addAttribute("user", user); return "userForm"; } // 处理POST请求,更新用户 @PostMapping("/users/{id}/update") public String updateUser(@PathVariable("id") long id, User updatedUser) { User user = userRepository.findById(id); user.setName(updatedUser.getName()); user.setAge(updatedUser.getAge()); userRepository.save(user); return "redirect:/users"; } // 处理GET请求,删除用户 @GetMapping("/users/{id}/delete") public String deleteUser(@PathVariable("id") long id) { userRepository.delete(id); return "redirect:/users"; } } ``` 在上述Controller类中,我们使用了`@GetMapping`和`@PostMapping`注解来处理HTTP请求,并使用Thymeleaf模板引擎渲染视图。在listUsers方法中,我们通过Model对象将用户列表传递给视图。在createUser和updateUser方法中,我们使用userRepository将用户保存到数据库中,并在需要时进行更新。在deleteUser方法中,我们使用userRepository根据id删除用户。 最后,我们需要创建Thymeleaf模板来渲染视图。我们可以创建一个名为userList.html和userForm.html的模板文件,来分别显示用户列表和创建/更新用户的表单。在模板文件中,我们可以使用Thymeleaf提供的语法来动态显示数据和处理用户输入。 综上所述,使用Spring Boot和Thymeleaf可以很方便地完成增删改查的操作。通过创建实体类、编写Controller类、编写Thymeleaf模板,我们可以完成对数据库的操作,并将结果显示在视图中,从而实现简单的增删改查功能。 ### 回答2: Spring Boot是一个基于Java的开发框架,Thymeleaf是Spring Boot中一种模板引擎,用于前端页面的渲染和展示。使用Spring Boot和Thymeleaf可以很方便地完成增删改查操作。 首先,需要在pom.xml文件中引入Thymeleaf的依赖项,确保项目中已经导入了相关的JAR包。 在Spring Boot中,可以使用@Controller注解来定义控制器,使用@RequestMapping注解来定义请求映射。 例如,我们可以创建一个UserController类来处理用户的增删改查请求: ```java @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public String userList(Model model) { List<User> userList = userService.getUserList(); model.addAttribute("users", userList); return "userList"; } @GetMapping("/add") public String addUserForm(Model model) { model.addAttribute("user", new User()); return "addUser"; } @PostMapping("/add") public String addUserSubmit(@ModelAttribute User user) { userService.addUser(user); return "redirect:/user/list"; } @GetMapping("/edit/{id}") public String editUserForm(@PathVariable("id") Integer id, Model model) { User user = userService.getUser(id); model.addAttribute("user", user); return "editUser"; } @PostMapping("/edit/{id}") public String editUserSubmit(@PathVariable("id") Integer id, @ModelAttribute User user) { userService.updateUser(id, user); return "redirect:/user/list"; } @GetMapping("/delete/{id}") public String deleteUser(@PathVariable("id") Integer id) { userService.deleteUser(id); return "redirect:/user/list"; } } ``` 其中,@GetMapping注解用于处理GET请求,@PostMapping注解用于处理POST请求,@PathVariable注解用于获取路径中的参数。 在Thymeleaf模板中,可以使用Thymeleaf语法来渲染动态数据。例如,可以通过th:each来遍历用户列表,使用th:text来显示用户的属性值,使用th:href来指定链接的目标。 以下是一个使用Thymeleaf的模板示例: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>User List</title> </head> <body> <table> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Actions</th> </tr> <tr th:each="user : ${users}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.email}"></td> <td> <a th:href="@{/user/edit/{id}(id=${user.id})}">Edit</a> <a th:href="@{/user/delete/{id}(id=${user.id})}">Delete</a> </td> </tr> </table> </body> </html> ``` 以上是一个使用Spring Boot和Thymeleaf完成增删改查的简单示例,具体的实现方式可能会根据具体需求而略有差异。希望对您有所帮助! ### 回答3: Spring Boot是一种用于构建Java应用程序的开源框架,而Thymeleaf是一种用于构建Web页面的Java模板引擎。结合使用Spring Boot和Thymeleaf可以方便地完成增删改查操作。 对于增加操作,我们可以通过在控制器类中添加@RequestMapping注解的方法来处理用户发送的请求。在该方法中,我们可以使用@RequestParam注解来获取用户输入的数据,并将数据保存到数据库中。 对于删除操作,我们可以通过在控制器类中添加@RequestMapping注解的方法来处理用户发送的请求。在该方法中,我们可以使用@RequestParam注解来获取要删除的数据的唯一标识,并从数据库中删除相应的数据。 对于修改操作,我们可以通过在控制器类中添加@RequestMapping注解的方法来处理用户发送的请求。在该方法中,我们可以使用@RequestParam注解来获取用户想要修改的数据,并将修改后的数据保存到数据库中。 对于查询操作,我们可以通过在控制器类中添加@RequestMapping注解的方法来处理用户发送的请求。在该方法中,我们可以使用@RequestParam注解来获取用户想要查询的条件,并从数据库中查询符合条件的数据。 在以上的操作中,我们可以使用Thymeleaf语法来渲染并展示数据库中的数据。可以通过在HTML模板中使用Thymeleaf的标签和表达式来动态地展示数据,例如使用th:each来循环展示查询到的数据列表,使用th:text来显示数据的具体值等。 总结来说,结合Spring Boot和Thymeleaf可以方便地完成增删改查操作。通过在控制器类中添加适当的方法和使用Thymeleaf语法,可以实现数据的添加、删除、修改和查询,并将处理结果通过页面展示给用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值