Spring MVC框架:第七章:REST架构风格

第一节 REST简介
1.概念
Representational State Transfer——表现层(资源)状态转化。是目前最流行的一种互联网软件架构风格。它倡导结构清晰、符合标准、易于理解、扩展方便的Web架构体系,主张严格按照HTTP协议中定义的规范设计结构严谨的Web应用架构体系。由于REST所倡导的理念让Web应用更易于开发和维护,更加优雅简洁,所以正得到越来越多网站的采用。
资源(Resources):网络上的一个实体,或者说是网络上的一个具体信息。它可以是一段文本、一张图片、一首歌曲、一种服务,总之就是一个具体的存在。可以用一个URI(统一资源定位符)指向它,每种资源对应一个特定的 URI 。要获取这个资源,访问它的URI就可以,因此 URI 即为每一个资源的独一无二的识别符。

表现层(Representation):把资源具体呈现出来的形式,叫做它的表现层(Representation)。比如,文本可以用txt格式表现,也可以用HTML格式、XML格式、JSON格式表现,甚至可以采用二进制格式。

状态转化(State Transfer):每发出一个请求,就代表了客户端和服务器的一次交互过程。HTTP协议,是一个无状态协议,即所有的状态都保存在服务器端。因此,如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化”(State Transfer)。而这种转化是建立在表现层之上的,所以就是 “表现层状态转化”。具体说,就是 HTTP 协议里面,四个表示操作方式的动词:GET、POST、PUT、DELETE。它们分别对应四种基本操作:GET 用来获取资源,POST 用来新建资源,PUT 用来更新资源,DELETE 用来删除资源。

请求方式 作用
GET 查询
POST 保存
PUT 更新
DELETE 删除

2.REST风格的URL

REST风格要求我们不要再使用问号键值对的方式携带请求参数,而是从URL地址中获取。下面我们进行一下对比:

3.REST风格URL的好处
①含蓄,安全

使用问号键值对的方式给服务器传递数据太明显,容易被人利用来对系统进行破坏。使用REST风格携带数据不再需要明显的暴露数据的名称。
②风格统一

URL地址整体格式统一,从前到后始终都使用斜杠划分各个内容部分,用简单一致的格式表达语义。
③无状态

在调用一个接口(访问、操作资源)的时候,可以不用考虑上下文,不用考虑当前状态,极大的降低了系统设计的复杂度。
④严谨,规范

严格按照HTTP1.1协议中定义的请求方式本身的语义进行操作。
⑤简洁,优雅

过去做增删改查操作需要设计4个不同的URL,现在一个就够了

⑥丰富的语义

通过URL地址就可以知道资源之间的关系。

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

第二节 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请求参数附带请求方式名称
jsp代码:

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

③通过点击超链接执行删除操作。

这是一个难点,超链接中没有表单隐藏域,所以需要将超链接转换为表单进行提交,这就需要借助于JavaScript。

[1]在页面上创建一个action属性为空的form表单
jsp代码:

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

[2]给所有超链接绑定单击响应函数
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对象后调用submit()方法可以提交表单,相当于点击表单的提交按钮
		$form.submit();

		//5.超链接不跳转
		return false;

});

@PathVariable注解

通过URL地址携带的数据需要通过@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”;
}

实战代码:
项目结构

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>
	<!-- 保证常规资源可以访问 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 保证静态资源可以访问 -->
	<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对象后调用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>没有查询到数据</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>部门数据查询失败!!!</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";
}

}