1. 程式人生 > >【SpringMVC】9.REST風格的CRUD實戰(三)之新增操作

【SpringMVC】9.REST風格的CRUD實戰(三)之新增操作

##注意!!!

  • URI:emp
  • 請求方式:GET
  • 顯示效果 這裡寫圖片描述

####新增員工資訊

  • URI:emp
  • 請求方式:POST
  • 顯示效果:完成新增,重定向到 list 頁面。

##二、介面分析

  1. 顯示頁面的URL都是emp,但是請求方式分別是GETPOST
  2. 頁面中有一個select按鈕(Department),這個必須從資料庫拿,所以需要經過handler類。
  3. 為了更快的開發頁面,需要使用springmvc標籤。
  4. 新增成功後,重定向到list頁面。
  5. Spring 的表單標籤:通過 SpringMVC 的表單標籤可以實現將模型資料 中的屬性和 HTML 表單元素相繫結,以實現表單 資料更便捷編輯和表單值的回顯。
  6. form 標籤:一般情況下,通過 GET 請求獲取表單頁面,而通過 POST 請求提交表單頁面,因此獲取表單頁面和提交表單 頁面的 URL 是相同的。只要滿足該最佳條件的契約,<form:form> 標籤就無需通過 action 屬性指定表單 提交的 URL可以通過 modelAttribute 屬性指定繫結的模型屬性,若沒有指定該屬性,則預設從 request域物件中讀取 command的表單 bean,如果該屬性值也不存在,則會 發生錯誤。

##三、具體步驟

###1.在EmployeeHandler把相關的Handler方法寫好 由於兩個全域性變數employeeDao

departmentDao在之前的文章已經有被建立了,在這裡重複寫是為了照顧沒有看之前文章的同學,方便理解這個變數是從哪來的,切記不要重複建立這兩個全域性變量了。

ackage 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(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";
	}
}

###2.input.jsp相關程式碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ 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 'input.jsp' starting page</title>

</head>

<body>
	<!-- 
  	為什麼使用form標籤呢?
  	1.因為可以更快速地開發出表單頁面,而且可以更方便地進行表單值的回顯
  	2.注意
  		可以通過modelAttribute 屬性指定繫結的模型屬性,
  		若沒有指定該屬性,則預設從request 域物件中讀取command的表單bean,
  		如果也不存在,則報錯java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute。
   -->
	<form:form action="${pageContext.request.contextPath }/emp" method="POST" modelAttribute="employee">
	<c:if test="${employee.id == null }">
		<!-- path屬性對應html表單標籤的name -->
  		LastName:<form:input path="lastName" />
  		<br>
	</c:if>
	<c:if test="${employee.id != null }">
		<form:hidden path="id"/>
		<input type="hidden" name="_method" value="PUT">
		<%--
		對於_method 不能使用form:hidden 標籤,因為ModelAttribute 對應的bean中沒有 _method 屬性--%>
		<%-- <form:hidden path="_method"/> --%> 
		<br>
	</c:if>
		
  	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:<form:radiobuttons path="gender" items="${genders }" />
		<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>

###3.在list.jsp中新增一個用於新增操作的超連結

<a href="emp">Add New Employee</a>

這裡寫圖片描述 點選超連結Add New Employee

這裡寫圖片描述

Department下拉框(select)能夠獲取全部的資料
能夠成功新增使用者

這裡寫圖片描述