Springboot+mybatis使用thymeleaf模板th空值问题

    xiaoxiao2024-12-30  78

    控制层返回的对象列表 pom引入依赖

    <!-- 引入thymelaf 则不需要引入web依赖,若不需要thymelaf则需要添加spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

    application.yml配置thymeleaf

    spring: thymeleaf: mode: HTML5 encoding: UTF-8 cache: false datasource: url: jdbc:mysql://localhost:3306/djks?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 hikari: connection-timeout: 60000 server: tomcat: uri-encoding: utf-8 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.team5408.entity configuration: map-underscore-to-camel-case: true call-setters-on-nulls: true

    static存放静态资源thmplates存放页面文件

    前端引用th标签

    <html xmlns:th="http://www.thymeleaf.org" xmlns:o="http://www.w3.org/1999/xhtml">

    在有可能出现空值的对象后面加上?避免空值导致查询结果不完整

    <table border="1"> <tr> <th>ID</th> <th>学号</th> <th>准考证号</th> <th>姓名</th> <th>报考类别</th> <th>场次</th> <th>开始时间</th> <th>机房号</th> <th>院系名称</th> <th>专业名称</th> <th>年级</th> <th>性别</th> <th>专业班级名称</th> <th>校区</th> <th>考场</th> <th>理论考试</th> </tr> <tr th:each="user : ${userList}"> <td th:text="${user.id}"></td> <td th:text="${user.no}"></td> <td th:text="${user.zkzh}"></td> <td th:text="${user.name}"></td> <td th:text="${user.bclass}"></td> <!-- 在有可能出现空值的对象后面加上?避免空值导致查询结果不完整--> <td th:text="${user?.ccc?.date}"></td> <td th:text="${user?.ccc?.starttime}"></td> <td th:text="${user.jfh}"></td> <td th:text="${user.yx}"></td> <td th:text="${user.zy}"></td> <td th:text="${user.nj}"></td> <td th:text="${user.sex}"></td> <td th:text="${user.sclass}"></td> <td th:text="${user.xiaoqu.xxq}"></td> <td th:text="${user.kc}"></td> <td th:text="${user.llks}"></td> </tr> </table>

    效果

    最新回复(0)