Spring MVC acquires the request object in a non-controller layer implementation

Source: Internet
Author: User

Normally, we'll write a method like this in the controller layer.

@Controller
@RequestMapping (value= "/detail")

   Public class Geturidetailcontroller {

  @SystemControllerLog (description  = "id" ) @RequestMapping (value  = "/{id}", Method={requestmethod.get})  public   Modelandview Getdetailid (@PathVariable (" id ", httpservletrequest request) {Modelandview Modelandview  = new   Modelandview (); 
someting invocation of the Request object method Modelandview.addobject ( "id" ,id); Modelandview.setviewname ( "detail" ); return Modelandview; }}

In essence, the controller is a spring component, so in my request method Getdetailid, you can add a httpservletrequest that obtains the current request.

The Request object contains the user ID session, and so on information

-------------------------------------------------------------

Suppose we're going to write an AOP intercept controller to implement our Pointcut transaction, this time

Requestcontextholder this static class is useful,
HttpServletRequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes ()). GetRequest () ;  

private static final threadlocal<requestattributes> Requestattributesholder =
New Namedthreadlocal<requestattributes> ("Request attributes");//namedthreadlocal thread Local



publicstatic requestattributes getrequestattributes () { = Requestattributesholder.get ();//Get thread Local request object ifnull) {= Inheritablerequestattributesholder.get ();/ / }return attributes; }
I read spring's source code, which gets the request object that is currently requested.

Work first, make up at night,

Spring MVC acquires the request object in a non-controller layer implementation

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.