springMVC中的HttpSession與Model



突然發問:相信很多人在做WEB開發的時候,在傳屬性的時候都用到過這兩個,但是這兩個有什么區別?使用場景?

1.1 spring的@MODELATTRIBUTE

這里有好幾種向spring的Model添加數據的方式。數據或者對象通常通過在controller上的注釋方法添加到spring中的model中去。下邊這個例子中,@ModelAttribute用來將MyCommandBean的實例以key值為myRequestObject添加到model中去

@Controller
public class MyController {
 
	@ModelAttribute("myRequestObject")
	public MyCommandBean addStuffToRequestScope() {
		System.out.println("Inside of addStuffToRequestScope");
		MyCommandBean bean = new MyCommandBean("Hello World",42);
		return bean;
	}
 
	@RequestMapping("/dosomething")
	public String requestHandlingMethod(Model model, HttpServletRequest request) {
		System.out.println("Inside of dosomething handler method");
 
		System.out.println("--- Model data ---");
		Map modelMap = model.asMap();
		for (Object modelKey : modelMap.keySet()) {
			Object modelValue = modelMap.get(modelKey);
			System.out.println(modelKey + " -- " + modelValue);
		}
 
		System.out.println("=== Request data ===");
		java.util.Enumeration reqEnum = request.getAttributeNames();
		while (reqEnum.hasMoreElements()) {
			String s = reqEnum.nextElement();
			System.out.println(s);
			System.out.println("==" + request.getAttribute(s));
		}
 
		return "nextpage";
	}
 
         //  ... the rest of the controller
}

在一個請求的request中,任何使用@ModelAttribute注解的方法會在controller的handler方法(像上邊例子匯總的requestHandlingMethod 方法)之前被調用。在這些handler方法執行前,這些方法把數據增加到java.util.map中最終添加到spring Model中去。這可以通過一個簡單的實驗證明,我創建了兩個jsp頁面:index.jsp和nextpage.jsp。index.jsp中的鏈接用來發送request到web應用中來觸發Mycontroller中的requestHandlingMethod()方法。requestHandlingMethod()方法返回“nextpage”作為下一個視圖邏輯上的名字,在這個例子中我們解析為nextpage.jsp。
當這個小的web站點用這種方式執行的時候,controller里邊的System.out.println方法表明了@ModelAttribute方法是如何在handler方法之前運行的。它同樣也展示了這個MyCommandBean被創建和添加到springModel中去的過程。

Inside of addStuffToRequestScope
Inside of dosomething handler method
--- Model data ---
myRequestObject -- MyCommandBean [someString=Hello World, someNumber=42]
=== Request data ===
org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE
==WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Oct 13 21:40:56 CDT 2013]; root of context hierarchy
org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER
==org.springframework.web.servlet.theme.FixedThemeResolver@204af48c
org.springframework.web.servlet.DispatcherServlet.CONTEXT
==WebApplicationContext for namespace 'dispatcher-servlet': startup date [Sun Oct 13 21:40:56 CDT 2013]; root of context hierarchy
org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping
==dosomething.request
org.springframework.web.servlet.HandlerMapping.bestMatchingPattern
==/dosomething.*
org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER
==org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver@18fd23e4

2.1 session的概念

session即 會話,是客戶為實現特定應用目的與系統的多次請求交互。它具體是指一個終端用戶與交互系統進行通信的時間間隔,通常指從 注冊進入系統到注銷退出系統 之間所經過的時間。
modelAttribute與sessionAttribute區別?

  • modelAttribute:常量定義,比如key,value的映射。像下拉框,多選框等等
  • sessionAttribute:跟用戶會話相關的常量,如用戶基本信息等

3.1 示例

顯示當前登錄用戶的狀態信息(二者效果一樣)

controller
  session.setAttribute("admin",admin);
  session.setAttribute("loginUser",username);
頁面
  <a href="#">[[${session.admin.account}]]</a>
  <a href="#">[[${session.loginUser}]]</a>
  (session需要加上)
controller
  model.addAttribute("msg", "賬號或密碼錯誤");
頁面
  <a href="javascript:;">[[${msg}]]</a>
  (直接用)

4.1 為什么springmvc框架要使用model這個對象呢?

在之前原生的Servlet中就有了Session,為什么還要用到Model?
request他只是一個請求,他的作用就是從客戶端發起一個請求,並且攜帶客戶端發起的這個請求所帶的參數,在業務層中進行參數的獲取並且做出相對應的處理,到這里這個request請求對象的工作就應該結束了,剩下的就是客戶端對這個請求和參數做出處理結果並且生成響應response返回客戶端。

所以不應該用request進行存值,來達到模型層和視圖層的一個連接,所以才使用model或是modelandview這個專門的對象來進行模型層的存在和視圖層的取值

model會在模型層進行存值,在視圖層中,他會去檢查model對象中是否用這個屬性,有他就會渲染出來,request請求結束后就會自動清除model的數據

這樣一來具體的Model我們可以寫在具體的Controller里的方法上,與視圖層一一綁定。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM