Get request in Spring MVC

Source: Internet
Author: User

How does spring MVC get the request?
There are the following ways:
method One: Inject request in action
Add HttpServletRequest request directly to the action parameter
For example

/*** * Returns JSON * @param ID * @param rolelevel * @param model * @para  M Request * @param targetview * @return * @throws SecurityException *  @throws nosuchfieldexception * @throws illegalargumentexception * @throws Illegalacce Ssexception * *    @ResponseBody    @RequestMapping(Value ="/{id}/update/json", Method=requestmethod.post) PublicStringjson_update(@PathVariableintId,t rolelevel, Model model,httpservletrequest request,string TargetView)throwsSecurityException, Nosuchfieldexception, IllegalArgumentException, illegalaccessexception {Booleansuccess= Updatecommon (ID, rolelevel, model, request); Map map=NewHashMap ();if(!success) {Map.put (Constant2.login_result_key,false); Map.put (Constant2.response_key_error_message,"Record not found, ID:"+ID);returnHWUTILS.GETJSONP (map); }returnConstant2.response_right_result; }

mode two: Use Requestcontextholder
HttpServletRequest request = ((servletrequestattributes) requestcontextholder.getrequestattributes ()). GetRequest () ;

protectedgetDao() {        if(this.dao==null){            System.out.println("请先执行init(request)");            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();            init(request);        }        returnthis.dao;    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Get request in Spring MVC

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.