How to Use REST-style URLs in SpringMVC, springmvcrest

Source: Internet
Author: User
Tags form post

How to Use REST-style URLs in SpringMVC, springmvcrest
How to Use REST url1.url in SpringMVC:

Get:/restUrl/{id}

Post:/restUrl

Delete:/restUrl/{id}

Put:/restUrl

2. controller Syntax: 1) target method of the GET request:
    @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";       }

Note:

1. method = RequestMethod must be added to the @ RequestMapping annotation. GET indicates that this is a target method for processing get requests 2. use the @ PathVariable ("id") Integer id annotation to retrieve the {id} value in the url and assign the value to the input parameter id2 modified by the annotation) Target method of the post request:
    @RequestMapping(value="/restUrl", method=RequestMethod.POST)    public String post(Object obj){        System.out.println(obj);        return "success";    }

Note:

1. method = RequestMethod must be added to the @ RequestMapping annotation. POST indicates that this is a target method for processing post requests 2. the parameter {id} is not required in the url of the post request. 3) Target method of the delete request:
    @RequestMapping(value="/restUrl/{id}", method=RequestMethod.DELETE)    public String delete(@PathVariable("id") Integerid){        System.out.println(id);        return "success";    }

 

Note:

1. method = RequestMethod must be added to the @ RequestMapping annotation. DELETE, indicating that this is a target method for processing the delete request 2. the url must contain the parameter {id} 4) Target method of the put request:
    @RequestMapping(value="/restUrl", method=RequestMethod.PUT)    public String put(Object obj){        System.out.println(obj);        return "success";    }
Note:1. method = RequestMethod must be added to the @ RequestMapping annotation. PUT, indicating that this is a target method for processing put requests 2. the url does not need to contain the parameter {id} 3. if you need to use @ ModelAttribute to perform some operations before modification (for example, to query an object in the database, use the put target method ), please refer to my other blog "@ ModelAttribute annotation Usage Details" 3. link syntax on the jsp page: 1) get request:
<a href="${pageContext.request.contextPath}/user/restUrl/{id}">get user</a>

Note:

1. Here {id} cannot be written directly to {id}, but you want to assign a value dynamically. 2) post request:
<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>

Note:

1. Because hyperlinks are get requests, Form 2 must be used for post-style url requests. The form submission method must be method = post3) delete request:
<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;    })})

Note:

1. since Hyperlinks can only send get requests, if we need to send a delete request, we must submit a form to convert the form post request to a delete request. add a hidden field <input type = "hidden" name = "_ method" value = "DELETE"> to the form, converts a form request to a delete request when it is submitted. when clicking a hyperlink using js, the form is actually submitted. However, before using JavaScript, introduce the jquery file 4) put request:
<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>

Note:

1. Similar to the delete request, we need a hidden field <input type = "hidden" name = "_ method" value = "PUT"> to convert the post request to a put request.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.