省略了Service层,为了教学方便
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 |
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">
class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
|
|
/WEB-INF/views/list.jsp
index.jsp
|
|
package com.sirius.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.sirius.springmvc.crud.entities.Department; import com.sirius.springmvc.crud.entities.Employee;
@Repository public class EmployeeDao {
private static Map
@Autowired private DepartmentDao departmentDao;
static{ employees = new HashMap
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 return employees.values(); }
public Employee get(Integer id){ return employees.get(id); }
public void delete(Integer id){ employees.remove(id); } } |
package com.sirius.springmvc.crud.dao;
import java.util.Collection; import java.util.HashMap; import java.util.Map;
import org.springframework.stereotype.Repository;
import com.sirius.springmvc.crud.entities.Department;
@Repository public class DepartmentDao {
private static Map
static{ departments = new LinkedHashMap
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 return departments.values(); }
public Department getDepartment(Integer id){ return departments.get(id); }
} |
package com.sirius.springmvc.crud.handlers;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;
import com.sirius.springmvc.crud.dao.EmployeeDao;
@Controller public class EmployeeHandler {
@Autowired private EmployeeDao employeeDao ;
@RequestMapping("/empList") public String empList(Map map.put("empList", employeeDao.getAll()); //默认存放到request域中 return "list"; } } |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
对不起,没有找到任何员工!
|
@RequestMapping(value="/empInput",method=RequestMethod.GET) public String empInput(Map map.put("deptList", departmentDao.getDepartments()); //解决错误:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute Employee employee = new Employee(); //map.put("command", employee); map.put("employee", employee); return "add"; } |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
LastName : Email : <% Map map.put("1", "Male"); map.put("0","Female"); request.setAttribute("genders", map); %> Gender : DeptName : items="${deptList }" itemLabel="departmentName" itemValue="id">
|
HTTP Status 500 -
type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/views/add.jsp at line 18 15: ② 表单回显 Stacktrace: root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
"http://www.w3.org/TR/html4/loose.dtd">
LastName : Email : <% Map map.put("1", "Male"); map.put("0","Female"); request.setAttribute("genders", map); %> Gender : DeptName : items="${deptList }" itemLabel="departmentName" itemValue="id">
|
@Controller public class EmployeeHandler { @RequestMapping(value="/empAdd",method=RequestMethod.POST) public String empAdd(Employee employee){ employeeDao.save(employee); return "redirect:/empList"; } } |
Delete |
@RequestMapping(value="/empDelete/{id}" ,method=RequestMethod.DELETE) public String empDelete(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/empList"; } |
/scripts/jquery-1.9.1.min.js
警告: No mapping found for HTTP request with URI [/SpringMVC_03_RESTFul_CRUD/scripts/jquery-1.9.1.min.js] in DispatcherServlet with name 'springDispatcherServlet' |
|
SpringMVC 处理静态资源:
优雅的 REST 风格的资源URL 不希望带 .html 或 .do 等后缀
若将 DispatcherServlet 请求映射配置为 /,
则 Spring MVC 将捕获 WEB 容器的所有请求, 包括静态资源的请求, SpringMVC 会将他们当成一个普通请求处理,
因找不到对应处理器将导致错误。
需要配置
|
class="delete" href="empDelete/${emp.id }">Delete |
|
$(function(){ $(".delete").click(function(){ var href = $(this).attr("href"); $("form").attr("action",href).submit(); return false ; }); }); |
Edit |
//修改员工 - 表单回显 @RequestMapping(value="/empEdit/{id}",method=RequestMethod.GET) public String empEdit(@PathVariable("id") Integer id,Map map.put("employee", employeeDao.get(id)); map.put("deptList",departmentDao.getDepartments()); return "edit"; } |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
"http://www.w3.org/TR/html4/loose.dtd">
<form:form action="${pageContext.request.contextPath }/empUpdate" method="POST" modelAttribute="employee"> <%-- LastName : --%> hidden" name="_method" value="PUT"> <%-- 这里不要使用form:hidden标签,否则会报错。 Spring的隐含标签,没有value属性,同时,path指定的值,在模型对象中也没有这个属性(_method),所以回显时会报错。 org.springframework.beans.NotReadablePropertyException: Invalid property '_method' of bean class [com.sirius.springmvc.crud.entities.Employee]: Bean property '_method' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter? --%>
Email : <% Map map.put("1", "Male"); map.put("0","Female"); request.setAttribute("genders", map); %> Gender : DeptName : items="${deptList }" itemLabel="departmentName" itemValue="id"> form:form> |
@RequestMapping(value="/empUpdate",method=RequestMethod.PUT) public String empUpdate(Employee employee){ employeeDao.save(employee); //用户名称不需要修改,会被置空 return "redirect:/empList"; } @ModelAttribute public void getEmployee( @RequestParam(value="id",required=false) Integer id,Map if(id!=null){ map.put("employee", employeeDao.get(id)); } } |