博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC_2
阅读量:4321 次
发布时间:2019-06-06

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

web.xml

1 
2
3
SpringMVC_2
4
5
6
springDispatcherServlet
7
org.springframework.web.servlet.DispatcherServlet
8
9
contextConfigLocation
10
classpath:springmvc.xml
11
12
1
13
14 15
16
17
springDispatcherServlet
18
/
19
20 21
22
23
HiddenHttpMethodFilter
24
org.springframework.web.filter.HiddenHttpMethodFilter
25
26
27
HiddenHttpMethodFilter
28
/*
29
30

 

 

 springmvc.xml

1 
2
9
10
11
12
13
14
15
16 17
18
19
20
21
22

 Dao层:

1 @Repository//注解于Dao层使得用于spring管理实现自动注入 2 public class EmployeeDao { 3 private static Map
employees; 4 private static Integer initId =1006; 5 @Autowired 6 private DepartmentDao departmentDao; 7 static { 8 employees = new HashMap<>(); 9 employees.put(1001, new Employee(1001, "employee_1001","1001@163.com", 1,new Department()));10 employees.put(1002, new Employee(1002, "employee_1002","1001@163.com", 0,new Department()));11 employees.put(1003, new Employee(1003, "employee_1003","1001@163.com", 1,new Department()));12 employees.put(1004, new Employee(1004, "employee_1004","1001@163.com", 0,new Department()));13 employees.put(1005, new Employee(1005, "employee_1005","1001@163.com", 1,new Department()));14 }15 public void save(Employee employee){16 if (employee.getId()==null) {17 employee.setId(initId++);18 }19 employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));20 employees.put(employee.getId(), employee);21 }22 public Collection
getAll(){23 return employees.values();24 }25 public Employee get(Integer id) {26 return employees.get(id);27 }28 public void delete(Integer id) { 29 employees.remove(id);30 }31 }32

 

1 package org.springmvc.curd.entity; 2  3 public class Department { 4 private int id; 5 private String departmentName; 6 public int getId() { 7     return id; 8 } 9 public void setId(int id) {10     this.id = id;11 }12 public String getDepartmentName() {13     return departmentName;14 }15 public void setDepartmentName(String departmentName) {16     this.departmentName = departmentName;17 }18 @Override19 public String toString() {20     return "Department [id=" + id + ", departmentName=" + departmentName + "]";21 }22 public Department() {23 }24 public Department(int id, String departmentName) {25     super();26     this.id = id;27     this.departmentName = departmentName;28 }29 30 }

Controller层:

1 @Controller 2 public class EmployeeHandler { 3 @Autowired 4 private EmployeeDao employeeDao; 5 @Autowired 6 private DepartmentDao departmentDao; 7      8 @RequestMapping("emps") 9 public String list(Map
map) {10 map.put("employees",employeeDao.getAll());11 return "list"; 12 }13 @RequestMapping(value="addEmployee",method=RequestMethod.GET)14 public String input(Map
map) {15 map.put("departments",departmentDao.getDepartments());16 map.put("employee", new Employee());17 return "input";18 }19 //@PathVariable可用于获取随url发送过来的变量 匹配到参数20 @RequestMapping(value="save", method = RequestMethod.POST)21 public String save(Employee employee) {22 employeeDao.save(employee);23 return "redirect:emps";24 }25 @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)26 public String delete(@PathVariable("id")Integer id) {27 employeeDao.delete(id);28 return "redirect:emps"; 29 }30 31 @RequestMapping("/testFileUpload")//@PathVariable可用于获取随url发送过来的变量 匹配到参数32 public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException {33 System.out.println("desc"+desc);34 System.out.println("OriginalFileName:"+file.getName());35 System.out.println("InputStream:"+file.getInputStream()); 36 return "success";37 }38 39 40 41 }

 

转载于:https://www.cnblogs.com/the-wang/p/8542570.html

你可能感兴趣的文章
读《分布式一致性原理》CURATOR客户端3
查看>>
iOS 虚拟机测试出现的相关问题
查看>>
MySQL crash-safe replication(3): MySQL的Crash Safe和Binlog的关系
查看>>
mac 无法打开xx ,因为无法确认开发者身份
查看>>
简单的排序算法(冒泡、选择、插入)
查看>>
[剑指Offer] 11.二进制中1的个数
查看>>
重置报表输出选择
查看>>
ip代理池抓取qq音乐热歌前300
查看>>
Android面试题集合
查看>>
Android NDK开发
查看>>
Centos中安装和配置vsftp简明教程
查看>>
spring源码学习之AOP(一)
查看>>
AES加密算法动画演示
查看>>
三种方法实现调用Restful接口
查看>>
php第五节(字符串函数和时间、日期函数)
查看>>
magento主页限制某个目录的产品显示数量
查看>>
SpringBoot整合Netty
查看>>
MongoDB数据库的基本操作
查看>>
PAT乙级1014
查看>>
ORACLE wm_concat自定义
查看>>