标签:obj @param attribute ESS 源码 app ring ann color
springMVC处理请求过程中,往作用域中放值有以下三种方式:
总结:根据ModelAndView源码调试,不管用以下哪种方式,都会把Model数据和View数据封装到一个ModelAndView中
1 package com.iwakan.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.servlet.ModelAndView; 7 8 import java.util.Map; 9 10 /** 11 * 三种方式实现作用域放值 12 */ 13 @Controller 14 public class TestController { 15 16 /** 17 * 第一种:返回值为ModelAndView 18 * @return 19 */ 20 @RequestMapping("/hello") 21 public ModelAndView hello(){ 22 ModelAndView mav = new ModelAndView(); 23 mav.addObject("msg","spring mvc"); 24 mav.setViewName("success"); 25 return mav; 26 } 27 28 /** 29 * 第二种:形参为Map 30 * @param map 31 * @return 32 */ 33 @RequestMapping("/hello2") 34 public String hello2(Map<String,Object> map){ 35 map.put("msg","spring mvc2"); 36 return "success"; 37 } 38 39 /** 40 * 第三种:形参为Model 41 * @param model 42 * @return 43 */ 44 @RequestMapping("/hello3") 45 public String hello3(Model model){ 46 model.addAttribute("msg","spring mvc3"); 47 return "success"; 48 } 49 }
标签:obj @param attribute ESS 源码 app ring ann color
原文地址:https://www.cnblogs.com/iwakan/p/13775776.html