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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM