Mapping requests with @requestmapping

Source: Internet
Author: User

Dispatcherservlet after a Web request is accepted, the request is sent to the different controller classes of the @controller annotation declaration.

This scheduling process relies on various @requestmapping annotations declared in the Controller class and its handler methods.

In Spring MVC, Web requests are mapped to handlers through one or more @requestmapping annotations declared in the controller class.

The handler mappings match URLs based on relative paths to the context path (Web App context path) and the servlet path (the path mapped to Dispatcherservlet).

The simplest strategy for using @requestmapping annotations is to directly modify the handler method. To do this, each handler method is declared with a @requestmapping annotation that contains a URL pattern.

If the @requestmapping annotation of the method matches the requested Url,dispatcherservlet, the request is sent to this method for processing.

@Controller

Control class

@Autowired

Instance of control inversion generation control class

@RequestMapping ("/member/add")

Method 1

@RequestMapping (value={"/member/remove", "/member/delete"}, Method=requestmethod.get)

Method 2

Another kind

@Controller

@RequestMapping ("/member/*")

Control class

@Autowired

Instance of control inversion generation control class

@RequestMapping ("Add")

Method 1

@RequestMapping (value={"Remove", "delete"}, Method=requestmethod.get)

Method Two

@RequestMapping ("Display/{user}")

Method Three (@RequestParam ("MemberName") string membername, @PathVariable ("user") string user)

@RequestMapping

Method Four

Method Five

For method Three, if the accepted request form is/member/display/jdoe, the method accesses the user variable with the JDoe value

The @requestmapping annotation is used for method four, but the URL value is missing because the class level uses the/member/* URL wildcard, which is performed as an all-in-one method.

Any URL request, such as/MEMBER/ABCDEFG or/member/randomrout, will trigger this method.

The last method has no @requestmapping annotations, meaning that this method is a tool that has no effect on spring MVC.

Mapping requests by HTTP request type

By default, @RequestMapping annotations assume that all inbound requests are of the HTTP get type. However, when an inbound request is a different HTTP type, it is necessary to explicitly specify the type in the @requestmapping annotation.

@RequestMapping (method = Requestmethod.post)

Public String SubmitForm (@ModelAttribute ("member") member member, bindingresult result, model model) {

......

}

Mapping requests with @requestmapping

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.