SpringMVC_14_RESTFUL_CRUD(三)實現查詢並且顯示所有資料
阿新 • • 發佈:2019-01-07
ok,上一章搞定了顯示所有的資料
這一章來搞定新增資料操作。先在list.jsp尾部新增一個標籤,也就是顯示了資料過後,可以新增資料的< a>標籤,新增完畢後重新顯示list.jsp操作。
一、編寫這個< a>標籤的handler方法
大家沒有忘記吧,所有的處理方法都在com.springmvc.crud.handlers包裡的EmployeeHandler類中處理
新增上處理這個< a>標籤請求的方法
@RequestMapping(value="emp",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("departments",departmentDao.getDepartments());//放入departments,方便頁面直接顯示部門名稱
map.put("employee",new Employee()); //放入一個空的Employee,這樣在之後的顯示不會報錯,利用率更高。記住就行
return "input";
}
不難發現,return 的是"input",很容易理解,需要一個新的jsp頁面來用一個表單實現註冊。
二、編寫這個input.jsp註冊介面
很容易想到,在/web/WEB-INF/views下建立這個檢視
input.jsp
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
< html>
<head>
<title>Title</title>
</head>
<body>
<!--
1.WHY使用form標籤呢?
可以更快速的開發出表單頁面,而且可以更方便的進行表單值的回顯
2.注意:
可以通過modelAttribute屬性指定繫結的模型屬性,
則預設為request域物件中讀取command的表單的bean.
如果該屬性值也不存在,則會發生錯誤。
-->
<form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee">
LastName: <form:input path="lastName"/>
<br>
Email:<form:input path="email"/>
<br>
<%
Map<String,String> genders = new HashMap<String, String>();
genders.put("1","Male");
genders.put("0","Female");
request.setAttribute("genders",genders);
%>
Gender:
<br>
<form:radiobuttons path="gender" items="${genders}" delimiter="<br>"/>
<br>
Department:<form:select path="department.id"
items="${departments}" itemLabel="departmentName" itemValue="id"></form:select>
<br>
<input type="submit" value="Submit"/>
</form:form>
</body>
</html>
tips:
1.WHY使用form標籤呢?
可以更快速的開發出表單頁面,而且可以更方便的進行表單值的回顯
2.注意:
可以通過modelAttribute屬性指定繫結的模型屬性,
則預設為request域物件中讀取command的表單的bean.
如果該屬性值也不存在,則會發生錯誤。
注意:這個表單提交的action是/emp,method的是POST實現新增操作,於是需要在handler中寫這個新的POST請求方法
三、處理新增POST請求
@RequestMapping(value = "/emp",method = RequestMethod.POST)
public String save(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
這個方法實現了把一個新的employee新增到資料庫中,並且使用了一個重定向redirect到我們的第一個方法/emps,實現顯示list.jsp
四、顯示效果
現在的EmployeeHandler.java
package com.springmvc.crud.handlers;
import com.springmvc.crud.dao.DepartmentDao;
import com.springmvc.crud.dao.EmployeeDao;
import com.springmvc.crud.entities.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
@RequestMapping(value = "/emp",method = RequestMethod.POST)
public String save(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
@RequestMapping(value="emp",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("departments",departmentDao.getDepartments());
map.put("employee",new Employee());
return "input";
}
@RequestMapping("/emps")
public String list(Map<String,Object> map){
map.put("employees",employeeDao.getAll());
return "list";
}
}