thymeleaf與restful風格結合的遍歷

html:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="../../webapp/js/thymeleaf.js"
	th:src="@{/js/thymeleaf.js}"></script>
	<script type="text/javascript" src="../../webapp/js/jquery.min.js"
	th:src="@{/js/jquery.min.js}"></script>
<script>
	
	$(function(){                     
 	   $(".delete").click(function(){
 		   alert("1");
 		   var href=$(this).attr("href");
 		   alert(href);
 		   $("#formdelete").attr("action",href).submit();
 		   return false;
 	   })
    }) 
</script>
</head>
<body>
	<div style="width: 500px; margin: 20px auto; text-align: center">
		<table align='center' border='1' cellspacing='0'>
			<tr>
				<td>id</td>
				<td>name</td>
				<td>編輯</td>
				<td>刪除</td>
			</tr>
			<tbody>
				<tr th:each="p: ${page}">
					<td th:text="${p.id}"></td>
					<td th:text="${p.name}"></td>
					<td><a th:href="@{/categories/{id}(id=${p.id})}">編輯</a></td>
					<td><a class="delete" th:href="@{/categories/{id}(id=${p.id})}">刪除</a></td>
				</tr>
			</tbody>
			<!-- <c:forEach items="${page}" var="c" varStatus="st">
				<tr>
					<td>${c.id}</td>
					<td>${c.name}</td>
					<td><a href="categories/${c.id}">編輯</a></td>
					<td><a class="delete" href="categories/${c.id}">刪除</a></td>
				</tr>
			</c:forEach> -->




		</table>
		<br> <br>
		<form action="categories" method="post">
			name: <input name="name"> <br>
			<button type="submit">提交</button>

		</form>

		<form id="formdelete" action="" method="POST">
			<input type="hidden" name="_method" value="DELETE">
		</form>

		<select size="3">
		
	</div>
</body>
</html>

controller:

package com.peng.demo.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.peng.demo.pojo.TbUser;
import com.peng.demo.service.UserService;
 
@Controller
public class UserController {
	
	@Reference
	private UserService userService;
	
	
	@GetMapping("/categories")
    public String listCategory(Model m) {
    	List<TbUser> page = userService.findAll();
    	m.addAttribute("page", page);
        return "listCategory";
    }
	
	@DeleteMapping("/categories/{id}")
	public String delete(TbUser thUser) {
    	System.out.println("=="+thUser.getId());
        return "redirect:/categories";
    }
	@GetMapping("/categories/{id}")
    public String getCategory(TbUser thUser,Model m) throws Exception {
		System.out.println("1111"+thUser.getId());

    	return "editCategory";
    }
 
}

注意:thymeleaf的遍歷:{}是代表參數,()代表參數的值。

<tbody>
    <tr th:each="user:${users}">
    <td th:text="${user.name}"></td>
    <td th:text="${user.email}"></td>
    <td><a th:href="@{/category/{id}(id=${user.id})}">修改</a></td>
    <td><a th:href="@{/category/{id}(id=${user.id})}">刪除</a></td>
     </tr>
</tbody>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章