Spring MVC框架:第七章:REST架構風格

第一節(jié) REST簡介
1.概念
Representational State Transfer——表現(xiàn)層(資源)狀態(tài)轉化。是目前最流行的一種互聯(lián)網(wǎng)軟件架構風格。它倡導結構清晰、符合標準、易于理解、擴展方便的Web架構體系,主張嚴格按照HTTP協(xié)議中定義的規(guī)范設計結構嚴謹?shù)腤eb應用架構體系。由于REST所倡導的理念讓Web應用更易于開發(fā)和維護,更加優(yōu)雅簡潔,所以正得到越來越多網(wǎng)站的采用。
資源(Resources):網(wǎng)絡上的一個實體,或者說是網(wǎng)絡上的一個具體信息。它可以是一段文本、一張圖片、一首歌曲、一種服務,總之就是一個具體的存在??梢杂靡粋€URI(統(tǒng)一資源定位符)指向它,每種資源對應一個特定的 URI 。要獲取這個資源,訪問它的URI就可以,因此 URI 即為每一個資源的獨一無二的識別符。

表現(xiàn)層(Representation):把資源具體呈現(xiàn)出來的形式,叫做它的表現(xiàn)層(Representation)。比如,文本可以用txt格式表現(xiàn),也可以用HTML格式、XML格式、JSON格式表現(xiàn),甚至可以采用二進制格式。

狀態(tài)轉化(State Transfer):每發(fā)出一個請求,就代表了客戶端和服務器的一次交互過程。HTTP協(xié)議,是一個無狀態(tài)協(xié)議,即所有的狀態(tài)都保存在服務器端。因此,如果客戶端想要操作服務器,必須通過某種手段,讓服務器端發(fā)生“狀態(tài)轉化”(State Transfer)。而這種轉化是建立在表現(xiàn)層之上的,所以就是 “表現(xiàn)層狀態(tài)轉化”。具體說,就是 HTTP 協(xié)議里面,四個表示操作方式的動詞:GET、POST、PUT、DELETE。它們分別對應四種基本操作:GET 用來獲取資源,POST 用來新建資源,PUT 用來更新資源,DELETE 用來刪除資源。

請求方式 作用
GET 查詢
POST 保存
PUT 更新
DELETE 刪除

2.REST風格的URL

REST風格要求我們不要再使用問號鍵值對的方式攜帶請求參數(shù),而是從URL地址中獲取。下面我們進行一下對比:

3.REST風格URL的好處
①含蓄,安全

使用問號鍵值對的方式給服務器傳遞數(shù)據(jù)太明顯,容易被人利用來對系統(tǒng)進行破壞。使用REST風格攜帶數(shù)據(jù)不再需要明顯的暴露數(shù)據(jù)的名稱。
②風格統(tǒng)一

URL地址整體格式統(tǒng)一,從前到后始終都使用斜杠劃分各個內(nèi)容部分,用簡單一致的格式表達語義。
③無狀態(tài)

在調(diào)用一個接口(訪問、操作資源)的時候,可以不用考慮上下文,不用考慮當前狀態(tài),極大的降低了系統(tǒng)設計的復雜度。
④嚴謹,規(guī)范

嚴格按照HTTP1.1協(xié)議中定義的請求方式本身的語義進行操作。
⑤簡潔,優(yōu)雅

過去做增刪改查操作需要設計4個不同的URL,現(xiàn)在一個就夠了

⑥豐富的語義

通過URL地址就可以知道資源之間的關系。

http://localhost:8080/shop
http://localhost:8080/shop/product
http://localhost:8080/shop/product/cellPhone
http://localhost:8080/shop/product/cellPhone/iPhone

第二節(jié) SpringMVC對四種請求方式的支持
1.說明

受HTML的限制,只有GET請求和POST請求是可以直接生成的。為了生成PUT和DELETE請求方式我們需要借助一個過濾器:org.springframework.web.filter.HiddenHttpMethodFilter,這個過濾器可以將POST請求轉換為PUT或DELETE等其他形式。
2.HiddenHttpMethodFilter的使用方法
①在web.xml中進行配置,攔截所有資源。

<filter>
	<filter-name>HiddenHttpMethodFilter</filter-name>
	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>HiddenHttpMethodFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

②在表單隱藏域中通過_method請求參數(shù)附帶請求方式名稱
jsp代碼:

<input type="hidden" name="_method" value="put"/>

③通過點擊超鏈接執(zhí)行刪除操作。

