web--8.restful-3.对Spring MVC支持

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26553781/article/details/79830061

参看博客:https://blog.csdn.net/chenxiaochan/article/details/73716617

https://blog.csdn.net/chenxiaochan/article/details/52988323

 

强调一点,不是不能用其他方式和参数,只是规范而已

 

1      显示—get

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

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

注意:

    用EL接受参数时,参数一致,最好写全称

2      crud

2.1  查询(oid)—get

RESTful方式实现商品信息查询,返回json数据,使用RESTful风格开发的接口,根据id查询商品,接口地址是:http://127.0.0.1/item/1

 

我们需要从url上获取商品id,步骤如下:

1.使用注解@RequestMapping("item/{id}")声明请求的url

{xxx}叫做占位符,请求的URL可以是“item /1”或“item/2

 

2.使用(@PathVariable()Integer id)获取url上的数据

注意两个区别

1. @PathVariable是获取url上数据的。@RequestParam获取请求参数的(包括post表单提交)

2. 如果加上@ResponseBody注解,就不会走视图解析器,不会返回页面,目前返回的json数据。如果不加,就走视图解析器,返回页面

 

2.2  添加post(json)并显示所有get

 

2.3  删除员工(oid)—delete

<td><a class="delete"href="emp/${emp.id}">Delete</a></td>

<form action="" method="POST">
       <input type="hidden"name="_method" value="DELETE"/>
    </form>
 
<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();        
           returnfalse;
       });
    })
</script>
 
//删除操作
@RequestMapping(value="/emp/{id}" , method=RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
    employeeDao.delete(id);
    return"redirect:/emps";
}
 
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>

2.4  修改—put

<c:if test="${employee.id== null }">
           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 这个属性 --%>
       </c:if>

<td><a href="emp/${emp.id}">Edit</a></td>
 
    //修改操作
    @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";
    }

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

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

 

 

 

 

 


猜你喜欢

转载自blog.csdn.net/qq_26553781/article/details/79830061