一:新建项目(下面的几乎属于公共的方法,不需要改动)
1.结构
2.添加lib
3.配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 SpringMvcCRUD
4
5
6 DispatchServlet
7 org.springframework.web.servlet.DispatcherServlet
8
9 contextConfigLocation
10 classpath:springmcv.xml
11
12 1
13
14
15
16 DispatchServlet
17 /
18
19
20
21
22 HiddenHttpMethodFilter
23 org.springframework.web.filter.HiddenHttpMethodFilter
24
25
26 HiddenHttpMethodFilter
27 /*
28
29
4.配置springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans.xsd8 http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-4.0.xsd10 http://www.springframework.org/schema/mvc11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12
13
14
15
16
17
18
19
20
5.实体类Department
1 packagecom.spring.it.enties;2
3 public classDepartment {4
5 privateInteger id;6 privateString departmentName;7
8 publicDepartment() {9
10 }11
12 public Department(inti, String string) {13 this.id =i;14 this.departmentName =string;15 }16
17 publicInteger getId() {18 returnid;19 }20
21 public voidsetId(Integer id) {22 this.id =id;23 }24
25 publicString getDepartmentName() {26 returndepartmentName;27 }28
29 public voidsetDepartmentName(String departmentName) {30 this.departmentName =departmentName;31 }32
33 @Override34 publicString toString() {35 return "Department [id=" + id + ", departmentName=" +departmentName36 + "]";37 }38
39 }
6.实体类Employee
1 packagecom.spring.it.enties;2
3 importjava.util.Date;4 importorg.springframework.format.annotation.DateTimeFormat;5 importorg.springframework.format.annotation.NumberFormat;6
7 public classEmployee {8
9 privateInteger id;10 privateString lastName;11 privateString email;12 //1 male, 0 female
13 privateInteger gender;14 privateDepartment department;15
16 publicEmployee(Integer id, String lastName, String email, Integer gender,17 Department department) {18 super();19 this.id =id;20 this.lastName =lastName;21 this.email =email;22 this.gender =gender;23 this.department =department;24 }25
26 publicEmployee() {27
28 }29 publicInteger getId() {30 returnid;31 }32
33 public voidsetId(Integer id) {34 this.id =id;35 }36
37 publicString getLastName() {38 returnlastName;39 }40
41 public voidsetLastName(String lastName) {42 this.lastName =lastName;43 }44
45 publicString getEmail() {46 returnemail;47 }48
49 public voidsetEmail(String email) {50 this.email =email;51 }52
53 publicInteger getGender() {54 returngender;55 }56
57 public voidsetGender(Integer gender) {58 this.gender =gender;59 }60
61 publicDepartment getDepartment() {62 returndepartment;63 }64
65 public voidsetDepartment(Department department) {66 this.department =department;67 }68
69 @Override70 publicString toString() {71 return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" +gender72 + ", department=" + department + "]";73 }74
75
76 }
7.Dao---DepartmentDao
1 packagecom.spring.it.dao;2
3 importjava.util.Collection;4 importjava.util.HashMap;5 importjava.util.Map;6
7 importorg.springframework.stereotype.Repository;8 importcom.spring.it.enties.Department;9
10 @Repository11 public classDepartmentDao {12
13 private static Map departments = null;14
15 static{16 departments = new HashMap();17
18 departments.put(101, new Department(101, "D-AA"));19 departments.put(102, new Department(102, "D-BB"));20 departments.put(103, new Department(103, "D-CC"));21 departments.put(104, new Department(104, "D-DD"));22 departments.put(105, new Department(105, "D-EE"));23 }24
25 public CollectiongetDepartments(){26 returndepartments.values();27 }28
29 publicDepartment getDepartment(Integer id){30 returndepartments.get(id);31 }32
33 }
8.EmployeeDao
1 packagecom.spring.it.dao;2
3 importcom.spring.it.enties.Department;4 importjava.util.Collection;5 importjava.util.HashMap;6 importjava.util.Map;7
8 importorg.springframework.beans.factory.annotation.Autowired;9 importorg.springframework.stereotype.Repository;10 importcom.spring.it.enties.Employee;11 @Repository12 public classEmployeeDao {13
14 private static Map employees = null;15
16 @Autowired17 privateDepartmentDao departmentDao;18
19 static{20 employees = new HashMap();21
22 employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA")));23 employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB")));24 employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC")));25 employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD")));26 employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 1, new Department(105, "D-EE")));27 }28
29 private static Integer initId = 1006;30
31 public voidsave(Employee employee){32 if(employee.getId() == null){33 employee.setId(initId++);34 }35
36 employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));37 employees.put(employee.getId(), employee);38 }39
40 public CollectiongetAll(){41 returnemployees.values();42 }43
44 publicEmployee get(Integer id){45 returnemployees.get(id);46 }47
48 public voiddelete(Integer id){49 employees.remove(id);50 }51 }
二:展示所有的员工---查看操作
1.Controller--EmployeeHander
1 packagecom.spring.it.handlers;2
3 importjava.util.Map;4
5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.stereotype.Controller;7 importorg.springframework.web.bind.annotation.RequestMapping;8
9 importcom.spring.it.dao.EmployeeDao;10
11 @Controller12 public classEmployeeHander {13 @Autowired(required=true)14 privateEmployeeDao employeeDao;15
16 @RequestMapping("/emps")17 public String list(Mapmap) {18 System.out.println("====");19 map.put("employee", employeeDao.getAll());20 return "list";21 }22 }
2.首页index.jsp
1
2 pageEncoding="ISO-8859-1"%>
3
4
5
6
7
Insert title here8
9
10
11 list emps
12
13
3.list.jsp
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
Insert title here9
10
11 This Is All Employee12
13 没有任何的员工信息14
15
16
17
18
ID19
LastName20
Email21
Gender22
Department23
Edit24
Delete25
26
27
28
${emp.id}29
${emp.lastName}30
${emp.email}31
${emp.gender==0?'Female':'Male'}32
${emp.department.departmentName}33
Edit34
Delete35
36
37
38
39
40
4.效果
三:添加操作
1.controller
主要是增加了input与save操作。
1 packagecom.spring.it.handlers;2
3 importjava.util.Map;4
5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.stereotype.Controller;7 importorg.springframework.web.bind.annotation.RequestMapping;8 importorg.springframework.web.bind.annotation.RequestMethod;9
10 importcom.spring.it.dao.DepartmentDao;11 importcom.spring.it.dao.EmployeeDao;12 importcom.spring.it.enties.Department;13 importcom.spring.it.enties.Employee;14
15 @Controller16 public classEmployeeHander {17 @Autowired(required=true)18 privateEmployeeDao employeeDao;19
20 @Autowired(required=true)21 privateDepartmentDao departmentDao;22
23 /**
24 * 保存,是submit提交过来的请求,属于Post请求25 */
26 @RequestMapping(value="/emp",method=RequestMethod.POST)27 publicString save(Employee employee) {28 employeeDao.save(employee);29 return "redirect:/emps";30 }31
32 /**
33 * 跳转到input页面,用于添加员工,是Get请求34 */
35 @RequestMapping(value="/emp",method=RequestMethod.GET)36 public String input(Mapmap) {37 map.put("department", departmentDao.getDepartments());38 //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值
39 map.put("employee", newEmployee());40 return "input";41 }42
43 /**
44 * 展示所有的员工45 */
46 @RequestMapping("/emps")47 public String list(Mapmap) {48 System.out.println("====");49 map.put("employee", employeeDao.getAll());50 return "list";51 }52 }
2.input.jsp
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
9
10
Insert title here11
12
13
14
15
16 LastName:
17 Email:
18
19 Mapgenders=newHashMap();20 genders.put("1","Male");21 genders.put("0","Female");22 request.setAttribute("genders", genders);23 %>
24 Gender:
25 Department:
26 items="${department}"itemLabel="departmentName"itemValue="id">
27
28
29
30
3.效果
4.PS---form表单
使用的是spring form表单,在input中需要引入标签。
四:删除操作
1.修改list.jsp
因为这个时候的list.jsp的delete按钮的连接还是空的,需要补充进去。
这个get不能直接转换成delete操作,所以需要借助js。
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
Insert title here9
10
11 $(function(){12 $(".delete").click(function(){13 varhref=$(this).attr("href");14 $("form").attr("action", href).submit();15 return false;16 })17 })18
19
20
21
22
23
24
25 This Is All Employee26
27 没有任何的员工信息28
29
30
31
32
ID33
LastName34
Email35
Gender36
Department37
Edit38
Delete39
40
41
42
${emp.id}43
${emp.lastName}44
${emp.email}45
${emp.gender==0?'Female':'Male'}46
${emp.department.departmentName}47
Edit48
Delete49
50
51
52
53
54 Add New Employee
55
56
2.controller
1 packagecom.spring.it.handlers;2
3 importjava.util.Map;4
5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.stereotype.Controller;7 importorg.springframework.web.bind.annotation.PathVariable;8 importorg.springframework.web.bind.annotation.RequestMapping;9 importorg.springframework.web.bind.annotation.RequestMethod;10 importorg.springframework.web.bind.annotation.ResponseBody;11
12 importcom.spring.it.dao.DepartmentDao;13 importcom.spring.it.dao.EmployeeDao;14 importcom.spring.it.enties.Department;15 importcom.spring.it.enties.Employee;16
17 @Controller18 public classEmployeeHander {19 @Autowired(required=true)20 privateEmployeeDao employeeDao;21
22 @Autowired(required=true)23 privateDepartmentDao departmentDao;24
25 /**
26 * 删除操作27 */
28 @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)29 @ResponseBody30 public String delete(@PathVariable("id") Integer id) {31 employeeDao.delete(id);32 return "redirect:/emps";33 }34
35 /**
36 * 保存,是submit提交过来的请求,属于Post请求37 */
38 @RequestMapping(value="/emp",method=RequestMethod.POST)39 publicString save(Employee employee) {40 employeeDao.save(employee);41 return "redirect:/emps";42 }43
44 /**
45 * 跳转到input页面,用于添加员工,是Get请求46 */
47 @RequestMapping(value="/emp",method=RequestMethod.GET)48 public String input(Mapmap) {49 map.put("department", departmentDao.getDepartments());50 //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值
51 map.put("employee", newEmployee());52 return "input";53 }54
55 /**
56 * 展示所有的员工57 */
58 @RequestMapping("/emps")59 public String list(Mapmap) {60 System.out.println("====");61 map.put("employee", employeeDao.getAll());62 return "list";63 }64 }
3.处理静态资源
因为springmvc拦截所有的请求,所以静态资源也被拦截,但是静态资源没有被映射。
但是静态资源是不需要映射的。
解决方式是在springmvc的配置文件中配置default-servlet-handler,但是以前的功能又出现了问题,这个时候需要添加上annotation-driven
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:mvc="http://www.springframework.org/schema/mvc"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans7 http://www.springframework.org/schema/beans/spring-beans.xsd8 http://www.springframework.org/schema/context9 http://www.springframework.org/schema/context/spring-context-4.0.xsd10 http://www.springframework.org/schema/mvc11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
12
13
14
15
16
17
18
19
20
21
22
23
4.效果
五:修改
1.修改list
Edit的连接需要修改。
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
Insert title here9
10
11 $(function(){12 $(".delete").click(function(){13 varhref=$(this).attr("href");14 $("form").attr("action", href).submit();15 return false;16 })17 })18
19
20
21
22
23
24
25 This Is All Employee26
27 没有任何的员工信息28
29
30
31
32
ID33
LastName34
Email35
Gender36
Department37
Edit38
Delete39
40
41
42
${emp.id}43
${emp.lastName}44
${emp.email}45
${emp.gender==0?'Female':'Male'}46
${emp.department.departmentName}47
Edit48
Delete49
50
51
52
53
54 Add New Employee
55
56
2.java
主要有三个部分:
弹出到修改的页面函数
更新的函数
保证lastname不改变的函数。
1 packagecom.spring.it.handlers;2
3 importjava.util.Map;4
5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.stereotype.Controller;7 importorg.springframework.web.bind.annotation.ModelAttribute;8 importorg.springframework.web.bind.annotation.PathVariable;9 importorg.springframework.web.bind.annotation.RequestMapping;10 importorg.springframework.web.bind.annotation.RequestMethod;11 importorg.springframework.web.bind.annotation.RequestParam;12 importorg.springframework.web.bind.annotation.ResponseBody;13 importorg.springframework.web.servlet.view.RedirectView;14
15 importcom.spring.it.dao.DepartmentDao;16 importcom.spring.it.dao.EmployeeDao;17 importcom.spring.it.enties.Department;18 importcom.spring.it.enties.Employee;19
20 @Controller21 public classEmployeeHander {22 @Autowired(required=true)23 privateEmployeeDao employeeDao;24
25 @Autowired(required=true)26 privateDepartmentDao departmentDao;27
28 /**
29 * 使得lastName不被修改,使用ModelAttribute30 */
31 @ModelAttribute32 public void getEmployee(@RequestParam(value="id",required=false) Integer id,Mapmap) {33 if(id!=null) {34 map.put("employee", employeeDao.get(id));35 }36 }37
38 /**
39 * 编辑,主要是提交40 */
41 @RequestMapping(value="/emp",method=RequestMethod.PUT)42 publicString update(Employee employee) {43 employeeDao.save(employee);44 return "redirect:/emps";45 }46
47 /**
48 * 编辑,主要是跳转到要编辑的页面49 */
50 @RequestMapping(value="/emp/{id}",method=RequestMethod.GET)51 public String input(@PathVariable("id") Integer id,Mapmap) {52 map.put("department", departmentDao.getDepartments());53 //回显
54 map.put("employee", employeeDao.get(id));55 return "input";56 }57
58 /**
59 * 删除操作60 */
61 @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)62 @ResponseBody63 public String delete(@PathVariable("id") Integer id) {64 employeeDao.delete(id);65 return "redirect:/emps";66 }67
68 /**
69 * 保存,是submit提交过来的请求,属于Post请求70 */
71 @RequestMapping(value="/emp",method=RequestMethod.POST)72 publicString save(Employee employee) {73 employeeDao.save(employee);74 return "redirect:/emps";75 }76
77 /**
78 * 跳转到input页面,用于添加员工,是Get请求79 */
80 @RequestMapping(value="/emp",method=RequestMethod.GET)81 public String input(Mapmap) {82 map.put("department", departmentDao.getDepartments());83 //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值
84 map.put("employee", newEmployee());85 return "input";86 }87
88 /**
89 * 展示所有的员工90 */
91 @RequestMapping("/emps")92 public String list(Mapmap) {93 System.out.println("====");94 map.put("employee", employeeDao.getAll());95 return "list";96 }97 }
3.input页面
根据id是否存在,决定lastname是否显示。
id决定是否进行走PUT。
1
2 pageEncoding="utf-8"%>
3
4
5
6
7
8
9
10
11
Insert title here12
13
14
15
16
17
18
19 LastName:
20
21
22
23
24
25
26
27 Email:
28
29 Mapgenders=newHashMap();30 genders.put("1","Male");31 genders.put("0","Female");32 request.setAttribute("genders", genders);33 %>
34 Gender:
35 Department:
36 items="${department}"itemLabel="departmentName"itemValue="id">
37
38
39
40
4.效果
二: