@RequestMapping annotations

Source: Internet
Author: User

There are many annotations for parameter binding in Spring MVC, all in the Org.springframework.web.bind.annotation package, which can be divided into four classes based on the different parts of the request they handle (mainly on common types)

>> Note on the body part of the request: @RequestParam, @RequestBody

>> handling annotations in reuqest URL section: @PathVariable

>> handling notes in the header section of the request: @RequestHeader, @CookieValue

>> handling attribute types of annotations: @SessionAttibute, @ModelAttribute

@RequestMapping notes:

* * @RequestMapping Although under org.springframework.web.bind.annotation, but strictly speaking, he does not belong to the parameter binding annotations

Developers need to develop appropriate processing methods for each request action within the controller. The org.springframework.web.bind.annotation.RequestMapping annotation type indicates which class or method spring uses to handle the request action, which can be used for a class or method.

@RequestMapping can be used to annotate a controller class, in which case all methods will be mapped to requests relative to the class level, indicating that all requests processed by the controller are mapped to the path indicated by the Value property

= "/user")publicclass  usercontroller{  = "/register")    Public String Register () {        return "register";  }   = "/login")  public  String Login () {        return "Login";     }              }

Request Url:http://localhost:8080/user/register

Http://localhost:8080/user/login

>>1.value Property

@RequestMapping (value = "/hello") public modelandview Hello () {    return  ...;}

The instance uses the Value property of the @requestmapping comment to map the URL to the method, in which case the Hello is mapped to the Hello method, which is handled by the Hello method when the application is accessed using the following URL:

Http://localhost:8080/context/hello

Because the Value property is the default property of the @requestmapping comment, the property name can be omitted if there is only a unique property

@RequestMapping (value = "/hello") @RequestMapping ("/hello")

But if you have more than one property, you must write the Value property name

The value of the Value property can also be an empty string, at which point the method is mapped to the following request URL:

Http://localhost:8080/context

>>2.method Property

This property is used to indicate that the method only handles those HTTP request methods

@RequestMapping (value = "/hello" method = "Requestmethod.post")

The code above indicates that the method only supports post requests

You can also support multiple HTTP request modes at the same time:

@RequestMapping (value = "/hello", method = {Requestmethod.post,requestmethod.get})

If you do not specify the method property value, the request processing method can handle any HTTP request mode

  

@RequestMapping annotations

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.