Four REST-style request methods of SpringMVC, springmvcrest

Source: Internet
Author: User

Four REST-style request methods of SpringMVC, springmvcrest

 I,In the HTTP protocol, four verbs indicate the operation method:GET, POST, PUT, DELETE.

· They correspond to four basic operations:

1. GET ===== obtain resources
2. POST ===== create a resource
3. PUT ====== update Resources
4. DELETE === DELETE a resource

  

  Ii. REST: Representational State Transfer. (Resource) performance Layer status conversion. Is currently the most popular Internet software architecture. It has a clear structure, complies with standards, is easy to understand, and is easy to expand, so it is being adopted by more and more websites.

We can use the rest-style placeholder method and the @ PathVariable annotation to assign the placeholder value to the call method parameter,Implementation result:

      • /A path/1 http get: GET a piece of data with id = 1
      • /A path/1 http delete: DELETE a data entry with id = 1
      • /A path/1 http put: update a piece of data with id = 1
      • /A path for http post: Add a new data entry

    

    Implementation Method (four REST-style request methods ):

   The method parameter in the @ RequestMapping ing request is used to call four request methods. The following is the sample code.

      GET request:

  1 @RequestMapping(value="/student",method=RequestMethod.GET)  2     public ModelAndView toAddPage(){  3         ModelAndView mView=new ModelAndView();  4         mView.addObject("employee",new Employee());  5         mView.setViewName("add-stu");  6         mView.addObject("departments", departmentDao.getDepartments());  7         return mView;  8     }

    

POST request:

  1 @RequestMapping(value="/student",method=RequestMethod.POST)  2     public String addStu(Employee employee){  3         employeeDao.save(employee);  4         return "redirect:/show" ;  5     }

 

DELETE request:

  1 @RequestMapping(value="/student/{id}",method=RequestMethod.DELETE)  2     public String deleteStu(@PathVariable(value="id") Integer id){  3         employeeDao.delete(id);  4         return "redirect:/show" ;  5     }

 

PUT request:

  1 @RequestMapping(value="/student",method=RequestMethod.PUT)  2     public String Update(@RequestParam(value="id")Integer id,Employee employee){  3         employeeDao.save(employee);  4         return "redirect:/show" ;  5     }

 

 

 3. Convert POST requests to put requests and delele requests

    1. Configure the HiddenHttpMethodFilter in the web. xml file:

1 <! -- HiddenHttpMethodFilter can convert POST requests to put requests and delete requests! --> 2 <filter> 3 <filter-name> hiddenHttpMethodFilter </filter-name> 4 <filter-class> org. springframework. web. filter. hiddenHttpMethodFilter </filter-class> 5 </filter> 6 <filter-mapping> 7 <filter-name> hiddenHttpMethodFilter </filter-name> 8 <url-pattern>/* </url-pattern> 9 </filter-mapping>

 

    

2. In the form field, a parameter with the name value as _ method and the value as put or delete is required, as shown below:

1   <form action="" method="post">2         <input type="hidden" name="_method" value="delete">3     </form>

 

1 <form: form action = "$ {pageContext. request. contextPath}/student "method =" post "modelAttribute =" employee "> 2 <c: if test =" $ {empty employee. id} "> 3 name: <form: input path =" lastName "/> <br> 4 </c: if> 5 <c: if test =" $ {! Empty employee. id} "> 6 name: <form: input path =" lastName "readonly =" true "/> <br> 7 <form: hidden path = "id"/> 8 <input type = "hidden" name = "_ method" value = "put"> 9 </c: if> 10 Email: <form: input path = "email"/> <br> 11 <% 12 Map <String, Object> map = new HashMap <String, Object> (); 13 map. put ("1", "Male"); 14 map. put ("0", "Female"); 15 request. setAttribute ("genders", map); 16%> 17 gender: <form: radiobuttons path = "gender" items = "$ {genders}"/> <br> Department 18: <form: select path = "department. id "items =" $ {ments} "itemValue =" id "itemLabel =" departmentName "> </form: select> <br> 19 <input type = "submit" value = "submit"> 20 </form: form>

Finally, you can call it at the Controller layer. Find the matching function based on the value of @ RequestMapping, the carried parameters, and the request method.

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.