一、项目代码概览
二、配置文件
1、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <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> <servlet> <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
2、转发配置器
在web.xml中已经取了名字叫: springmvc.xml
<?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/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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.test.springmvc"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- default-servlet-handler 将在 SpringMVC 上下文中定义一个 DefaultServletHttpRequestHandler, 它会对进入 DispatcherServlet 的请求进行筛查, 如果发现是没有经过映射的请求, 就将该请求交由 WEB 应用服务器默认的 Servlet 处理. 如果不是静态资源的请求,才由 DispatcherServlet 继续处理 一般 WEB 应用服务器默认的 Servlet 的名称都是 default. 若所使用的 WEB 服务器的默认 Servlet 名称不是 default,则需要通过 default-servlet-name 属性显式指定 --> <mvc:default-servlet-handler/> <!-- 前面的 mvc:default-servlet-handler 可以解决静态资源了,但是让其他经过handler的请求,又失效了,所以配置了下面的 mvc:annotation-driven 来解决问题 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
三、处理逻辑
1、handler
EmployeeHandler.java
package com.test.springmvc.crud.handlers; import java.util.Map; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.Errors; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.DeleteMapping; 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.test.springmvc.crud.dao.DepartmentDao; import com.test.springmvc.crud.dao.EmployeeDao; import com.test.springmvc.crud.entities.Employee; @Controller public class EmployeeHandler { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; //该控制器中,运行所有方法前的预处理 //【这里主要是为了在修改修改雇员信息中,率先创建pojo并进行赋值, //避免表单因为缺少一些字段导致赋值给pojo的属性一些为空,当然pojo是同一个】 @ModelAttribute public void getEmployee(@RequestParam(value="id",required=false) Integer id, Map<String, Object> map){ if(id != null){ map.put("employee", employeeDao.get(id)); } } //利用pojo更新雇员信息 @RequestMapping(value="/emp",method=RequestMethod.PUT) public String updateEmp(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; } //跳转到更新雇员信息页面 @RequestMapping(value="/emp/{id}",method=RequestMethod.GET) public String doUpdateEmp(@PathVariable("id") Integer id,Map<String, Object> map){ map.put("employee", employeeDao.get(id)); map.put("departments", departmentDao.getDepartments()); return "updateEmp"; } //在所有雇员信息页,进行删除雇员操作 //跳转部分不需要了,本身就是跳转到显示有雇员信息页 @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE) public String deleteEmp(@PathVariable("id") Integer id){ employeeDao.delete(id); System.out.println("hello delete"); return "redirect:/emps"; } //跳转到添加雇员信息页面 @RequestMapping("/doAddEmp") public String doAddEmp(Map<String, Object> map){ //为了提供在添加页面,部门列表 map.put("departments",departmentDao.getDepartments()); //为了提供雇员信息的回显值,添加一个空的雇员信息 map.put("employee", new Employee()); return "addEmp"; } //处理添加雇员请求,并重定向到 @RequestMapping("/emps") @RequestMapping(value="emp", method=RequestMethod.POST) public String addEmp(Employee employee) { employeeDao.save(employee); return "redirect:/emps"; } //跳转到查看所有雇员信息页 @RequestMapping("doGetEmps") public String doGetEmps(){ return "getEmps"; } //查看所有雇员信息,采用默认的get方法,属于crud操作 @RequestMapping("/emps") public String getEmps(Map<String, Object> map){ map.put("employees", employeeDao.getAll()); return "showGetEmps"; } }
2、bean
Employee.java
package com.test.springmvc.crud.entities; import java.util.Date; import javax.validation.constraints.Past; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.NumberFormat; public class Employee { private Integer id; private String lastName; private String email; //1 male, 0 female 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() { // TODO Auto-generated constructor stub } }
Department.java
package com.test.springmvc.crud.entities; public class Department { private Integer id; private String departmentName; public Department() { // TODO Auto-generated constructor stub } public Department(int i, String string) { this.id = i; this.departmentName = string; } 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; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + "]"; } }
3、dao
EmployeeDao.java
package com.test.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.test.springmvc.crud.entities.Department; import com.test.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", "[email protected]", 1, new Department(101, "D-AA"))); employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1, new Department(102, "D-BB"))); employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0, new Department(103, "D-CC"))); employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0, new Department(104, "D-DD"))); employees.put(1005, new Employee(1005, "E-EE", "[email protected]", 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); } }
DepartmentDao.java
package com.test.springmvc.crud.dao; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Repository; import com.test.springmvc.crud.entities.Department; @Repository public class DepartmentDao { private static Map<Integer, Department> departments = null; static{ departments = new HashMap<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); } }
四、jsp页面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="doGetEmps">转到请求所有雇员信息页</a> <br> <a href="doAddEmp">转到添加雇员信息页</a> </body> </html>
getEmps.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/fmt" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="emps">请求所有雇员信息(超链接属于get请求)</a> </body> </html>
showGetEmps.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Get All</title> <!-- SpringMVC 处理静态资源: 1. 为什么会有这样的问题: 优雅的 REST 风格的资源URL 不希望带 .html 或 .do 等后缀 若将 DispatcherServlet 请求映射配置为 /, 则 Spring MVC 将捕获 WEB 容器的所有请求, 包括静态资源的请求, SpringMVC 会将他们当成一个普通请求处理, 因找不到对应处理器将导致错误。 2. 解决: 在 SpringMVC 的配置文件中配置 <mvc:default-servlet-handler/> --> <script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script> <script type="text/javascript"> $(function(){ alert("hello jquery"); $(".delete").click(function(){ var href = $(this).attr("href"); $("form").attr("action", href).submit(); return false; }); }) </script> </head> <body> <form action="" method="POST"> <input type="hidden" name="_method" value="DELETE"/> </form> <c:if test="${empty requestScope.employees}"> 没有任何员工信息 </c:if> <c:if test="${ !empty requestScope.employees }"> <table border="1" cellpadding="10" cellspacing="0"> <tr> <th>ID</th> <th>LastName</th> <th>Email</th> <th>Gender</th> <th>Department</th> <th>Edit</th> <th>Delete</th> </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? "Female":"Male"}</td> <td>${emp.department.departmentName}</td> <td><a href="emp/${emp.id}">Edit</a></td> <td><a class="delete" href="emp/${emp.id}">Delete</a></td> </tr> </c:forEach> </table> </c:if> </body> </html>
addEmp.jsp
<%@ page import="java.util.HashMap"%> <%@ page import="java.util.Map"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 1. WHY 使用 form 标签呢 ? 可以更快速的开发出表单页面, 而且可以更方便的进行表单值的回显 2. 注意: 可以通过 modelAttribute 属性指定绑定的模型属性, 若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean 如果该属性值也不存在,则会发生错误。 大白话:form:form 表单标签,在springmvc中是一定要回显的,哪怕是第一次也要支持回显。 所以第一次请求时,form:form 表单标签会从 request域中的 关键字中查找 默认值是command(如果 form:form标签 指定modelAttribute="XXX",则按照XXX关键字来) 查看属性值,用来填充表单值。 --> <!-- 表单 路径也可以是 action="emp" --> <form:form action="${pageContext.request.contextPath }/emp" method="post" modelAttribute="employee"> <!-- path标签对应 html表单标签的 name属性值,支持级联 如 path="department.id" --> LastName:<form:input path="lastName" /> <br> Email:<form:input path="email" /> <% Map<String, String> genders = new HashMap(); genders.put("1", "Male"); genders.put("0", "Female"); request.setAttribute("genders", genders); %> <br> Gender:<form:radiobuttons path="gender" items="${genders}" /> <br> Department:<form:select path="department.id" items="${departments}" itemLabel="departmentName" itemValue="id"></form:select> <br> <input type="submit" value="submit" /> </form:form> </body> </html>
updateEmp.jsp
<%@ page import="java.util.HashMap"%> <%@ page import="java.util.Map"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!-- 1. WHY 使用 form 标签呢 ? 可以更快速的开发出表单页面, 而且可以更方便的进行表单值的回显 2. 注意: 可以通过 modelAttribute 属性指定绑定的模型属性, 若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean 如果该属性值也不存在,则会发生错误。 大白话:form:form 表单标签,在springmvc中是一定要回显的,哪怕是第一次也要支持回显。 所以第一次请求时,form:form 表单标签会从 request域中的 关键字中查找 默认值是command(如果 form:form标签 指定modelAttribute="XXX",则按照XXX关键字来) 查看属性值,用来填充表单值。 --> <!-- 表单 路径不可以是 action="emp" ,因为是 相对路径 只会在最后一个 目录 替换文件 比如 http://localhost:8080/springmvc-2/emp/1003 提交后 变成 http://localhost:8080/springmvc-2/emp/emp --> <form:form action="${pageContext.request.contextPath }/emp" method="post" modelAttribute="employee"> <!-- pageContext.request.contextPath 如果项目放在根目录,就返回 -> "" (空字符串) 如果不是就返回-> "/项目名" --> <c:if test="${employee.id == null }"> <!-- 这个lastName的属性是不会出现的, 这里用 if条件是因为 updateEmp.jsp 和 addEmp.jsp 本来混在一起用if 作为判断区分的 --> <!-- path标签对应 html表单标签的 name属性值,支持级联 如 path="department.id" --> LastName: <form:input path="lastName"/> </c:if> <br> Email:<form:input path="email" /> <form:hidden path="id"/> <input type="hidden" name="_method" value="PUT"/> <%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%> <%-- <form:hidden path="_method" value="PUT"/> --%> <% Map<String, String> genders = new HashMap <String, String>() ; genders.put("1", "Male"); genders.put("0", "Female"); request.setAttribute("genders", genders); %> <br> Gender:<form:radiobuttons path="gender" items="${genders}" /> <br> Department:<form:select path="department.id" items="${departments}" itemLabel="departmentName" itemValue="id"></form:select> <br> <input type="submit" value="submit" /> </form:form> </body> </html>