博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC中CRUD实例
阅读量:6375 次
发布时间:2019-06-23

本文共 20037 字,大约阅读时间需要 66 分钟。

一:新建项目(下面的几乎属于公共的方法,不需要改动)

1.结构

  

 

  

2.添加lib

 

3.配置web.xml

1 
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 
2
12
13
14 15
16
17
18
19
20

 

5.实体类Department

1 package com.spring.it.enties; 2  3 public class Department { 4  5     private Integer id; 6     private String departmentName; 7  8     public Department() { 9         10     }11     12     public Department(int i, String string) {13         this.id = i;14         this.departmentName = string;15     }16 17     public Integer getId() {18         return id;19     }20 21     public void setId(Integer id) {22         this.id = id;23     }24 25     public String getDepartmentName() {26         return departmentName;27     }28 29     public void setDepartmentName(String departmentName) {30         this.departmentName = departmentName;31     }32 33     @Override34     public String toString() {35         return "Department [id=" + id + ", departmentName=" + departmentName36                 + "]";37     }38     39 }

 

6.实体类Employee

1 package com.spring.it.enties; 2  3 import java.util.Date; 4 import org.springframework.format.annotation.DateTimeFormat; 5 import org.springframework.format.annotation.NumberFormat; 6  7 public class Employee { 8  9     private Integer id;10     private String lastName;11     private String email;12     //1 male, 0 female13     private Integer gender;14     private Department department;15     16     public Employee(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     public Employee() {27         28     }29     public Integer getId() {30         return id;31     }32 33     public void setId(Integer id) {34         this.id = id;35     }36 37     public String getLastName() {38         return lastName;39     }40 41     public void setLastName(String lastName) {42         this.lastName = lastName;43     }44 45     public String getEmail() {46         return email;47     }48 49     public void setEmail(String email) {50         this.email = email;51     }52 53     public Integer getGender() {54         return gender;55     }56 57     public void setGender(Integer gender) {58         this.gender = gender;59     }60 61     public Department getDepartment() {62         return department;63     }64 65     public void setDepartment(Department department) {66         this.department = department;67     }68     69     @Override70     public String toString() {71         return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender72                 + ", department=" + department + "]";73     }74 75     76 }

 

7.Dao---DepartmentDao

1 package com.spring.it.dao; 2  3 import java.util.Collection; 4 import java.util.HashMap; 5 import java.util.Map; 6  7 import org.springframework.stereotype.Repository; 8 import com.spring.it.enties.Department; 9 10 @Repository11 public class DepartmentDao {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 Collection
getDepartments(){26 return departments.values();27 }28 29 public Department getDepartment(Integer id){30 return departments.get(id);31 }32 33 }

 

8.EmployeeDao

1 package com.spring.it.dao; 2  3 import com.spring.it.enties.Department; 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.Map; 7  8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Repository;10 import com.spring.it.enties.Employee;11 @Repository12 public class EmployeeDao {13 14     private static Map
employees = null;15 16 @Autowired17 private DepartmentDao 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 void save(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 Collection
getAll(){41 return employees.values();42 }43 44 public Employee get(Integer id){45 return employees.get(id);46 }47 48 public void delete(Integer id){49 employees.remove(id);50 }51 }

 

二:展示所有的员工---查看操作

1.Controller--EmployeeHander

1 package com.spring.it.handlers; 2  3 import java.util.Map; 4  5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8  9 import com.spring.it.dao.EmployeeDao;10 11 @Controller12 public class EmployeeHander {13     @Autowired(required=true)14     private EmployeeDao employeeDao;15     16     @RequestMapping("/emps")17     public String list(Map
map) {18 System.out.println("====");19 map.put("employee", employeeDao.getAll());20 return "list";21 }22 }

 

2.首页index.jsp

1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" 2     pageEncoding="ISO-8859-1"%> 3  4  5  6 
7 Insert title here 8 9 10
11 list emps12 13

 

3.list.jsp

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4  5  6  7 
8 Insert title here 9 10 11 This Is All Employee12
13 没有任何的员工信息14
15
16
27
17
18
19
20
21
22
23
24
25
26
28
29
30
31
32
33
34
35
36 37
ID LastName Email Gender Department Edit Delete
${emp.id} ${emp.lastName} ${emp.email} ${emp.gender==0?'Female':'Male'} ${emp.department.departmentName} Edit Delete
38
39 40

 

4.效果

  

 

三:添加操作

1.controller

  主要是增加了input与save操作。

1 package com.spring.it.handlers; 2  3 import java.util.Map; 4  5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 10 import com.spring.it.dao.DepartmentDao;11 import com.spring.it.dao.EmployeeDao;12 import com.spring.it.enties.Department;13 import com.spring.it.enties.Employee;14 15 @Controller16 public class EmployeeHander {17     @Autowired(required=true)18     private EmployeeDao employeeDao;19     20     @Autowired(required=true)21     private DepartmentDao departmentDao;22     23     /**24      * 保存,是submit提交过来的请求,属于Post请求25      */26     @RequestMapping(value="/emp",method=RequestMethod.POST)27     public String 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(Map
map) {37 map.put("department", departmentDao.getDepartments());38 //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值39 map.put("employee", new Employee());40 return "input";41 }42 43 /**44 * 展示所有的员工45 */46 @RequestMapping("/emps")47 public String list(Map
map) {48 System.out.println("====");49 map.put("employee", employeeDao.getAll());50 return "list";51 }52 }

 

2.input.jsp

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 4 <%@ page import="java.util.Map"%> 5 <%@ page import="java.util.HashMap"%> 6  7  8  9 
10 Insert title here11 12 13
14
15
16 LastName:
17 Email:
18 <%19 Map
genders=new HashMap();20 genders.put("1", "Male");21 genders.put("0", "Female");22 request.setAttribute("genders", genders);23 %>24 Gender:
25 Department:
27
28
29 30

 

3.效果

  

  

 

4.PS---form表单

  使用的是spring form表单,在input中需要引入标签。

  

  

 

四:删除操作

1.修改list.jsp

  因为这个时候的list.jsp的delete按钮的连接还是空的,需要补充进去。

  这个get不能直接转换成delete操作,所以需要借助js。

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4  5  6  7 
8 Insert title here 9 10 19 20 21
22
23
24 25 This Is All Employee26
27 没有任何的员工信息28
29
30
41
31
32
33
34
35
36
37
38
39
40
42
43
44
45
46
47
48
49
50 51
ID LastName Email Gender Department Edit Delete
${emp.id} ${emp.lastName} ${emp.email} ${emp.gender==0?'Female':'Male'} ${emp.department.departmentName} Edit Delete
52
53
54 Add New Employee55 56

 

2.controller

1 package com.spring.it.handlers; 2  3 import java.util.Map; 4  5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.PathVariable; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod;10 import org.springframework.web.bind.annotation.ResponseBody;11 12 import com.spring.it.dao.DepartmentDao;13 import com.spring.it.dao.EmployeeDao;14 import com.spring.it.enties.Department;15 import com.spring.it.enties.Employee;16 17 @Controller18 public class EmployeeHander {19     @Autowired(required=true)20     private EmployeeDao employeeDao;21     22     @Autowired(required=true)23     private DepartmentDao 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     public String 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(Map
map) {49 map.put("department", departmentDao.getDepartments());50 //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值51 map.put("employee", new Employee());52 return "input";53 }54 55 /**56 * 展示所有的员工57 */58 @RequestMapping("/emps")59 public String list(Map
map) {60 System.out.println("====");61 map.put("employee", employeeDao.getAll());62 return "list";63 }64 }

 

3.处理静态资源

  因为springmvc拦截所有的请求,所以静态资源也被拦截,但是静态资源没有被映射。

  但是静态资源是不需要映射的。

  解决方式是在springmvc的配置文件中配置default-servlet-handler,但是以前的功能又出现了问题,这个时候需要添加上annotation-driven

1 
2
12
13
14 15
16
17
18
19
20 21
22
23

 

4.效果

  

 

五:修改

1.修改list

  Edit的连接需要修改。

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 4  5  6  7 
8 Insert title here 9 10 19 20 21
22
23
24 25 This Is All Employee26
27 没有任何的员工信息28
29
30
41
31
32
33
34
35
36
37
38
39
40
42
43
44
45
46
47
48
49
50 51
ID LastName Email Gender Department Edit Delete
${emp.id} ${emp.lastName} ${emp.email} ${emp.gender==0?'Female':'Male'} ${emp.department.departmentName} Edit Delete
52
53
54 Add New Employee55 56

 

2.java

  主要有三个部分:

  弹出到修改的页面函数

  更新的函数

  保证lastname不改变的函数。

1 package com.spring.it.handlers; 2  3 import java.util.Map; 4  5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.ModelAttribute; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.RequestMapping;10 import org.springframework.web.bind.annotation.RequestMethod;11 import org.springframework.web.bind.annotation.RequestParam;12 import org.springframework.web.bind.annotation.ResponseBody;13 import org.springframework.web.servlet.view.RedirectView;14 15 import com.spring.it.dao.DepartmentDao;16 import com.spring.it.dao.EmployeeDao;17 import com.spring.it.enties.Department;18 import com.spring.it.enties.Employee;19 20 @Controller21 public class EmployeeHander {22     @Autowired(required=true)23     private EmployeeDao employeeDao;24     25     @Autowired(required=true)26     private DepartmentDao departmentDao;27 28     /**29      * 使得lastName不被修改,使用ModelAttribute30      */31     @ModelAttribute32     public void getEmployee(@RequestParam(value="id",required=false) Integer id,Map
map) {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 public String 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,Map
map) {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 public String 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(Map
map) {82 map.put("department", departmentDao.getDepartments());83 //因为form表单的原因,默认一定要回显,第一次尽进来需要传入一个值84 map.put("employee", new Employee());85 return "input";86 }87 88 /**89 * 展示所有的员工90 */91 @RequestMapping("/emps")92 public String list(Map
map) {93 System.out.println("====");94 map.put("employee", employeeDao.getAll());95 return "list";96 }97 }

 

3.input页面

  根据id是否存在,决定lastname是否显示。

  id决定是否进行走PUT。

1 <%@ page language="java" contentType="text/html; charset=utf-8" 2     pageEncoding="utf-8"%> 3 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 4 <%@ page import="java.util.Map"%> 5 <%@ page import="java.util.HashMap"%> 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 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 Map
genders=new HashMap();30 genders.put("1", "Male");31 genders.put("0", "Female");32 request.setAttribute("genders", genders);33 %>34 Gender:
35 Department:
37
38
39 40

 

4.效果

  

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

二:

转载地址:http://zvtqa.baihongyu.com/

你可能感兴趣的文章
Confluence 6 配置校验和识别
查看>>
Ubuntu Server 上安装 Jexus
查看>>
二台inux主机之间scp复制文件
查看>>
Android studio 申请签名,设置签名key位置 查看 sha1
查看>>
浏览器渲染原理及解剖浏览器内部工作原理
查看>>
向大院大所要智慧——江苏创新转型扫描
查看>>
dubbo连接zookeeper注册中心因为断网导致线程无限等待问题【转】
查看>>
Spring Boot项目配置RabbitMQ集群
查看>>
bash 交互与非交互
查看>>
怎么提高自身技术
查看>>
北京游泳馆
查看>>
cacti安装与配置
查看>>
Mac 安卓模拟器打开 ONS
查看>>
完全卸载Oracle 11g教程
查看>>
Oracle调整表空间大小——ORA-03297: 文件包含在请求的 RESIZE 值以外使用的数据
查看>>
二叉树(一)
查看>>
[Windows Azure]Windows Azure Identity
查看>>
Java 技术新手入门
查看>>
【运维囧事】显卡而引起的事故
查看>>
Oracle10G的性能优化之AWR生产实践一
查看>>