springMVC數據模型model,modelmap,map,@ModelAttribute的相互關系
結論:
a.註解方法中形參為model,modelmap,map一個或幾個時,他們指向的引用對象相同即他們的值相同。
b.當使用@ModelAttribute註解請求參數時,springmvc自動將該參數放入model,modelmap,map中。
c.model,modelmap,map中put,request.setattribute(),b中@ModelAttribute以及modelandveiw.addObj()效果相同,return時都是將參數放request的attribute中。
d.model,modelmap,map,modelandview的生命同期僅存在於當前方法內,forward/return後重新生成新空對象。
e.當使用redirectAttribute.addFlashAttribute重定向時,FlashAttribute會自動註入下一個action內部model,modelmap,map,詳細請參考https://www.cnblogs.com/pu20065226/p/10032048.html中的3.2model跟蹤。
1.發送請求:http://localhost:8080/project/page/login/ModelTest/Map.do?aa=111&bb=333
@Controller @RequestMapping("/page/login/ModelTest") public class ModelTestController { @RequestMapping(value= "/Map.do") public String MapTest(HttpServletRequest request, Map<String, Object> map) { System.out.println("hello"); System.out.println(map); map.put("step1", "step1"); PrintRequestInfo.printSessionAndRequest(request, ModelTestController.class.getName());//打印 map.put("step2", "step2"); request.setAttribute("step3", "step3"); final HttpSession session = request.getSession(); session.setAttribute("step4", "step4"); // return "../loginSuccess.jsp"; return "Map1.do"; } }
輸出:
hello
{}
================com..controller.ModelTestController====================
print request parameter:
aa==111
bb==333
print request attribute:
print session parameter:
step4==step4
結論:map的初始值為空
2.當請求forward到第二個action(Map1.do)中
@RequestMapping(value = "/Map1.do") public String MapTest1(HttpServletRequest request, @ModelAttribute("aa") String aa, Map<String, Object> map, Model model) { System.out.println("welcome to MapTest1"); model.addAttribute("mdbefore", "before"); System.out.println(map); System.out.println("................"); System.out.println(model.asMap()); model.addAttribute("mdafter", "after"); System.out.println("hello"); System.out.println(map); System.out.println("................"); System.out.println(model.asMap()); PrintRequestInfo.printSessionAndRequest(request, ModelTestController.class.getName() + "1"); // return "../loginSuccess.jsp"; return "Map2.do"; }
輸出:
welcome to MapTest1
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before}
................//aa由形參@ModelAttribute(“aa”)註入
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before}
hello
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before, mdafter=after}
................
{aa=111, org.springframework.validation.BindingResult.aa=org.springframework.validation.BeanPropertyBindingResult: 0 errors, mdbefore=before, mdafter=after}
================com.controller.ModelTestController1====================
print request parameter:
aa==111
bb==333
print request attribute:
step3==step3 //上一個action中map.put加入
step2==step2
step1==step1 //上一個action中request.setattribute加入
print session parameter:
step4==step4
結論:
a.註解方法中形參為model,modelmap,map一個或幾個時,他們指向的引用對象相同即他們的值相同。
b.當使用@ModelAttribute註解請求參數時,springmvc自動將該參數放入model,modelmap,map中。
3.當請求進入第三個action(Map2.do)時
@RequestMapping(value = "/Map2.do") public String MapTest2(HttpServletRequest request, Map<String, Object> map, Model model, ModelMap mm) { System.out.println("welcome to MapTest2"); model.addAttribute("mdbefore2", "before2"); System.out.println(map); System.out.println("................"); System.out.println(model.asMap()); System.out.println("................"); System.out.println(mm); model.addAttribute("mdafter2", "after2"); System.out.println("hello"); System.out.println(map); System.out.println("................"); System.out.println(model.asMap()); PrintRequestInfo.printSessionAndRequest(request, ModelTestController.class.getName() + "2"); return "../loginSuccess.jsp"; }
輸出結果:
welcome to MapTest2
{mdbefore2=before2}
................
{mdbefore2=before2}
................
{mdbefore2=before2}
hello
{mdbefore2=before2, mdafter2=after2}
................
{mdbefore2=before2, mdafter2=after2}
================com.controller.ModelTestController2====================
print request parameter:
aa==111
bb==333
print request attribute:
mdbefore==before//由上一個action中model.addAtrribute加入
step3==step3
step2==step2
step1==step1
mdafter==after//由上一個action中model.addAtrribute加入
aa==111 //aa由上一個action形參@ModelAttribute(“aa”)註入
print session parameter:
step4==step4
結論:
c.model,modelmap,map中put,request.setattribute(),b中@ModelAttribute以及modelandveiw.addObj()效果相同(可以自己測試),return時都是將參數放request的attribute中。
d.model,modelmap,map,modelandview的生命同期僅存在於當前方法內,forward/return後重新生成新空對象
打印方法代碼
public class PrintRequestInfo { public static void printSessionAndRequest(HttpServletRequest request, String remark) { System.out.println("================" + remark + "===================="); System.out.println("print request parameter:"); final Enumeration reqEnum = request.getParameterNames(); while (reqEnum.hasMoreElements()) { final String s = (String) reqEnum.nextElement(); System.out.println(s + "==" + request.getParameter(s)); } System.out.println("print request attribute:"); final Enumeration reqAttrs = request.getAttributeNames(); while (reqAttrs.hasMoreElements()) { final String s = (String) reqAttrs.nextElement(); System.out.println(s + "==" + request.getAttribute(s)); } System.out.println("print session parameter:"); final HttpSession session = request.getSession(); final Enumeration se = session.getAttributeNames(); while (se.hasMoreElements()) { final String key = (String) se.nextElement(); System.out.println(key + "==" + session.getAttribute(key)); } } }
springMVC數據模型model,modelmap,map,@ModelAttribute的相互關系