Spring Learning Springmvc return type selection and order of model,modelmap.request,session values in SPRINGMVC

Source: Internet
Author: User

The

Spring mvc processing method supports the following return methods: Modelandview, model, modelmap, map,view, string, void. The details are described below:
modelandview
java code         @RequestMapping (" /show1 ")    Publicmodelandview show1 (httpservletrequest request,               httpservletresponse response)  throwsException  {          modelandview mav = newmodelandview ("/demo2/show");          mav.addobject ("account",  "account"  -1 ");          returnmav;      }   


You can specify the page name returned by using the Modelandview constructor, or you can jump to the specified page   by using the Setviewname () method,
use AddObject () to set the value to return, AddObject () There are several different parameters that can be used by default and specify the name of the returned object. The
calls the AddObject () method to set the value to a class property named Modelmap, Modelmap is a subclass of Linkedhashmap,
see the class specifically. The

model  is an interface,  its implementation class is Extendedmodelmap and inherits the Modelmap class.

map  
Java code    @RequestMapping ("/demo2/show")         publicmap<string, string> getmap ()  {            map<string, string> map = newhashmap<string, string > ();           map.put ("Key1",  "value-1");            map.put ("Key2",  "value-2");            returnmap;       }  


The value can be obtained directly through ${key1} in the JSP page, Map.put () is equivalent to the Request.setattribute method.
When you write an example, you find that the key value includes-. There will be problems.

View can be returned to the PDF Excel, etc., for the time being not fully understood.

STRING Specifies the returned view page name, which can be accessed by combining the set return address path plus the page name suffix.
Note: If the method declares the annotation @responsebody, it will output the return value directly to the page.
For example:
Java code @RequestMapping (value = "/something", method = Requestmethod.get) @ResponseBody publicstring HelloWorld () {   Return to "Hello World"; }


The results above will write the text "Hello World" directly to the HTTP response stream.
Java code @RequestMapping ("/welcome") publicstring Welcomehandler () {return "center"; }


The corresponding logical view is named "Center", url= prefix prefix + view name +suffix suffix.

void if the return value is NULL, the response's view page corresponds to the access address
Java code @RequestMapping ("/welcome") Publicvoidwelcomehandler () {}


This example corresponds to the logical view named "Welcome".

Summary:
1. Using String as the return value type of the request processing method is a more general method, so that the returned logical view name does not bind to the request URL, and has great flexibility, and the model data can be controlled by Modelmap.
2. When using Void,map,model, return the corresponding logical view name the true URL is: prefix prefix + view name +suffix suffix.
3. Returning the view name using String,modelandview can be unbound from the requested URL, Modelandview can set the view name returned.

Model model,httpservletrequest request, modelmap map declare variable

request.getsession (). SetAttribute ("Test",  "haiwei2session");
Request.setattribute ("Test",  "Haiwei1request");  
Map.addattribute ("Test",  Haiweimodelmap ");
Model.addattribute ("Test",  "Haiweimodel");

I pass ${test} This way to take the value, the priority to take model and MODELMAP, model and Modelmap is the same thing, who finally assigned to take who, and then the request, and finally from the session to get

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.