需求:在首页点击链接显示全部employee信息,点击edit和delete分别进行修改和删除操作,点击add进行添加操作。CRUD分别对应post get put delete四种请求。

目录结构如图

web.xml配置

springDispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springDispatcherServlet
/
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*

springmvc.xml配置

error

Department.java

public class Department {	private Integer id;	private String departmentName;	//...}

Employee.java

public class Employee {	private Integer id;	private String lastName;	private String email;	private Integer gender;	//1 male   0 female	private Department department;	//...}

-----------------------------------------------------------------------

DepartmenDao.java

@Repositorypublic class DepartmenDao {//模拟数据库操作	private static  Map
 departments = null; static{ departments = new HashMap
(); departments.put(1, new Department(1, "D-AA1")); departments.put(2, new Department(2, "D-AA2")); departments.put(3, new Department(3, "D-AA3")); departments.put(4, new Department(4, "D-AA4")); departments.put(5, new Department(5, "D-AA5")); } public Collection
 getDepartments(){ return departments.values(); } public Department getDepartment(Integer id) { return departments.get(id); }}

EmployeeDao.java

@Repositorypublic class EmployeeDao {//模拟数据库操作	private static  Map
 employees = null; @Autowired private DepartmenDao departmenDao; static{ employees = new HashMap
(); employees.put(1, new Employee(1, "E-AA1", "1@126.com", 0, new Department(1,"D-AA1"))); employees.put(2, new Employee(2, "E-AA2", "2@126.com", 1, new Department(1,"D-AA1"))); employees.put(3, new Employee(3, "E-AA3", "3@126.com", 0, new Department(2,"D-AA2"))); employees.put(4, new Employee(4, "E-AA4", "4@126.com", 0, new Department(3,"D-AA3"))); employees.put(5, new Employee(5, "E-AA5", "5@126.com", 1, new Department(1,"D-AA1"))); } private static Integer initId = 1; public void save(Employee employee) { if (employee.getId() == null) {//insert or update employee.setId(initId++); } employee.setDepartment(departmenDao.getDepartment(employee.getDepartment().getId())); employees.put(employee.getId(), employee); } public Collection
 getAll(){ return employees.values(); } public Employee get(Integer id) { return employees.get(id); } public void delete(Integer id) { employees.remove(id); }}

EmployeeHandler.java   处理http请求

@Controllerpublic class EmployeeHandler {	@Autowired	private EmployeeDao employeeDao;	@Autowired	private DepartmenDao departmenDao;		@ModelAttribute	public void getEmployee(@RequestParam(value="id", required=false) Integer id, Map
 map) { if (id != null) {//修改操作 map.put("employee", employeeDao.get(id)); } } @RequestMapping("/emps")   //对应index.jsp的链接请求 public String list(Map
map) {// System.out.println(employeeDao.getAll()); map.put("employees", employeeDao.getAll()); return "list";//list.jsp } //add操作 @RequestMapping(value="/emp", method=RequestMethod.GET) public String input(Map
 map) { map.put("departments", departmenDao.getDepartments()); //默认要进行表单回显,所以这里要放一个employee到map里面, //对应input.jsp中的modelAttribute="employee" map.put("employee", new Employee());//input.jsp line17 return "input"; } @RequestMapping(value="/emp", method=RequestMethod.POST) public String save(Employee employee) { employeeDao.save(employee); return "redirect:/emps"; } @RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id) { employeeDao.delete(id); return "redirect:/emps"; } //与前面add操作的input方法不同,此处需要接收参数 @RequestMapping(value="/emp/{id}", method=RequestMethod.GET) public String input(@PathVariable("id") Integer id, Map
 map) {         //默认要进行表单回显,所以这里要放一个employee到map里面, //对应input.jsp中的modelAttribute="employee" map.put("employee", employeeDao.get(id)); map.put("departments", departmenDao.getDepartments()); return "input"; } @RequestMapping(value="/emp", method=RequestMethod.PUT) public String update(Employee employee) { employeeDao.save(employee); return "redirect:/emps"; }}

index.jsp

List all employees

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
$(function() { $(".delete").click(function() { //将post请求转为delete请求 var href = $(this).attr("href"); $("form").attr("action", href).submit(); return false; }); })
Insert title here
没有员工信息
ID LastName Email Gender Department Edit Delete
${emp.id} ${emp.lastName } ${emp.email} ${emp.gender == 0 ? 'Female' : 'Male'} ${emp.department.departmentName }
Edit
Delete
Add new emp

<a href="emp">Add new emp</a> 链接到input页面

input.jsp  添加和修改公用

<%@page import="java.util.HashMap"%><%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%><%@ page import="java.util.HashMap"%><%@ page import="java.util.Map"%><%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
 LastName
 Email
<% Map
 genders = new HashMap(); genders.put("1", "male"); genders.put("0", "female"); request.setAttribute("genders", genders); %> Gender
Department

删除请求对应handler中的delete请求

项目源码地址   访问密码 8069