URI:emps
请求方式:GET
显示效果
显示添加页面:
URI:emp
请求方式:GET
显示效果
添加员工信息:
URI:emp
请求方式:POST
显示效果:完成添加, 重定向到 list 页面。
URL:emp/{id}
请求方式:DELETE
删除后效果:对应记录从数据表中删除
URI:emp/{id}
请求方式:GET
显示效果: 回显表单。
URI:emp
请求方式:PUT
显示效果:完成修改, 重定向到 list 页面。
省略了 Service 层
实体类:Employee、Department
Handler:EmployeeHandler
Dao:EmployeeDao、DepartmentDao
list.jsp
input.jsp
edit.jsp
<?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.suncaper.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>
<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>
<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
package com.suncaper.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.suncaper.springmvc.crud.entities.Department;
import com.suncaper.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(