如何在SpringMVC中使用REST風格的url


如何在SpringMVC中使用REST風格的url

1.url寫法:

get:/restUrl/{id}

post:/restUrl

delete:/restUrl/{id}

put:/restUrl

2.controller寫法:

1)GET請求的目標方法:

    @RequestMapping(value="/restUrl/{id}", method=RequestMethod.GET)
    public String get(Map<String, Object> map, @PathVariable("id") Integer id){
        Object obj = new Object();
        map.put("obj", obj);
        return "success";   
    }

注意:

1.必須在@RequestMapping注解中添加method=RequestMethod.GET,表明這是一個處理get請求的目標方法

2.通過@PathVariable("id") Integer id注解,將url中的{id}值取出,並賦值給該注解修飾的入參id

2)POST請求的目標方法:

    @RequestMapping(value="/restUrl", method=RequestMethod.POST)
    public String post(Object obj){
        System.out.println(obj);
        return "success";
    }

注意:

1.必須在@RequestMapping注解中添加method=RequestMethod.POST,表明這是一個處理post請求的目標方法

2.post請求的url中不需要寫參數{id}

3)DELETE請求的目標方法:

    @RequestMapping(value="/restUrl/{id}", method=RequestMethod.DELETE)
    public String delete(@PathVariable("id") Integerid){
        System.out.println(id);
        return "success";
    }

 

注意:

1.必須在@RequestMapping注解中添加method=RequestMethod.DELETE,表明這是一個處理delete請求的目標方法

2.url中必須帶有參數{id}

4)PUT請求的目標方法:

    @RequestMapping(value="/restUrl", method=RequestMethod.PUT)
    public String put(Object obj){
        System.out.println(obj);
        return "success";
    }

注意:

1.必須在@RequestMapping注解中添加method=RequestMethod.PUT,表明這是一個處理put請求的目標方法

2.url中不需要帶有參數{id}

3.如果需要使用@ModelAttribute來進行一些修改前的操作(如:先去數據庫查詢一個實體,在使用put目標方法),請參考我的另一篇博客《@ModelAttribute注解的使用詳解》

3.jsp頁面中的鏈接寫法:

1)get請求:

<a href="${pageContext.request.contextPath}/user/restUrl/{id}">get user</a>

注意:

1.這里的{id}不能直接寫{id},而是你要動態賦值的

2)post請求:

<form action="${pageContext.request.contextPath }/restUrl" method="post" >
    name:<input type="text" name="username"><br>
    password:<input type="password" name="password"><br>
    <input type="submit" value="submit">
</form>

注意:

1.因為超鏈接是get請求,所以要使用post風格的url請求必須使用表單

2.必須表明表單的提交方式為method=post

3)delete請求:

<a class="delete_href" href="${pageContext.request.contextPath }/restUrl/{id}">remove</a>
<form id="delete_form" action="" method="post">
    <input type="hidden" name="_method" value="DELETE">
</form>
$(function(){
    $(".delete_href").on("click", function(){var href = $(this).attr("href");
        $("#delete_form").attr("action", href).submit();
        return false;
    })
})

注意:

1.由於超鏈接只能發送get請求,我們需要發送delete請求的話,必須通過一個表單提交,將表單的post請求,轉換成delete請求

2.在表單中添加一個隱藏域<input type="hidden" name="_method" value="DELETE">,能讓表單在提交的時候將請求轉換成delete請求

3.用js實現在點擊超鏈接時,實際上提交的是表單。但是要注意在js之前,請先引入jquery文件

4)put請求:

<form action="${pageContext.request.contextPath }/restUrl" method="post" >
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="id" value="${id }">
    name:<input type="text" name="username"><br>
    password:<input type="password" name="password"><br>
    <input type="submit" value="submit">
</form>

注意:

1.跟delete請求類似,我們需要一個隱藏域<input type="hidden" name="_method" value="PUT">,來將post請求轉換成put請求

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM