Spring MVC supports the following return methods: Modelandview, Model, Modelmap, Map,view, String, void.
Modelandview
@RequestMapping ("/hello") public Modelandview helloWorld () { = ' Hello world, Spring 3.x! " ; return New Modelandview ("Hello", "message", message); }
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.
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.
View
You can return 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.
@RequestMapping (value= "/showdog") public String hello1 () { return ' Hello "; }
@RequestMapping (value= "/print") @ResponseBody public String print () { = "Hello World, Spring mvc!" ; return message; }
Return to the JSON example (using Jackson):
@RequestMapping ("/load1") @ResponseBody PublicString Load1 (@RequestParam string name, @RequestParam string password)throwsioexception{System.out.println (name+" : "+password); //return name+ ":" +password;Mydog dog=NewMydog (); Dog.setname ("Xiao Ha");d og.setage ("1 years old");d Og.setcolor ("dark grey"); Objectmapper Objectmapper=NewObjectmapper (); String jsonstring=objectmapper.writevalueasstring (dog); System.out.println (jsonstring); returnjsonstring; }
void
If the return value is empty, the Response view page corresponds to the access address
@RequestMapping ("/index") publicvoid index () { return ; }
The corresponding logical view is named "Index".
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.
Optional type of SPRINGMVC Controller return value