Type of the return value of spring processing method, spring Processing Method

Source: Internet
Author: User

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

Related Article

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.