Type of the return value of spring processing method, spring Processing Method
Spring mvc supports the following return methods: ModelAndView, Model, ModelMap, Map, View, String, void. The specific descriptions are as follows:
ModeAndView:
@RequestMapping("/show1") public ModelAndView show1(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = new ModelAndView("/demo2/show"); mav.addObject("account", "account -1"); return mav; }
You can use the ModelAndView constructor to specify the name of the returned page, or you can use the setViewName () method to jump to the specified page, and use addObject () to set the value to be returned, addObject () there are several different parameter methods, which can be used to specify and specify the name of the returned object by default.
Model:
@RequestMapping("/demo2/show") public Map<String, String> getMap() { Map<String, String> 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<String, String> map = new HashMap<String, String>(); map.put("key1", "value-1"); map.put("key2", "value-2"); return map; }
On the jsp page, you can obtain the value through $ {key1}. map. put () is equivalent to the request. setAttribute method.
String:
@RequestMapping(value = "/something", method = RequestMethod.GET)@ResponseBodypublic String helloWorld() {return "Hello World";}
String specifies the name of the returned view page, which can be accessed by adding the suffix of the returned address path and the page name.
Note: If the method declares the annotation @ ResponseBody, the return value is directly output to the page.
Void:
@RequestMapping("/welcome")public void welcomeHandler() {}
If the returned value is null, 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 to expose the Model data required 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"); System.out.println(model == model2); System.out.println(model2 == model3); return "success";}
Although three different types (Model model, Map model2, ModelMap model3) are injected here, the three are the same object ,:
@ RequestMapping (value = "/mergeModel") public ModelAndView mergeModel (Model model) {model. addattriess ("a", "a"); // ① add model data ModelAndView mv = new ModelAndView ("success"); mv. addObject ("a", "update"); // ② update model data with the same name at ③ before rendering the view. addAttribute ("a", "new"); // (3) Modify model data with the same name. // a on the view page is displayed as "update" instead of "new" return mv ;}
From the code, we can sum up the Model data (such as ModelAndView) in the return values of the function processing method, and merge the Model data (such as Model) in the function processing method parameters ), if the two parameters have the same name, the model data in the returned value will overwrite the model data in the form parameter.
The above content from: http://jinnianshilongnian.iteye.com/blog/1698916