The type of the spring processing method return value

Source: Internet
Author: User

The spring MVC processing method supports the following return methods: Modelandview, Model, Modelmap, Map,view, String, void. Each of these will be explained in detail below:

Modeandview:

    @RequestMapping ("/show1" public modelandview Show1 (httpservletrequest request,             throws Exception {        new Modelandview ("/demo2/show");        Mav.addobject ("account", "account-1");         return Mav;    }

The Modelandview construction method allows you to specify the page name returned, or you can jump to the specified page through the Setviewname () method, set the value to be returned using AddObject (), and AddObject () a method with several different parameters. You can default and specify the name of the returned object.

Model:

@RequestMapping ("/demo2/show")    public map<string, string> getmap () {        Map  New Hashmap<string, string>();        Map.put ("Key1", "value-1");        Map.put ("Key2", "value-2");         return map;    }

MAP:

@RequestMapping ("/demo2/show")    public map<string, string> getmap () {        Map  New Hashmap<string, string>();        Map.put ("Key1", "value-1");        Map.put ("Key2", "value-2");         return map;    }

The value can be obtained straight through ${key1} in the JSP page, and Map.put () is equivalent to the Request.setattribute method.

String:

@RequestMapping (value = "/something", method = requestmethod.get) @ResponseBody public String HelloWorld ()  {return "Hello World";}

String Specifies the name of the returned view page, combined with the return address path set and the page name suffix to access.
Note: If the method declares annotation @responsebody, the return value is output directly to the page.

Void:

@RequestMapping ("/welcome")publicvoid Welcomehandler () {}

If the return value is empty, the Response view page corresponds to the access address.

The above content from: http://flyer2010.iteye.com/blog/1294400

Spring Web MVC provides model, map, or modelmap so that we can expose the modeling data needed by the rendering view.

@RequestMapping (value = "/model") public String CreateUser (model model, Map Model2, Modelmap MODEL3) {    Model.addattribute ("A", "a");    Model2.put ("B", "B");    Model3.put ("C", "C");     = = Model2)    ; = = model3)    ; return "Success";}

Although three different types are injected here (model, Map Model2, Modelmap model3), the three are the same object:

Annotationmethodhandleradapter and Requestmappinghandleradapter will use BINDINGAWAREMODELMAP as the implementation of the model object, where our formal parameters (model Model, Map Model2, Modelmap model3) are all the same bindingawaremodelmap instances.

Here is another point we need to take note:

@RequestMapping (value = "/mergemodel") public Modelandview Mergemodel (model model) {    Model.addattribute ("A", "a"); // ① Adding model data    New Modelandview ("Success");    Mv.addobject ("A", "Update"); // ② updating ③ model data at the same name before view rendering    Model.addattribute ("A", "new"); // ③ modifying model data    with the same name at ① // A of the view page will be displayed as "update" instead of "new"    return MV;}

From the code we can conclude that the model data in the return value of the function processing method (such as Modelandview) merges the model data in the function processing method parameters (such as models), but if there is a same name between the two, the model data in the return value overrides the model data in the formal parameter.

The above content from: http://jinnianshilongnian.iteye.com/blog/1698916

The type of the spring processing method return value

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.