th 表达式的简单使用。

一、使用场景,页面中,循环遍历,获得控制器穿过来的值。

1.1 控制器

    /**
     * 获得所有的图书信息
     * @return
     */
    @RequestMapping("/turnIndexPage")
    public String turnIndexPage(ModelMap modelMap){
        Map<String, Object> resultMap = bookService.selectAllBooks();
        if (200 == (Integer)resultMap.get("code")){
            List<Book> bookList = (List<Book>) resultMap.get("result");
            modelMap.addAttribute("bookList",bookList);
            return "index";
        }
        return  "404";
    }

1.2 HTML页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>首页</title>

</head>

<h1>欢迎登录图书管理系统</h1>

    <table  border="1px solid black" cellspacing="0" >
        <tr>
            <th>图书编号</th>
            <th>名称</th>
            <th>价格</th>
            <th>作者</th>
            <th>日期</th>
            <th>类型</th>
            <th>操作</th>
        </tr>

        <!--                                      1.1 book,起的名字       1.2   后台的值
              1. 写在th 标签内部   each 标签       th:each="book:          ${bookList}"
              2. 直接跳HTML页面,搞不定,跳到后台控制器,在让它跳转到指定的HTML 页面。
         -->
        <tr th:each="book:${bookList}">
            <td th:text="${book.id}"></td>
            <td th:text="${book.bookname}"></td>
            <td th:text="${book.price}"></td>
            <td th:text="${book.autor}"></td>
            <td th:text="${book.bookdate}"></td>
            <td th:text="${book.booktype}"></td>
            <td>
                <a th:href="@{deleteBook(id=${book.id})}">删除</a>
                <a th:href="@{updateBook(id=${book.id})}">修改</a>
                <a href="toAdd">录入</a>
            </td>
        </tr>

    </table>



</body>
</html>

1.3 跳到后台,再跳到HTML页面。

    /**
     *
     * 跳转到add 添加页面
     * @return
     */
    @RequestMapping("/toAdd")
    public  String toAdd(){
        return  "add";
    }
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>录入书籍</title>
</head>
<body>
<form method="post" action="insertBook">
    名称   <input type="text" name="bookname" ><br />
    价格  <input type="number" name="price" ><br />
    作者  <input type="text" name="autor"><br />
    类型 <input type="text" name="booktype" ><br />
    <input type="submit" value="提交">
</form>

</body>
</html>

二、页面取值。

2.1 控制器

    /**
     * 获得要修改的信息
     * @param book
     * @param modelMap
     * @return
     */
    @RequestMapping("/updateBook")
    public String updateBook(Book book,ModelMap modelMap){

        Map oneBook = bookService.getOneBook(book.getId());
        if (200 == (Integer) oneBook.get("code")){
            modelMap.addAttribute("book",oneBook.get("result"));
            return "update";
        }else {
            return "404";
        }
    }

2.2 页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="update">
    <input type="hidden" name="id" th:value="${book.id}"><br />
    名称   <input type="text" name="bookname" th:value="${book.bookname}"><br />
    价格  <input type="number" name="price" th:value="${book.price}"><br />
    作者  <input type="text" name="autor" th:value="${book.autor}"><br />
    类型 <input type="text" name="booktype" th:value="${book.booktype}"><br />
    <input type="submit" value="提交">
</form>

</body>
</html>
原文地址:https://www.cnblogs.com/ZXF6/p/11376179.html