SpringMVC框架 第7章 综合案例RESTRUL_CRUD
第7章 综合案例RESTRUL_CRUD
7.1 RESTRUL_CRUD_需求
7.1.1 显示所有员工信息
- URI:emps
- 请求方式:GET
- 显示效果
7.1.2 添加操作-去往添加页面
- 显示添加页面:
- URI:emp
- 请求方式:GET
- 显示效果
7.1.3 添加操作-添加员工
- 添加员工信息:
- URI:emp
- 请求方式:POST
- 显示效果:完成添加,重定向到 list 页面。
7.1.4 删除操作
- URL:emp/{id}
- 请求方式:DELETE
- 删除后效果:对应记录从数据表中删除
7.1.5 修改操作-去往修改页面
- URI:emp/{id}
- 请求方式:GET
- 显示效果:回显表单。
7.1.6 修改操作-修改员工
- URI:emp
- 请求方式:PUT
- 显示效果:完成修改,重定向到 list 页面。
7.1.7 相关的类
省略了Service层,为了教学方便
- 实体类:Employee、Department
- Handler:EmployeeHandler
- Dao:EmployeeDao、DepartmentDao
7.1.8 相关的页面
7.2 搭建开发环境
- 拷贝jar包
com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar spring-aop-4.0.0.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar commons-logging-1.1.3.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar spring-jdbc-4.0.0.RELEASE.jar spring-orm-4.0.0.RELEASE.jar spring-tx-4.0.0.RELEASE.jar spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEASE.jar |
- 创建配置文件:xml增加context,mvc,beans名称空间。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置扫描的包:com.atguigu.springmvc.crud --> <context:component-scan base-package="com.atguigu.springmvc"/>
<!-- 配置视图解析器:默认采用转发 --> <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"></property> </bean> </beans> |
- 配置核心控制器:xml
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> |
- 将 POST 请求转换为 PUT 或 DELETE 请求
<filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
- 创建相关页面
/WEB-INF/views/list.jsp
index.jsp
- 增加实体类
- 增加DAO类
package com.atguigu.springmvc.crud.dao;
import java.util.Collection; import java.util.HashMap; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;
import com.atguigu.springmvc.crud.entities.Department; import com.atguigu.springmvc.crud.entities.Employee;
@Repository public class EmployeeDao {
private static Map<Integer, Employee> employees = null;
@Autowired private DepartmentDao departmentDao;
static{ employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA"))); employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB"))); employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC"))); employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD"))); employees.put(1005, new Employee(1005, "E-EE", "ee@163.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<Employee> getAll(){ return employees.values(); }
public Employee get(Integer id){ return employees.get(id); }
public void delete(Integer id){ employees.remove(id); } } |
package com.atguigu.springmvc.crud.dao;
import java.util.Collection; import java.util.HashMap; import java.util.Map;
import org.springframework.stereotype.Repository;
import com.atguigu.springmvc.crud.entities.Department;
@Repository public class DepartmentDao {
private static Map<Integer, Department> departments = null;
static{ departments = new LinkedHashMap<Integer, Department>();
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<Department> getDepartments(){ return departments.values(); }
public Department getDepartment(Integer id){ return departments.get(id); }
} |