Get request in SPRINGMVC

Source: Internet
Author: User

Controller in addition parameter
@Controller  Public class TestController {    @RequestMapping ("/test")    publicvoid  Test ( HttpServletRequest request) {        ...    }}

After acquiring the request object in the controller, if you want to use the request object in other methods (such as service methods, tool class methods, and so on), you need to pass the request object as a parameter when calling these methods

At this point the Request object is a method parameter, equivalent to a local variable , which is undoubtedly thread-safe.

Automatic injection
@Controller  Public class testcontroller{         @Autowired    private// Auto-inject request         @ Requestmapping ("/test")    publicvoidthrows  interruptedexception{...    }

In this way, when the bean (TestController of this example) is initialized, spring does not inject a request object, but instead injects a proxy, and when the request object needs to be used in the bean, Gets the request object from the proxy. The request is actually a proxy: the implementation of the proxy is described in the internal class objectfactorydelegatinginvocationhandler of Autowireutils.

When invoking the method of request, it is actually called by method of the object generated by Objectfactory.getobject (); Objectfactory.getobject () The generated object is the real request object.

The type of objectfactory is the inner class of Webapplicationcontextutils requestobjectfactory and requestobjectfactory to get the request object needs to first call the Currentrequestattributes () method to get the Requestattributes object, The core code that generates the Requestattributes object is in class Requestcontextholder, and the Requestattributes object that is generated is the thread local variable (ThreadLocal). Therefore, the request object is also a thread-local variable, which guarantees thread security for the request object.

Automatic injection in base class
 Public class Basecontroller {    @Autowired    protected  httpservletrequest request;     } @Controllerpublicclassextends  basecontroller {}

Instead of repeating the request in different controllers as compared to Method 2, the method is no longer useful when the controller needs to inherit other classes, given that Java only allows inheriting one base class.

Call manually
@Controller  Public class TestController {    @RequestMapping ("/test")    publicvoidthrows interruptedexception {        = ((servletrequestattributes) ( Requestcontextholder.currentrequestattributes ()). Getrequest ();            .......    }}

The implementation of automatic injection is similar to the principle of manual method invocation. Therefore, this method is also thread-safe.

Pros: can be obtained directly from non-beans. Cons: The code is cumbersome if you use more places, so you can use it with other methods.

@ModelAttribute method
  @Controller  public  class   TestController { private  Span style= "color: #000000;" > HttpServletRequest request; The thread is unsafe here @ModelAttribute  public  void
     Bindrequest (HttpServletRequest request) { this . Request = request; Request thread Safety here} @RequestMapping ( "/test" )  public  void  Test () throws   Interruptedexception {...}}  

@ModelAttribute annotations are used when modifying a method in a controller that is performed before each @requestmapping method in the controller executes. The function of Bindrequest () is to assign a value to the Request object before test () executes. Although the parameter request in Bindrequest () itself is thread-safe, because TestController is a singleton, request is not guaranteed to be thread-safe as a domain of TestController.

Get request in SPRINGMVC

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.