REST风格的增删改查(2)

时间:2023-03-09 05:26:39
REST风格的增删改查(2)

一、源码

1.页面

index.jsp

    <a href="emps">List All Employee</a>
<br><br>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- SpringMVC 中没有遍历循环的标签,所以使用JSTL -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
<!--
1.使用RESTFUL风格的crud,其中需要将删除操作的请求方式改成DELETE,所以可以使用在web.xml文件中配置的Filter
将POST请求转化为DELETE请求,但是删除按钮是一个超链接,即GET请求,所以要使用JS处理GET请求。
2.SpringMVC处理静态资源,优雅的REST风格的资源URL不希望带.html或.do等后缀,若将DispatcherServlet请求映射
配置为/,则SpringMVC将捕获web容器的所有请求,包括静态资源的请求,SpringMVC会把他们当成普通请求处理,因找不到对应的处理器
将导致错误。
解决办法:在SpringMVC的配置文件中配置<mvc:default-servlet-handler/>的方式解决静资源的问题 -->
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$(".delete").click(function(){
var href = $(this).attr("href");
$("form").attr("action", href).submit();
return false;
});
})
</script> </head>
<body>
<br>
<p align="center">员工信息管理</p>
<br>
<!-- 添加员工操作 -->
<p align="center">
<a href="emp">Add New Employee</a>
</p> <!-- 将GET请求转化为POST请求 -->
<form action="" method="post">
<input type="hidden" name="_method" value="DELETE" />
</form> <!-- 首先判断跳转过来之前,请求域中是否包含有数据 -->
<c:if test="${empty requestScope.employees}">
没有任何员工信息
</c:if>
<!-- 请求域中包含的有数据 -->
<c:if test="${!empty requestScope.employees}">
<table border="1" cellspacing="0" align="center">
<tr>
<th>ID</th>
<th>LastName</th>
<th>Email</th>
<th>Gender</th>
<th>Departmet</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<c:forEach items="${requestScope.employees }" var="emp">
<tr>
<td>${emp.id }</td>
<td>${emp.lastName }</td>
<td>${emp.email }</td>
<td>${emp.gender == 0 ? 'Female' : 'Male' }</td>
<td>${emp.department.departmentName }</td>
<td><a href="emp/${emp.id }">Edit</a></td>
<td><a class="delete" href="emp/${emp.id }">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<!-- 使用SpringMVC的表单标签 -->
<%@ 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" "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> <!--
把表单提交的数据转换成Employee对象
-->
<form action="testConversionServiceConverer" method="POST">
<!-- lastname-email-gender-department.id,例如:GG-gg@163.com-0-105 -->
Employee:<input type="text" name="employee"/>
<input type="submit" value="Submit"/>
</form>
<br><br> <!--
1.该页面用于添加记录操作,同时又用于修改操作
2.使用SpringMVC标签的好处:快速开发,方便表单值的回显
3.注意:可以通过modelAttribute属性指定绑定的模型属性,若没有指定该属性,则默认中request域对象中读取command表单的
Bean,如果该属性值也不存在,则会发生错误。
-->
<form:form action="${pageContext.request.contextPath }/emp" method="POST"
modelAttribute="employee">
<!-- 显示错误信息 -->
<form:errors path="*"></form:errors>
<br>
<%-- <form:input path=""/>, 其中path属性对应着HTML表单标签中的name属性 --%> <!-- 判断是否有ID,因为这个页面是添加操作和修改操作一同使用的,所以使用有无ID来区别 -->
<c:if test="${employee.id == null }">
LastNmae:<form:input path="lastName"/>
<form:errors path="lastName"></form:errors>
<br>
</c:if>
<c:if test="${employee.id != null }">
<form:hidden path="id"/>
<input type="hidden" name="_method" value="PUT"/>
<%-- 对于_method不能使用form:hidden标签,因为methodAttribute对应的bean中没有_method这个属性
<form:hidden path="_method" value="PUT"/>
--%>
</c:if> Email:<form:input path="email"/>
<form:errors path="email"></form:errors>
<br>
<%
Map<String, String> genders = new HashMap();
genders.put("1","Male");
genders.put("0","Female");
request.setAttribute("genders", genders);
%>
Gender:<form:radiobuttons path="gender" items="${genders }"/>
<br>
<!--
临时添加一个出生日期的属性Birth,这时会遇见一些问题:
1.数据类型转换
2.数据类型格式化
3.数据校验 -->
Birth:<form:input path="birth"/>
<form:errors path="birth"></form:errors>
<br> Salary:<form:input path="salary"/>
<br> Department:<form:select path="department.id"
items="${departments }" itemLabel="departmentName" itemValue="id"></form:select>
<br>
<input type="submit" value="添加"/>
</form:form>
</p>
</body>
</html>

2.实体类

Employee.java

package com.springmvc.crud.entities;

import java.util.Date;

import javax.validation.constraints.Past;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
/**
* 实体类
*
*/
public class Employee { private Integer id;
//校验lastname不为空
@NotEmpty
private String lastName; @Email
private String email;
//1 male, 0 female
private Integer gender; private Department department; //校验日期为当前系统时间之前的时间
@Past
//对日期进行格式化,由字符串转化成yyyy-MM-dd
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth; //格式化字符串1,234,567.8为1234567.8
@NumberFormat(pattern="#,###,###.#")
private Float salary; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
} public Department getDepartment() {
return department;
} public void setDepartment(Department department) {
this.department = department;
} public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public Float getSalary() {
return salary;
} public void setSalary(Float salary) {
this.salary = salary;
} public Employee(Integer id, String lastName, String email, Integer gender,
Department department) {
super();
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
} public Employee() {
} @Override
public String toString() {
return "Employee [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", gender=" + gender + ", department=" + department
+ ", birth=" + birth + ", salary=" + salary + "]";
}
}

