【SpringMVC】8.REST風格的CRUD實戰(二)之查詢操作
阿新 • • 發佈:2018-12-16
##注意!!!
- URI:emps
- 請求方式:GET
- 顯示效果 所以我們就圍繞這個需求來進行程式設計。 ##二、具體步驟
###1.把Handler方法寫好
EmployeeHandler
相關程式碼
package com.springmvc.crud.handlers; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.springmvc.crud.dao.DepartmentDao; import com.springmvc.crud.dao.EmployeeDao; import com.springmvc.crud.entities.Employee; @Controller public class EmployeeHandler { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; @RequestMapping("/emps") public String list(Map<String, Object> map) { map.put("employees", employeeDao.getAll()); return "list"; } }
###2.list.jsp
相關程式碼
把從list
方法帶來的的employee
物件資料展示出來
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'list.jsp' starting page</title> </head> <body> <c:if test="${empty requestScope.employees }"> 沒有任何員工資訊! </c:if> <c:if test="${!empty requestScope.employees }"> <table border="1" cellpadding="10" cellspacing="0"> <tr> <th>ID</th> <th>LastName</th> <th>Email</th> <th>Gender</th> <th>Department</th> <th>Edit</th> <th>Delete</th> </tr> <c:forEach items="${requestScope.employees }" var="emp"> <tr> <td>${emp.id }</td> <td>${emp.lastName }</td> <td>${emp.email }</td> <td>${emp.gender == 0 ? 'Female':'Male' }</td> <td>${emp.department.departmentName }</td> <td><a href="emp/${emp.id }">Edit</a></td> <td><a class="delete" href="emp/${emp.id }">Delete</a></td> </tr> </c:forEach> </table> </c:if> <a href="emp">Add New Employee</a> </body> </html>