博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC实现RESTful的CRUD
阅读量:6179 次
发布时间:2019-06-21

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

hot3.png

需求:

1.显示所有员工信息:URI:emps ;请求方式:GET

显示效果:

2.添加所有员工:

>1.显示添加页面:URI:emp;请求方式:GET

显示效果:

>2.添加员工信息后的页面:URI:emp;请求方式:POST

显示效果:完成添加,重定向到list页面

3.删除操作:URI:emp/{id};请求方式:DELETE

删除后效果:对应记录从数据表中删除

4.修改操作(lastName不可修改):

>1.显示修改页面:URI:emp/{id};请求方式:GET

显示效果:

>2.修改员工信息:URI:emp;请求方式:PUT

显示效果:完成修改,重定向到list页面

相关类:

>实体类:Employee、Department

>Handler:EmployeeHandler

>Dao:EmployeeDao、DepartmentDao

相关页面:list.jsp、input.jsp、edit.jsp

这个小项目我们使用静态数据,没有连接数据库。

1.导入jar包

-commons-logging-x.x.jar

-jstl.jar

-standard.jar

-spring-aop-x.x.x.RELEASE.jar

-spring-beans-x.x.x.RELESE.jar

-spring-context-x.x.x.RELESE.jar

-spring-core-x.x.x.RELESE.jar

-spring-expression-x.x.x.RELESE.jar

-spring-web-x.x.x.RELESE.jar

-spring-webmvc-x.x.x.RELESE.jar

配置web.xml

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

在src下新建springmvc.xml

新建com.f145a.springmvc.crud.enties,在包下新建Department.java和Employee.java

Department.java

package com.f145a.springmvc.crud.enties;public class Department {	private Integer id;	private String departmentName;		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;	}		public Department(int id, String departmentName) {		super();		this.id = id;		this.departmentName = departmentName;	}	public Department() {		// TODO Auto-generated constructor stub	}	}

Employee.java

package com.f145a.springmvc.crud.enties;import java.util.Date;public class Employee {	private Integer id;	private String lastName;	private String email;	private Integer gender;	private Department department;	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;	}		@Override	public String toString() {		return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender				+ ", department=" + department + "]";	}	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() {			}}

新建com.f145a.springmvc.crud.dao,在包下新建DepartmentDao.java和EmployeeDao.java

DepartmentDao.java

package com.f145a.springmvc.crud.dao;import java.util.Collection;import java.util.HashMap;import org.springframework.stereotype.Repository;import com.f145a.springmvc.crud.enties.Department;@Repositorypublic class DepartmentDao {	private static HashMap
departments=null; static{ departments=new HashMap
(); 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
getDepartments(){ return departments.values(); } public Department getDepartment(Integer id){ return departments.get(id); } }

EmployeeDao.java

package com.f145a.springmvc.crud.dao;import java.util.Collection;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;import com.f145a.springmvc.crud.enties.Department;import com.f145a.springmvc.crud.enties.Employee;@Repositorypublic class EmployeeDao {	private static Map
employees=null; @Autowired private DepartmentDao departmentDao; static{ employees=new HashMap
(); employees.put(1001, new Employee(1001,"E-AA","aa@qq.com",1,new Department(101,"D-AA"))); employees.put(1002, new Employee(1002,"E-BB","bb@qq.com",1,new Department(102,"D-BB"))); employees.put(1003, new Employee(1003,"E-CC","cc@qq.com",0,new Department(103,"D-CC"))); employees.put(1004, new Employee(1004,"E-DD","dd@qq.com",0,new Department(104,"D-DD"))); employees.put(1005, new Employee(1005,"E-EE","ee@qq.com",1,new Department(105,"D-EE"))); } private static Integer initId=1006; public void save(Employee employee){ if(employee.getId()==null){ employee.setId(initId++); } employee.setDepartment(departmentDao.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); }}

新建com.f145a.springmvc.crud.handler,在包下新建DepartmentDao.java和EmployeeHandler.java

package com.f145a.springmvc.crud.handlers;import java.util.Map;import javax.websocket.server.PathParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;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.f145a.springmvc.crud.dao.DepartmentDao;import com.f145a.springmvc.crud.dao.EmployeeDao;import com.f145a.springmvc.crud.enties.Employee;@Controllerpublic class EmployeeHandler {	@Autowired	public EmployeeDao employeeDao;		@Autowired	private DepartmentDao departmentDao;		@ModelAttribute	public void getEmployee(@RequestParam(value="id",required=false) Integer id,			Map 
map){ if(id!=null){ map.put("employee", employeeDao.get(id)); } } @RequestMapping(value="/emp",method=RequestMethod.PUT) public String update(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; } @RequestMapping(value="/emp/{id}",method=RequestMethod.GET) public String input(@PathVariable("id") Integer id,Map
map){ map.put("employee", employeeDao.get(id)); map.put("departments", departmentDao.getDepartments()); return "input"; } @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/emps"; } @RequestMapping(value="/emp",method=RequestMethod.POST) public String save(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; } @RequestMapping(value="emp",method=RequestMethod.GET) public String input(Map
map){ map.put("departments", departmentDao.getDepartments()); map.put("employee", new Employee()); return "input"; } @RequestMapping("/emps") public String list(Map
map){ map.put("employees",employeeDao.getAll()); return "list"; }}

欢迎访问我的个人博客

转载于:https://my.oschina.net/chengzequn/blog/784619

你可能感兴趣的文章
Raid磁盘阵列真的是100%的安全吗?raid有哪些常见的故障?
查看>>
Raid5两块硬盘离线解决方案 -阵列数据恢复案例
查看>>
IBM AIX存储层结构介绍 / 常用命令整理
查看>>
sudo用法简记
查看>>
有关宏定义的一篇文章
查看>>
Kubernetes 基本概念
查看>>
Linux命令:ssh,scp使用及免密码登录
查看>>
我的友情链接
查看>>
在CentOS上编译安装Nginx+实验环境搭建+测试
查看>>
支持二次开发的Zigbee模块(SNAP技术)
查看>>
我的友情链接
查看>>
软件测试常用术语
查看>>
linux磁盘与文件系统管理
查看>>
ORACLE 索引详解
查看>>
第五课_课后习题解答
查看>>
Linux日志系统分析
查看>>
Linux下双网卡绑定bond0
查看>>
你是否也在服务器租用的过程中对服务器各方面的问题产生疑问呢????
查看>>
SSH2屌丝增强版1:构建GenericDao
查看>>
nfs服务配置
查看>>