码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC----RESTFUL_CRUD_修改操作

时间:2018-10-04 10:18:50      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:lan   why   body   str   信息   html4   文件中   orm   BMI   

1.注意:

<!--    对于_method不能使用form:hidden标签,因为modelAttribute对应的bean中没有_method这个属性,应该使用input标签 -->
 <!-- <form:hidden path="_method" value="PUT"/>     -->

2.代码:

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>
<!-- SpringMVC处理静态资源
    1.为什么会有这种问题?
        优雅的REST风格的资源URL不希望带.html或.do等后缀;
        若将DispatcherServlet请求映射为/,则springMVC将捕获WEB容器的所有请求,包括静态资源(图片、css、js)的请求,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() {
        $(".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>
                    <!-- 默认超链接是一个GET请求,但是使用HiddenHttpMethodFilter后POST请求才能转换成DELETE请求(借助js实现) -->
                    <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.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>
    <!-- why使用form标签
            1.可以更快速的开发出表单页面,而且可以更加方便的进行表单值的回显;
     -->

    <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" />
        </c:if>

        <br>    
        email:<form:input path="email" />
        <br>
        <%
            Map<String, String> genders = new HashMap();
                genders.put("1", "male");
                genders.put("0", "Female");

                request.setAttribute("genders", genders);
        %>
        <!-- delimiter="<br>" 按照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>


java代码:

package com.yikuan.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.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.yikuan.springmvc.crud.dao.DepartmentDao;
import com.yikuan.springmvc.crud.dao.EmployeeDao;
import com.yikuan.springmvc.crud.entities.Employee;

@Controller
public class EmployeeHandler {
    
    @Autowired
    private EmployeeDao employeeDao;
    
    @Autowired
    private DepartmentDao departmentDao;
    
    @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);    //此时修改完后,lastName为null@ModelAttribute;
        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));
        /*employee必须和input.jsp中表单页面的modelAttribute="employee"值一样*/
        map.put("departments", departmentDao.getDepartments());
        return "input";
    }
    
    
    @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return "redirect:/emps";
    }
    
    
    @RequestMapping(value="/emp",method=RequestMethod.POST)
    public String save(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }
    
    
    @RequestMapping(value="emp",method=RequestMethod.GET)
    public String input(Map<String,Object> map){
        map.put("departments", departmentDao.getDepartments());
        map.put("employee", new Employee());
        return "input";
    }
    
    @RequestMapping("/emps")
    public String list(Map<String, Object> map){
        map.put("employees", employeeDao.getAll());
        return "list";
    }
}

 

SpringMVC----RESTFUL_CRUD_修改操作

标签:lan   why   body   str   信息   html4   文件中   orm   BMI   

原文地址:https://www.cnblogs.com/yikuan-919/p/9741153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!