Department.java

package com.springmvc.crud.entities;
/**
* 实体类
*/
public class Department { private Integer id;
private String departmentName; public Department() {
} public Department(int i, String string) {
this.id = i;
this.departmentName = string;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getDepartmentName() {
return departmentName;
} public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
} @Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName
+ "]";
} }

3.dao类

EmployeeDao.java

package com.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.springmvc.crud.entities.Department;
import com.springmvc.crud.entities.Employee;
@Repository
public class EmployeeDao { private static Map<Integer, Employee> employees = null; @Autowired
private DepartmentDao departmentDao;
//模拟数据库
static {
employees = new HashMap<Integer, Employee>(); employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 0, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 1, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));
}
//定义初始化变量
private static Integer initId = 1006; //添加一条记录
public void save(Employee employee){
//当员工的Id为空值时,就在1006的基础上加加,作用类似于数据库中主键的自增
if(employee.getId() == null){
employee.setId(initId++);
}
//设置employee中department的属性值
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
//向数据库中添加记录
employees.put(employee.getId(), employee);
} //获取所有的记录
public Collection<Employee> getAll(){
return employees.values();
} //获取一条记录
public Employee get(Integer id){
return employees.get(id);
} //删除一条记录
public void delete(Integer id){
employees.remove(id);
} }

DepartmentDao.java

package com.springmvc.crud.dao;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map; import org.springframework.stereotype.Repository; import com.springmvc.crud.entities.Department; @Repository
public class DepartmentDao { private static Map<Integer, Department> departments = null; //模拟数据库
static {
departments = new HashMap<Integer, Department>(); departments.put(101, new Department(101, "D-AA"));
departments.put(102, new Department(102, "D-BB"));
departments.put(103, new Department(103, "D-CC"));
departments.put(104, new Department(104, "D-DD"));
departments.put(105, new Department(105, "D-EE"));
} public Collection<Department> getDepartments(){
return departments.values();
} public Department getDepartment(Integer id){
return departments.get(id);
} }

4.Handler

EmployeeHandler.java

package com.springmvc.crud.handlers;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
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.springmvc.crud.dao.DepartmentDao;
import com.springmvc.crud.dao.EmployeeDao;
import com.springmvc.crud.entities.Employee;
/**
* 控制跳转,并传递参数
*/ @Controller
public class EmployeeHandler { @Autowired
private EmployeeDao employeeDao; //因为要在页面上显示部门信息,所以引入DepartmentDao
@Autowired
private DepartmentDao departmentDao; /**
* 1.控制跳转到查询所有员工信息的操作
* 当请求到这个方法,把查到员工的集合放到Map中,同时自动的放入到请求域中,跳转到目标页面之后就可以直接获取
* @param map
* @return
*/
@RequestMapping("/emps")
public String list(Map<String, Object> map){
map.put("employees", employeeDao.getAll());
return "list";
} /**
* 2.控制跳转到添加一条记录的页面,因为要在页面显示部门信息,所以使用Map映射
* @param map
* @return
*/
@RequestMapping(value="/emp", method=RequestMethod.GET )
public String input(Map<String, Object> map){
//把查找到的部门信息放入到map中,同时放到请求域中
map.put("departments", departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
} @RequestMapping(value="/emp", method=RequestMethod.POST)
public String save(@Valid Employee employee, BindingResult result,
Map<String, Object> map){
System.out.println("save:" + employee); if(result.getErrorCount() > 0){
System.out.println("出错了!");
for(FieldError error : result.getFieldErrors()){
System.out.println(error.getField() + ":" + error.getDefaultMessage());
} //若校验出错,则跳转到指定页面
map.put("departments", departmentDao.getDepartments());
return "input";
} //执行保存操作
employeeDao.save(employee);
return "redirect:/emps";
} /**
* 3.控制跳转到删除一条记录
* @param id
* @return
*/
@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
} /**
* 4.控制跳转到修改一条记录页面
* @param id
* @param map
* @return
*/
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String input(@PathVariable("id") Integer id, Map<String, Object> map){
System.out.println("修改");
//根据ID,查到记录,用于回显
map.put("employee", employeeDao.get(id));
//所有的部门都放到map中
map.put("departments", departmentDao.getDepartments());
return "input";
} @RequestMapping(value="/emp", method=RequestMethod.PUT)
public String update(Employee employee){
System.out.println("提交修改");
employeeDao.save(employee);
return "redirect:/emps";
} @ModelAttribute
public void getEmployee(@RequestParam(value="id", required=false) Integer id,
Map<String, Object> map){
System.out.println("@ModelAttribute"+id);
if(id != null){
map.put("employee", employeeDao.get(id));
}
} /**
* 功能:对lastName属性,不赋值
* @param binder
*/
/*@InitBinder
public void initBinder(WebDataBinder binder){
binder.setDisallowedFields("lastName");
}*/ }

5.配置文件

web.xml

  <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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 配置把post请求转化成PUT或DELETE请求 -->
<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>

springmvc.xml

<!-- 1.配置自动扫描的包 -->
<context:component-scan base-package="com.springmvc.crud"></context:component-scan> <!-- 2.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 3.解决静态资源文件的问题 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>