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. Each of these will be explained in detail below:
Modelandview

Java code
    1.    
    2. @RequestMapping (
    3. public modelandview show1 ( HttpServletRequest request, 
    4.            HttpServletResponse response)  throws exception { 
    5.         modelandview mav = new modelandview ( "/demo2/show")  
    6.        Mav.addobject (
    7.        RETURN MAV; 
    8.   } 


The Modelandview construction method allows you to specify the name of the page returned, or you can jump to the specified page by using the Setviewname () method.
Using AddObject () sets the value that needs to be returned, AddObject () has several different methods of parameters, which can be defaulted and specified to return the name of the object.
Call the AddObject () method to set the value to a class property named Modelmap, Modelmap is a subclass of Linkedhashmap,
See class for details.

Model is an interface whose implementation class is Extendedmodelmap and inherits the Modelmap class.

Map

Java code
    1. @RequestMapping ( "/demo2/show")  
    2.     public map<string, String > Getmap () { 
    3.         map<string, string> Map =  new hashmap<string, string> ();  
    4.          map.put ( "Key1",  
    5.         map.put ( "value-2");  
    6.         < span class= "keyword" >return map; 
    7.    } 


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

View can be returned to PDF Excel, etc., for the time being without detailed information.

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.
For example:

Java code
    1. @RequestMapping (value = "/something", method = Requestmethod.get)
    2. @ResponseBody
    3. Public String HelloWorld () {
    4. return "Hello World";
    5. }


The above results will write the text "Hello World" directly to the HTTP response stream.

Java code
    1. @RequestMapping ("/welcome")
    2. Public String Welcomehandler () {
    3. return "center";
    4. }


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

void if the return value is NULL, the Response view page corresponds to the access address

Java code
    1. @RequestMapping ("/welcome")
    2. Public void Welcomehandler () {}


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

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

Model model,httpservletrequest request, modelmap Map declaration variable

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

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

SPRINGMVC return type selection and order of model,modelmap.request,session values 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.