這是一個難點,超鏈接中沒有表單隱藏域,所以需要將超鏈接轉換為表單進行提交,這就需要借助于JavaScript。

[1]在頁面上創(chuàng)建一個action屬性為空的form表單
jsp代碼:

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

[2]給所有超鏈接綁定單擊響應函數(shù)
jsp代碼:

 <a href="${pageContext.request.contextPath}/emp/ID" class="empRemove">刪除</a>

jsp中jquery代碼:

$(".empRemove").click(function(){
//※※※※※※※※※以下操作將GET請求轉換為POST請求※※※※※※※※※
//1.先獲取到當前超鏈接原本要訪問的URL地址
//this是當前被點擊的超鏈接的引用,是DOM對象
var targetUrl = this.href;

		//2.獲取負責轉換請求方式的表單的jQuery對象
		var $form = $("form");

		//3.將表單的action屬性設置為超鏈接的URL地址
		$form.attr("action",targetUrl );

		//4.提交表單
		//將表單元素封裝為jQuery對象后調(diào)用submit()方法可以提交表單,相當于點擊表單的提交按鈕
		$form.submit();

		//5.超鏈接不跳轉
		return false;

});

@PathVariable注解

通過URL地址攜帶的數(shù)據(jù)需要通過@PathVariable注解來獲取。它的用法是:
Handled代碼:

//使用@PathVariable注解將URL地址中的變量匹配出來
@RequestMapping(value="/emp/{empId}", method=RequestMethod.DELETE)
public String testPathVariable(@PathVariable(“empId”) String empId) {
System.out.println(“empId=”+empId);
return “redirect:/result”;
}

實戰(zhàn)代碼:
項目結構

web.xml

<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
		<filter>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>HiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 包掃描 -->
	<context:component-scan base-package="com.*" />
	<!-- 加前綴后綴 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/page/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 保證常規(guī)資源可以訪問 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 保證靜態(tài)資源可以訪問 -->
	<mvc:default-servlet-handler />
</beans>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/emp">去list頁面</a>
</body>
</html>

dataList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
	$(function(){
		$(".remove").click(function(){
			var data = $(this).parents("tr").children("td:eq(1)").text();
			var confirm = window.confirm("你確定要"+data+"刪除嗎?");
			if(!confirm){
				return false;
			}
			//※※※※※※※※※以下操作將GET請求轉換為POST請求※※※※※※※※※
			//1.先獲取到當前超鏈接原本要訪問的URL地址
			//this是當前被點擊的超鏈接的引用,是DOM對象
			var targetUrl = this.href;
	
			//2.獲取負責轉換請求方式的表單的jQuery對象
			var $form = $("form");
	
			//3.將表單的action屬性設置為超鏈接的URL地址
			$form.attr("action",targetUrl );
	
			//4.提交表單
			//將表單元素封裝為jQuery對象后調(diào)用submit()方法可以提交表單,相當于點擊表單的提交按鈕
			$form.submit();
	
			//5.超鏈接不跳轉
			return false;
		});
	});
</script>
</head>
<body>
	<form method="POST">
		<input type="hidden" name="_method" value="DELETE" />
	</form>
	
	<center>
		<table>
			<c:if test="${empty requestScope.list }">
				<tr>
					<td>沒有查詢到數(shù)據(jù)</td>
				</tr>
			</c:if>
			<c:if test="${!empty requestScope.list }">
				<tr>
					<td>ID</td>
					<td>姓名</td>
					<td>SSN</td>
					<td>部門名稱</td>
					<td colspan="2">操作</td>
				</tr>
				<c:forEach items="${requestScope.list }" var="list">
					<tr>
						<td>${list.empId}</td>
						<td>${list.empName}</td>
						<td>${list.ssn }</td>
						<td>${list.department.deptName }</td>
						<td><a href="${pageContext.request.contextPath }/emp/${list.empId}" class="remove">刪除</a></td>
						<td><a href="${pageContext.request.contextPath }/emp/${list.empId}">編輯</a></td>
					</tr>
				</c:forEach>
			</c:if>
			<tr>
				<td colspan="6" align="center">
				<a href="${pageContext.request.contextPath }/emp/list">添加</a></td>
			</tr>
		</table>
	</center>
</body>
</html>

