Spring MVC 如何使用Thymeleaf模板引擎

<!--thymeleaf模板解析器-->
    <bean id="SpringResourceTemplateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/"></property>
        <property name="suffix" value=".html"></property>
        <property name="templateMode" value="HTML5"></property>
        <property name="cacheable" value="false"></property>
        <property name="characterEncoding" value="UTF-8"></property>
    </bean>

    <bean id="SpringTemplateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
        <property name="templateResolver" ref="SpringResourceTemplateResolver"></property>
    </bean>

    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="SpringTemplateEngine"></property>
        <property name="characterEncoding" value="UTF-8"></property>
    </bean>

pom.xml 引入依赖

<dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring5</artifactId>
      <version>3.0.5.M3</version>
    </dependency>

    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>3.0.11.RELEASE</version>
    </dependency>

使用过程

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div class="footer" th:fragment="myfooter(footerName)">
        <p th:align="center" th:text="${footerName}"></p></div>
</body>
</html>
<!DOCTYPE html>
<html  lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script th:src="@{/resources/js/jquery-1.11.2.min.js}"></script>
    <link rel="stylesheet" th:href="@{/resources/css/style.css}">
</head>
<body>
<div th:replace="common/header::myheader(title='员工列表')"></div>
    <table>
        <thead>
            <tr>
                <td>编号</td>
                <td>姓名</td>
                <td>性别</td>
                <td>地址</td>
                <td>部门</td>
                <td>时间</td>
                <td>工资</td>
                <td>增加</td>
                <td>编辑</td>
                <td>查看</td>
                <td>删除</td>
            </tr>
        </thead>
        <tbody>
            <tr th:each="rows:${employees}">
                <td th:text="${rows.id}"></td>
                <td th:text="${rows.name}"></td>
                <td th:text="${rows.gender==1?'男':'女'}"></td>
                <td th:text="${rows.address}"></td>
                <td th:text="${rows.dept.name}"></td>
                <td th:text="${#dates.format(rows.addTime,'yyyy-MM-dd')}"></td>
                <td th:text="${#numbers.formatDecimal(rows.saraly, 0, 'COMMA', 2, 'POINT')}"></td>
                <td><a th:href="@{'/add/'}" th:text="增加"></a></td>
                <td><a th:href="@{'/edit/'+${rows.id}}" th:text="编辑"></a></td>
                <td><a th:href="@{'/show/'+${rows.id}}" th:text="查看"></a></td>
                <td><a th:text="删除"></a></td>
            </tr>
        </tbody>
    </table>
<div th:replace="common/footer::myfooter(footerName='版权所有:XXXXX公司')"></div>
</body>
</html>
<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>编辑</title>
    <script th:src="@{/resources/js/jquery-1.11.2.min.js}"></script>
    <link rel="stylesheet" th:href="@{/resources/css/style.css}">
    <script>

        function goback(){
            window.history.go(-1);
        }
    </script>
</head>
<body>

    <form th:action="${Employee!=null&& Employee.id>0}?@{/save}:@{/add}" method="post">
        <input type="hidden" th:if="${Employee!=null  && Employee.id>0}" th:value="${Employee.id}" name="id">
        <p>姓名: <input type="text" th:value="${Employee!=null}?${Employee.name}" name="name">
            <span th:if="${ab!=null}"  class="error" th:text="${ab.get('name')!=null}?${ab.name}"></span>
        </p>
        <p>性别: <label><input type="radio"  id="gender1" name="gender" th:checked="${Employee!=null}?${Employee.gender==1}:'true'" value="1">男</label>
                  <label><input type="radio"  id="gender2" name="gender" th:checked="${Employee!=null}?${Employee.gender==2}" value="2">女</label>
        </p>
        <p>地址: <input type="text" th:value="${Employee!=null}?${Employee.address}" name="address">
            <span th:if="${ab!=null}"  class="error" th:text="${ab.get('address')!=null}?${ab.address}"></span>
        </p>
        <p>部门:
            <select id="dept" name="dept.id">
                <option value="">--请选择--</option>
                <option th:selected="${Employee!=null}?${Employee.dept.id==dept.id}" th:each="dept:${department}" th:value="${dept.id}" th:text="${dept.name}"></option>
            </select>
        </p>
        <p>时间:
            <input type="text" th:value="${Employee!=null}?${#dates.format(Employee.addTime,'yyyy-MM-dd')}" name="addTime" id="addTime">
        </p>
        <p>
            <input type="submit" name="btnsubmit" th:value="${Employee!=null && Employee.id>0}?'更新':'添加'" id="btnsubmit">
            <input type="button" name="btnback" id="btnback" value="取消" th:onclick="goback()">
        </p>
    </form>

</body>
</html>

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script th:src="@{/resources/js/jquery-1.11.2.min.js}"></script>
    <script>

        function goback(){
            window.history.go(-1);
        }
    </script>
</head>
<body>
<form action="">
    <p>编号:<span th:text="${Employee.id}"></span></p>
    <p>姓名:<span th:text="${Employee.name}"></span></p>
    <p>性别:<span th:text="${Employee.gender==1?'男':'女'}"></span></p>
    <p>地址:<span th:text="${Employee.address}"></span></p>
    <p><input type="button" id="btnsubmit" th:value="返回" th:onclick="goback()"></p>
</form>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值