SpringMVC笔记(四)

RESTful SpringMVC CRUD

1. 需求分析

a. 显示所有员工信息

  • -URI emps
  • -请求方式 GET
  • -显示效果

b. 添加所有员工信息

-显示添加

  • URL emp
  • 请求方式 GET
  • 显示效果

-添加员工信息

  • URL emp
  • 请求方式 POST
  • 显示效果:完成添加,重定向到 list.jsp

c. 删除操作

  • URL : emp/{id}
  • 请求方式 : DELETE
  • 删除后效果 : 对应记录从数据表中删除

d. 修改操作

  • 显示修改页面:
    • URL:emp/{id}
    • 请求方式 POST
    • 删除后效果:对应记录从数据表中删除
  • 修改员工信息
    • URL:emp
    • 请求方式 POST
    • 显示效果:完成修改,重定向到 list 页面

e.相关的类

  • 实体类:Employee、Department
  • Hander:EmployeeHandler
  • Dao:EmployeeDao、DepartmentDao

f. 相关的页面

  • list.jsp
  • input.jsp
  • edit.jsp

2. 需求分析显示所有员工信息

3. 添加操作&表单标签

4. 修改操作

导入的 jar 包

commons-logging-1.1.1.jar
jstl-1.2_1.jar
spring-aop-4.0.2.RELEASE.jar
spring-beans-4.0.2.RELEASE.jar
spring-context-4.0.2.RELEASE.jar
spring-core-4.0.2.RELEASE.jar
spring-expression-4.0.2.RELEASE.jar
spring-web-4.0.2.RELEASE.jar
spring-webmvc-4.0.2.RELEASE.jar

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_3_0.xsd" id="WebApp_ID" version="3.0">

    <!-- 配置Spring MVC 的 DispatcherServlet -->
    <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>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 配置:把 POST 请求转为 DELETE、PUT 请求 -->
    <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-app>

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/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">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.anqi.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:annotation-driven></mvc:annotation-driven>
</beans>

index.jsp

<a href="emps">List All Employees</a>

list.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>Insert title here</title>
<!-- 
    Spring MVC 处理静态资源:
    1. 为什么会有这样的问题:
    优雅的 REST 风格的资源 URL 不希望带 .html 或者 .do 等后缀
    若将 DispatcherServlet 请求映射配置为 /,
    则 Spring MVC 将捕获 WEB 容器的所有请求,包括静态资源的请求,Spring MVC 会把它们当成一个普通请求来处理
    则找不到对应处理器将导致错误。
    2. 解决:在 Spring MVC 的配置文件配置 <mvc:default-servlet-handler/>
 -->
<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");
            $("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>

    <br><br>

    <a href="emp">Add New Employee</a>
</body>
</html>

input.jsp

<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@ 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>

    <%
        Map<String,String> genders = new HashMap();
        genders.put("1", "Male");
        genders.put("0", "Female");
        request.setAttribute("genders", genders);
    %>

    <!-- 
        1. 为什么使用 form 标签呢?
        可以更快速的开发表单页面,而且可以更方便的进行表单值的回显
        2. 注意
        可以通过 modelAttribute 属性指定绑定的模型属性
        若没有指定该属性,则默认从 request 域对象中读取 command 的表单 bean
        如果该属性值不存在,则会发生错误
     -->
    <form:form action="${pageContext.request.contextPath }/emp" method="POST" modelAttribute="employee">

        <c:if test="${employee.id == null }">
            <!-- path 属性对应 html 表单标签的 name 属性值 -->
            LastName: <form:input path="lastName"/>
        </c:if>
        <c:if test="${employee.id != null }">
            <form:hidden path="id"/>
            <input type="hidden" name="_method" value="PUT"/>
            <%-- 对于 _method 不能使用 form:hidden 标签, 因为 modelAttribute 对应的 bean 中没有 _method 这个属性 --%>
            <%-- 
            <form:hidden path="_method" value="PUT"/>
            --%>
        </c:if>

        Email:<form:input path="email"/>
        <br>
        Gender:<form:radiobuttons path="gender" items="${genders }" delimiter="<br>"/>
        <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>

EmployeeHandler.java

@Controller
public class EmployeeHandler {

    @Autowired
    EmployeeDao employeeDao;  

    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Map<String, Object> map) {
        map.put("employees", employeeDao.getAll());     
        return "list";
    }

    @RequestMapping(value = "/emp", method = RequestMethod.GET)
    public String input(Map<String, Object> map) {
        map.put("departments", departmentDao.getDepartments());
        //用于表单的 modelAttribute 绑定使用
        map.put("employee", new Employee());
        return "input";
    }

    @RequestMapping(value = "/emp", method = RequestMethod.POST)
    public String save(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @RequestMapping(value = "/emp/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id) {
        employeeDao.delete(id);
        return "redirect:/emps";
    }

    @RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
    public String input(@PathVariable("id") Integer id, Map<String, Object> map){
        map.put("employee", employeeDao.get(id));
        map.put("departments", departmentDao.getDepartments());
        return "input";
    }

    @ModelAttribute
    public void getEmployee(@RequestParam(value="id",required=false) Integer id,
            Map<String, Object> map){
        if(id != null){
            map.put("employee", employeeDao.get(id));
        }
    }

    @RequestMapping(value="/emp", method=RequestMethod.PUT)
    public String update(Employee employee){
        employeeDao.save(employee);

        return "redirect:/emps";
    }
}

DepartmentDao

@Repository
public class DepartmentDao {

    private static Map<Integer, Department> departments = null;

    static {
        departments = new HashMap<>();

        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);
    }


}