dataEdit.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath }/emp"
		method="post">
		<input type="hidden" name="_method" value="put" />
		<input type="hidden" name="empId" value="${requestScope.emdit.empId }"/>
		<table>
			<tr>
				<td>姓名</td>
				<td><input type="text" name="empName" value="${emdit.empName }" />
				</td>
			</tr>
			<tr>
				<td>SSN</td>
				<td><input type="text" name="ssn" value="${emdit.ssn }" /></td>
			</tr>
			<tr>
				<td>所在部門</td>
				<td><select name="department.deptId">
						<c:if test="${!empty deptList }">
							<c:forEach items="${requestScope.deptList}" var="dept">
								<c:if test="${dept.deptId==requestScope.emdit.department.deptId }">
									<option value="${dept.deptId }" selected="selected">${dept.deptName }</option>
								</c:if>
								<option value="${dept.deptId }" selected="selected">${dept.deptName }</option>
							</c:forEach>
						</c:if>
				</select></td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit" value="保存" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

dataAdd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/emp" method="post">
		<table>
			<tr>
				<td>姓名</td>
				<td>
					<input type="text" name="empName"/>
				</td>
			</tr>
			<tr>
				<td>SSN</td>
				<td>
					<input type="text" name="ssn"/>
				</td>
			</tr>
			<tr>
				<td>所在部門</td>
				<td>
					<select name="department.deptId">
						<c:if test="${empty deptList }">
							<option>部門數(shù)據(jù)查詢失?。。。?span id="nsacrb5wa"    class="token tag"></option>
						</c:if>
						<c:if test="${!empty deptList }">
							<c:forEach items="${requestScope.deptList}" var="dept">
								<option value="${dept.deptId }">${dept.deptName }</option>
							</c:forEach>
						</c:if>
					</select>
				</td>
			</tr>
			<tr>
				<td colspan="2">
					<input type="submit" value="保存"/>
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

DeptDao.java

package com.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.pojo.Department;

@Repository
public class DeptDao {

private static Map<String, Department> dataMap;
private static Map<String, Department> namedMap;

static {
	dataMap = new HashMap<>();
	namedMap = new HashMap<>();

	Department department = new Department(UUID.randomUUID().toString(), "市場部");
	dataMap.put(department.getDeptId(), department);
	namedMap.put(department.getDeptName(), department);

	department = new Department(UUID.randomUUID().toString(), "銷售部");
	dataMap.put(department.getDeptId(), department);
	namedMap.put(department.getDeptName(), department);

	department = new Department(UUID.randomUUID().toString(), "行政部");
	dataMap.put(department.getDeptId(), department);
	namedMap.put(department.getDeptName(), department);

	department = new Department(UUID.randomUUID().toString(), "人事部");
	dataMap.put(department.getDeptId(), department);
	namedMap.put(department.getDeptName(), department);

	department = new Department(UUID.randomUUID().toString(), "技術部");
	dataMap.put(department.getDeptId(), department);
	namedMap.put(department.getDeptName(), department);

	department = new Department(UUID.randomUUID().toString(), "公關部");
	dataMap.put(department.getDeptId(), department);
	namedMap.put(department.getDeptName(), department);

}

public static Department getDeptByName(String deptName) {
	return namedMap.get(deptName);
}

public static Department getDeptById(String uuid) {
	return dataMap.get(uuid);
}

public List<Department> getDeptList() {
	return new ArrayList<>(dataMap.values());
}

}

EmpDao.java

