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

④严谨,规范

严格按照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中进行配置,拦截所有资源。

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

HiddenHttpMethodFilter

/*

②在表单隐藏域中通过_method请求参数附带请求方式名称

jsp代码:

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

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

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

jsp代码:

[2]给所有超链接绑定单击响应函数

jsp代码:

删除

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

springDispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring-mvc.xml

1

springDispatcherServlet

/

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

CharacterEncodingFilter

/*

HiddenHttpMethodFilter

org.springframework.web.filter.HiddenHttpMethodFilter

HiddenHttpMethodFilter

/*

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”>

mvc:annotation-driven</mvc:annotation-driven>

<mvc:default-servlet-handler />

index.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

Insert title here

去list页面

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”%>

Insert title here

<c:if test=“${empty requestScope.list }”>

没有查询到数据

</c:if>

<c:if test=“${!empty requestScope.list }”>

ID 姓名 SSN 部门名称 操作

<c:forEach items=“${requestScope.list }” var=“list”>

${list.empId} ${list.empName} ${list.ssn } ${list.department.deptName } 删除 编辑

</c:forEach>

</c:if>

添加

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”%>

Insert title here

method=“post”>

姓名 SSN 所在部门

<c:if test=“${!empty deptList }”>

<c:forEach items=“${requestScope.deptList}” var=“dept”>

<c:if test=“${dept.deptId==requestScope.emdit.department.deptId }”>

${dept.deptName }

</c:if>

${dept.deptName }

</c:forEach>

</c:if>

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”%>

Insert title here
姓名 SSN 所在部门

<c:if test=“${empty deptList }”>

部门数据查询失败!!!

</c:if>

<c:if test=“${!empty deptList }”>

<c:forEach items=“${requestScope.deptList}” var=“dept”>

${dept.deptName }

</c:forEach>

</c:if>

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 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 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() {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值