EmployeeDao

@Repository
public class EmployeeDao {

    private static Map<Integer, Employee> employees = null;

    @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<Employee> getAll(){
        return employees.values();
    }

    public Employee get(Integer id){
        return employees.get(id);
    }

    public void delete(Integer id){
        employees.remove(id);
    }
}
public class Department {

    private Integer id;
    private String departmentName;
    //setter.getter.toString.Structor
}
public class Employee {

    private Integer id;

    private String lastName;

    private String email;
    //1 male, 0 female
    private Integer gender;

    private Department department;
    //setter.getter.toString.Structot
}

5. SpringMVC表单标签&处理静态资源

❤ 使用Spring 的表单标签

  • 通过SpringMVC的表单标签可以实现将模型数据中的属性和HTML 表单元素相绑定,以实现表单数据更便捷编辑和表单值的回显

❤ form 标签

  • 一般情况下,通过GET 请求获取表单页面,而通过POST 请求提交表单页面,因此获取表单页面和提交表单页面的 URL 是相同的。只要满足该最佳条件的契约,form:form 标签就无需通过action 属性指定表单提交的URL
  • 可以通过 modelAttribute 属性指定绑定的模型属性,若没有指定该属性,则默认从request 域对象中读取command的表单bean,如果该属性值也不存在,则会发生错误。

❤ 表单标签

  • SpringMVC提供了多个表单组件标签,如、等,用以绑定表单字段的属性值,它们的共有属性如下:
  • – path:表单字段,对应html 元素的name 属性,支持级联属性

    – htmlEscape:是否对表单值的HTML 特殊字符进行转换,默认值为true
    
    – cssClass:表单组件对应的CSS 样式类名
    
    – cssErrorClass:表单组件的数据存在错误时,采取的CSS 样式
    
  • form:input、form:password、form:hidden、form:textarea:对应HTML 表单text、password、hidden、textarea标签

  • form:radiobutton:单选框组件标签,当表单bean 对应的属性值和value 值相等时,单选框被选中
  • form:radiobuttons:单选框组标签,用于构造多个单选框
    – items:可以是一个List、String[]或Map
    – itemValue:指定radio 的value 值。可以是集合中bean 的一个属性值
    – itemLabel:指定radio 的label 值
    – delimiter:多个单选框可以通过delimiter 指定分隔符 delimiter=”br>”

  • form:checkbox:复选框组件。用于构造单个复选框

  • form:checkboxs:用于构造多个复选框。使用方式同form:radiobuttons标签
  • form:select:用于构造下拉框组件。使用方式同form:radiobuttons标签
  • form:option:下拉框选项组件标签。使用方式同form:radiobuttons标签
  • form:errors:显示表单组件或数据校验所对应的错误
<form:errorspath=“*” />:显示表单所有的错误
– <form:errorspath=user*” />:显示所有以user 为前缀的属性对应的错误
– <form:errorspath=username”/>:显示特定表单对象属性的错误

❤ 处理静态资源

  • 优雅的REST 风格的资源URL 不希望带.html 或.do 等后缀
  • 若将DispatcherServlet请求映射配置为/,则Spring MVC 将捕获WEB 容器的所有请求,包括静态资源的请求,SpringMVC会将他们当成一个普通请求处理,因找不到对应处理器将导致错误。
  • 可以在SpringMVC的配置文件中配置
–  <mvc:default-servlet-handler/> 将在>SpringMVC上下文中定义一个
DefaultServletHttpRequestHandler,它会对进入DispatcherServlet的请求进行筛查,
如果发现是没有经过映射的请求,就将该请求交由WEB 应用服务器默认的Servlet 处理,如果不
是静态资源的请求,才由DispatcherServlet继续处理

–  一般WEB 应用服务器默认的Servlet 的名称都是default。若所使用的–WEB 服务器的默Servlet 名称不是default,则需要通过default-servlet-name 属性显式指定

猜你喜欢

转载自blog.csdn.net/baidu_37181928/article/details/80313940