package com.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import com.pojo.Department;
import com.pojo.Employee;
@Repository
public class EmpDao {
private static Map<String, Employee> dataMap;
static {
dataMap = new HashMap<>();
String empId = UUID.randomUUID().toString();
dataMap.put(empId, new Employee(empId, “喬峰”, “SSN001”, DeptDao.getDeptByName(“市場部”)));

	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "虛竹", "SSN002", DeptDao.getDeptByName("市場部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "段譽", "SSN003", DeptDao.getDeptByName("市場部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "鳩摩智", "SSN004", DeptDao.getDeptByName("技術部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "蕭遠山", "SSN005", DeptDao.getDeptByName("技術部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "慕容復", "SSN006", DeptDao.getDeptByName("技術部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "段正淳", "SSN007", DeptDao.getDeptByName("公關部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "段延慶", "SSN008", DeptDao.getDeptByName("公關部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "丁春秋", "SSN009", DeptDao.getDeptByName("銷售部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "無崖子", "SSN010", DeptDao.getDeptByName("人事部")));
	
	empId = UUID.randomUUID().toString();
	dataMap.put(empId, new Employee(empId, "慕容博", "SSN011", DeptDao.getDeptByName("人事部")));
}

public void saveEmp(Employee employee) {
	
	String empId = UUID.randomUUID().toString();
	employee.setEmpId(empId);
	
	String deptId = employee.getDepartment().getDeptId();
	Department department = DeptDao.getDeptById(deptId);
	employee.setDepartment(department);
	
	dataMap.put(empId, employee);
}

public void removeEmp(String empId) {
	dataMap.remove(empId);
}

public void updateEmp(Employee employee) {
	
	String deptId = employee.getDepartment().getDeptId();
	Department department = DeptDao.getDeptById(deptId);
	employee.setDepartment(department);
	
	dataMap.put(employee.getEmpId(), employee);
}

public Employee getEmpById(String empId) {
	return dataMap.get(empId);
}

public List<Employee> getEmpList() {
	return new ArrayList<>(dataMap.values());
}

}

Department

package com.pojo;

public class Department {

private String deptId;
private String deptName;
public Department() {
	
}
public Department(String deptId, String deptName) {
	super();
	this.deptId = deptId;
	this.deptName = deptName;
}
public String getDeptId() {
	return deptId;
}
public void setDeptId(String deptId) {
	this.deptId = deptId;
}
public String getDeptName() {
	return deptName;
}
public void setDeptName(String deptName) {
	this.deptName = deptName;
}
@Override
public String toString() {
	return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
}

}

Employee

package com.pojo;

public class Employee {

private String empId;
private String empName;
private String ssn;
private Department department;
public Employee(String empId, String empName, String ssn, Department department) {
	super();
	this.empId = empId;
	this.empName = empName;
	this.ssn = ssn;
	this.department = department;
}
public Employee() {
}
public String getEmpId() {
	return empId;
}
public void setEmpId(String empId) {
	this.empId = empId;
}
public String getEmpName() {
	return empName;
}
public void setEmpName(String empName) {
	this.empName = empName;
}
public String getSsn() {
	return ssn;
}
public void setSsn(String ssn) {
	this.ssn = ssn;
}
public Department getDepartment() {
	return department;
}
public void setDepartment(Department department) {
	this.department = department;
}
@Override
public String toString() {
	return "Employee [empId=" + empId + ", empName=" + empName + ", ssn=" + ssn + ", department=" + department
			+ "]";
}

}

Services

package com.services;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dao.DeptDao;
import com.dao.EmpDao;
import com.pojo.Department;
import com.pojo.Employee;

@Service
public class Services {
@Autowired
private DeptDao deptDao;
@Autowired
private EmpDao empDao;

public List<Department> deptDaoList(){
	return deptDao.getDeptList();
};

public List<Employee> empDaoList(){
	return empDao.getEmpList();
}

public void saveData(Employee employee) {
	empDao.saveEmp(employee);
}

public void delectData(String empId) {
	empDao.removeEmp(empId);
}

public Employee emdit(String empId) {
	Employee empById = empDao.getEmpById(empId);
	return empById;
}

public void updateEmp(Employee employee) {
	empDao.updateEmp(employee);
}

}

Handled

package com.handled;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestParam;
import com.pojo.Department;
import com.pojo.Employee;
import com.services.Services;
import com.sun.tracing.dtrace.ProviderAttributes;

@Controller
public class Handled {

@Autowired
private Services services;
@RequestMapping("/emp")
public String toList(Model model) {
	List<Employee> empDaoList = services.empDaoList();
	model.addAttribute("list", empDaoList);
	return "dataList";
}
@RequestMapping("/emp/list")
public String toAddData(Model model) {
	List<Department> deptDaoList = services.deptDaoList();
	model.addAttribute("deptList", deptDaoList);
	return "dataAdd";
}
@RequestMapping(value="/emp",method=RequestMethod.POST)
public String addData(Employee employee) {
	services.saveData(employee);
	return "redirect:/emp";
}
@RequestMapping(value="/emp/{empId}",method=RequestMethod.DELETE)
public String deleteData(@PathVariable("empId") String empId) {
	services.delectData(empId);
	return "redirect:/emp";
}
@RequestMapping(value="/emp/{empId}",method=RequestMethod.GET)
public String emditData(@PathVariable("empId") String empId,Model model) {
	List<Department> deptDaoList = services.deptDaoList();
	Employee employee = services.emdit(empId);
	model.addAttribute("emdit",employee);
	model.addAttribute("deptList",deptDaoList);
	return "dataEdit";
}
@RequestMapping(value="/emp",method=RequestMethod.PUT)
public String subemdData(Employee employee) {
	services.updateEmp(employee);
	return "redirect:/emp";
}

}