<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-cfg.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>dispatcherServletservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>hiddenHttpMethodFilterfilter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<filter>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<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"
xsi:schemaLocation="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.xsd">
<context:component-scan base-package="mao.shu.springmvc.crud"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
创建实体类:
Employee类
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Department department;
//getter和setter方法
}
public class Department {
private Integer id;
private String departmentName;
//getter和setter方法
}
编写DAO类:由于此次没有连接数据库所以使用静态的数据
EmployeeDAO
package mao.shu.springmvc.crud.dao;
import mao.shu.springmvc.crud.vo.Department;
import mao.shu.springmvc.crud.vo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class EmployeeDAO {
@Autowired
private DepartmentDAO departmentDAO;
//使用Map集合保存数据
private static Map<Integer, Employee> employees = null;
static {
employees = new HashMap<>();
for (int i = 0; i < 10; i++) {
int gender = i % 2 == 0 ? 1 : 0;
employees.put(1000 + i,
new Employee(1000 + i,
"xiemaoshu" + i,
"[email protected]" + i,
gender,
new Department(7890 + i,
"开发部" + i)));
}
}
/**
* 获取全部的雇员信息
* @return
*/
public Collection<Employee> getAll(){
return this.employees.values();
}
}
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDAO employeeDAO;
@RequestMapping
public String list(Map<String,Object> map){
map.put("employees",this.employeeDAO.getAll());
return "emp_list";
}
}
<body>
<c:if test="${empty requestScope.employees}">
<h1>没有任何雇员信息h1>
c:if>
<table border="1" >
<tr>
<td>雇员idtd>
<td>雇员名称td>
<td>雇员邮箱td>
<td>性别td>
<td>部门信息td>
<td>修改td>
<td>删除td>
tr>
<c:forEach items="${requestScope.employees}" var="emp">
<tr>
<td>${emp.id}td>
<td>${emp.lastName}td>
<td>${emp.email}td>
<td>${emp.gender==0?'女':'男'}td>
<td>${emp.department.departmentName}td>
<td><a href="">修改a> td>
<td><a href="">删除a>td>
tr>
c:forEach>
table>
body>
private static Integer startEmpid = 10;
/**
* 进行雇员添加操作
* @param vo
* @return
*/
public boolean add(Employee vo){
vo.setId(this.startEmpid++);
Department dept = this.departmentDAO.get(vo.getDepartment().getId());
vo.setDepartment(dept);
this.employees.put(vo.getId(),vo);
return true;
}
@Autowired
private DepartmentDAO departmentDAO;
/**
* 显示雇员的添加页面
* 将雇员字段和部门信息保存到请求域之中
* 供添加表单显示操作
* @param map
* @return
*/
@RequestMapping(value="/emp",method = RequestMethod.GET)
public String savePre(Map<String,Object> map){
map.put("employee",new Employee());
map.put("department",this.departmentDAO.getAll());
return "emp_add";
}
/**
* 显示雇员添加表单操作方法使用GET请求
* @param vo
* @return
*/
@RequestMapping(value="/emp",method=RequestMethod.POST)
public String save(Employee vo){
this.employeeDAO.add(vo);
return "redirect:empList";
}
@Repository
public class DepartmentDAO {
//使用Map集合保存数据
private static Map<Integer, Department> departments= null;
static {
departments = new HashMap<>();
for (int i = 0; i < 10; i++) {
int gender = i % 2 == 0 ? 1 : 0;
departments.put(7890 + i,
new Department(7890+i,"开发部"+i));
}
}
public Collection<Department> getAll(){
return this.departments.values();
}
public Department get(Integer deptno){
return this.departments.get(deptno);
}
}
<%@ taglib prefix="from" uri="http://www.springframework.org/tags/form" %>
<Connector URIEncoding="UTF-8" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<body>
<%--
使用SpringMVC的表单标签
modelAttribute 属性描述的是表单的Bean属性,
表单标签会根据modelAttribute提供的值在request中寻找对应的属性,
如果没有指定modelAttribute 则会在request中寻找名为"command"的属性,
如果也没有找到"command"的属性则会抛出异常
--%>
<form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee">
雇员名称:<form:input path="lastName"/><br/>
雇员邮箱<form:input path="email"/><br/>
<%
Map<String, String> genders = new HashMap<>();
genders.put("0", "女");
genders.put("1", "男");
request.setAttribute("genders", genders);
%>
性别:<form:radiobuttons path="gender" items="${genders}"/><br/>
部门:<form:select path="department.id" items="${departments}" itemLabel="departmentName" itemValue="id">form:select>
<br/>
<input type="submit" value="提交"/>
form:form>
body>
<mvc:default-servlet-handler/>
<mvc:annotation-driven >mvc:annotation-driven>
public boolean delete(Integer id){
return this.employees.remove(id) != null;
}
@RequestMapping(value="/emp/{id}",method={RequestMethod.DELETE})
public String remove(@PathVariable("id") Integer id){
this.employeeDAO.delete(id);
return "redirect:/empList";
}
<td><a href="emp/${emp.id}">删除a>td>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js">script>
<script type="text/javascript">
$(function(){
//为超链接绑定单击事件
$(".delete").click(function () {
var href = $(this).attr("href");
//替换表单的提交请求,并进行提交
$("#deleteFrom").attr("action",href).submit();
return false;//阻止超链接提交
});
})
script>
<%--
利用JavaScript将表单的POST请求转换为DELETE请求
标签:<input type="hidden" name="_method" value="DELETE"/>
的作用是用于SpringMVC的HiddenHttpMethodFilter过滤器使用
--%>
<form id="deleteFrom" action="" method="post">
<input type="hidden" name="_method" value="DELETE"/>
form>
<td><a href="emp/${emp.id}">修改a>td>
@RequestMapping(value = "/emp/{id}", method = RequestMethod.GET)
public String editPre(@PathVariable("id") Integer id, Map<String, Object> map) {
map.put("employee", this.employeeDAO.get(id));
map.put("department", this.departmentDAO.getAll());
return "emp_input";
}
@RequestMapping(value="/emp",method = RequestMethod.PUT)
public String edit(Employee employee) {
this.employeeDAO.add(employee);
return "redirect:/empList";
}
@ModelAttribute
public void getEmployee(@RequestParam(value="id",required = false) Integer id,
Map<String,Object> map){
Employee employee = this.employeeDAO.get(id);
map.put("employee",employee);
}
public Employee get(Integer id) {
return this.employees.get(id);
}
<c:if test="${employee.id == null}">
雇员名称:<form:input path="lastName"/><br/>
c:if>
<c:if test="${employee.id != null}">
<form:hidden path="id"/>
<%--
<input type="hidden" name="_method" value="PUT"/>
该标签用于给SpringMVC的HiddenMethoderFilter 过滤器将POST请求转换为PUT请求
这里不能够使用<form:hidden> 标签进行转换,因为<form:hidden> 默认于模型中的字段所对应
如果使用<form:hidden> 标签作为PUT方法的标识,则SpringMVC会在请求的模型参数中寻找定义的字段
如果没有找到就会报错
--%>
<input type="hidden" name="_method" value="PUT"/>
c:if>