W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
DoradoContext# 簡介
DoradoContext是Dorado的上下文對象,在DoradoContext中,將上下文作用域區(qū)分為四種類型,分別為:
通過DoradoContext提供的相關(guān)方法我們可以存取相關(guān)作用域下的屬性,這些方法有:
public Object getAttribute(String scope, String key);
public void setAttribute(String scope, String key, Object value);
public void removeAttribute(String scope, String key);
例如我們可以通過以下代碼訪問HttpSession中的值:
DoradoContext context = DoradoContext.getCurrent();
String username = context.getAttribute(DoradoContext.SESSION, "username");
通過以下代碼訪問ServletContext中某一個attribute的值
DoradoContext context = DoradoContext.getCurrent();
Object securityData = context.getAttribute(DoradoContext.APPLICATION, "securityData");
當(dāng)然了除了可以通過getAttribute獲取指定作用域中的值,你也可以用setAttribute方法設(shè)置指定作用域指定屬性的值,范例代碼如:?
public void authorization(String username, String password) throws Exception{
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)){
DoradoContext context = DoradoContext.getCurrent();
context.setAttribute(DoradoContext.SESSION, "username", username);
}else{
throw new Exception("用戶名和密碼不能為空!");
}
}
另外也提供了不指定作用域的相關(guān)方法,有:
public Object getAttribute(String key);
在獲取上下文對象的時候如果你不指定作用域的時候,如:
DoradoContext context = DoradoContext.getCurrent();
String username = context.getAttribute("username");
DoradoContext會依照如下的順序挨個到不同的作用域中查找是否有指定的屬性,如果找到就直接返回,不再繼續(xù)查找:
VIEW作用域的數(shù)據(jù)可以在Dorado的AJAX請求中自動在客戶端和服務(wù)器端同步。例如:你可以在Ajax請求中在服務(wù)器端設(shè)置變量,并在Ajax請求結(jié)束后,在客戶端獲取這個變量的值。 客戶端JS獲取Context中VIEW作用域的變量,參考代碼:
var username = view.get("context").get("username");
//獲取Spring中的ApplicationContext
public ApplicationContext getApplicationContext()
//獲取Spring中的WebApplicationContext
public WebApplicationContext getWebApplicationContext()
//獲取當(dāng)前web請求的ServletContext
public static ServletContext getServletContext()
//獲取當(dāng)前線程綁定的HttpServletRequest
public static HttpServletRequest getRequest()
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: