Thymeleaf 迭代,if,switch语法
Posted qianzf
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thymeleaf 迭代,if,switch语法相关的知识,希望对你有一定的参考价值。
1、Thymeleaf 目标
- 迭代语法:th:each; iteration status
- 条件语法:th:if; th:unless
- switch语法:th:switch; th:case; *
2、公共模拟SpringBoot 后台接口
后台逻辑(action)
// 分页查询
@RequestMapping("/allColumnValidates")
public String getPage(Model model, @RequestParam(defaultValue = "1") int pageNo,
@RequestParam(defaultValue = "10") int pageSize) {
// 注意:分页代码PageHelper.startPage(pageNo,pageSize);只对其后的第一个查询有效。
PageHelper.startPage(pageNo, pageSize);
List<ColumnValidate> list = service.selectAll();
PageInfo pageInfo = new PageInfo(list);
model.addAttribute("pageInfo", pageInfo);
// 获得当前页
model.addAttribute("pageNum", pageInfo.getPageNum());
// 获得一页显示的条数
model.addAttribute("pageSize", pageInfo.getPageSize());
// 是否是第一页
model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
// 获得总页数
model.addAttribute("totalPages", pageInfo.getPages());
// 是否是最后一页
model.addAttribute("isLastPage", pageInfo.isIsLastPage());
return "validate/validate";
}
// 跳转至新增页面
@RequestMapping(value = "/insertPage", method = { RequestMethod.GET })
public String insertPage() {
return "validate/add";
}
// 插入
@RequestMapping(value = "/insert", method = { RequestMethod.POST })
public String insert(ColumnValidate entity) {
IdGenerator idWorker = new IdGenerator(0,0);
String sid = String.valueOf(idWorker.nextId());
entity.setSid(sid);
// 默认始化值
entity.setCreateDt(new Date());
entity.setDeleteFlag(GlobalConstant.DELETE_FLAG_NO);
entity.setState(GlobalConstant.ENABLE);
entity.setVersion(GlobalConstant.DEFAULT_VALUE);
service.insert(entity);
// 字段验证规则新增成功,调整至字段验证首页
return "redirect:/api/column-validate/allColumnValidates";
}
实体对象:
package com.zzg.entity;
import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import com.zzg.common.core.model.BaseEntity;
public class ColumnValidate extends BaseEntity {
private String sid;
private String createBy;
private Date createDt;
private String updateBy;
private Date updateDt;
private Integer version;
private String validateName;
private String validateContent;
private String state;
private String deleteFlag;
private static final long serialVersionUID = 1L;
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid == null ? null : sid.trim();
}
public String getCreateBy() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy = createBy == null ? null : createBy.trim();
}
@DateTimeFormat(pattern = "dd-MMM-YYYY")
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
@DateTimeFormat(pattern = "dd-MMM-YYYY")
public String getUpdateBy() {
return updateBy;
}
public void setUpdateBy(String updateBy) {
this.updateBy = updateBy == null ? null : updateBy.trim();
}
public Date getUpdateDt() {
return updateDt;
}
public void setUpdateDt(Date updateDt) {
this.updateDt = updateDt;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public String getValidateName() {
return validateName;
}
public void setValidateName(String validateName) {
this.validateName = validateName;
}
public String getValidateContent() {
return validateContent;
}
public void setValidateContent(String validateContent) {
this.validateContent = validateContent;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state == null ? null : state.trim();
}
public String getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(String deleteFlag) {
this.deleteFlag = deleteFlag == null ? null : deleteFlag.trim();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", sid=").append(sid);
sb.append(", createBy=").append(createBy);
sb.append(", createDt=").append(createDt);
sb.append(", updateBy=").append(updateBy);
sb.append(", updateDt=").append(updateDt);
sb.append(", version=").append(version);
sb.append(", validateName=").append(validateName);
sb.append(", validateContent=").append(validateContent);
sb.append(", state=").append(state);
sb.append(", deleteFlag=").append(deleteFlag);
sb.append("]");
return sb.toString();
}
}
3、Thymeleaf 迭代语法:
<!--Thymeleaf迭代语句each -->
<tr th:each="obj : ${pageInfo.list}">
<td th:text="${obj.validateName}"></td>
<td th:text="${obj.validateContent}"></td>
<td th:text="${obj.sid}"></td>
<td th:text="${obj.createBy}"></td>
<td th:text="${#dates.format(obj.createDt,‘yyyy-MM-dd‘)}"></td>
<td th:text="${obj.updateBy}"></td>
<td th:text="${#dates.format(obj.updateDt,‘yyyy-MM-dd‘)}"></td>
<!--Thymeleaf 判断语句:if 和 switch -->
<td th:if="${obj.state} == 1" th:text="启用"></td>
<td th:if="${obj.state} == 2" th:text="禁用"></td>
<td th:if="${obj.deleteFlag} == 1" th:text="未删除"></td>
<td th:if="${obj.deleteFlag} == 2" th:text="已删除"></td>
<td>
<a th:href="@{/api/column-validate/insertPage}" class="btn btn-info btn-sm">修改</a>
<a th:href="@{/api/column-validate/insertPage}" class="btn btn-info btn-sm">删除</a>
</td>
</tr>
Thymeleaf 判断语法:
<!--Thymeleaf 判断语句:if 和 switch -->
<td th:if="${obj.state} == 1" th:text="启用"></td>
<td th:if="${obj.state} == 2" th:text="禁用"></td>
<td th:if="${obj.deleteFlag} == 1" th:text="未删除"></td>
<td th:if="${obj.deleteFlag} == 2" th:text="已删除"></td>
Thymeleaf switch语法:
<!-- th:switch / th:case -->
<div th:switch="${user.name}">
<p th:case="‘admin‘">User is an administrator</p>
<!-- *: case的默认的选项 -->
<p th:case="*">User is some other thing</p>
</div>
以上是关于Thymeleaf 迭代,if,switch语法的主要内容,如果未能解决你的问题,请参考以下文章