使用ExternalContext類提供的方法可以獲取ServletContext、ServletRequest和ServletResponse對象,構(gòu)造FacesContext實例時需要這些對象。除此之外,ExternalContext實例提供了包裝器方法,可以使用這些方法獲得原來需要從ServletContext、ServletRequest及ServletResponse對象上調(diào)用一些方法獲得的信息。
3.3.1 獲取ServletContext、ServletRequest和ServletResponse對象
可使用下列方法獲取servlet信息:
● getContext 此方法可獲取Web應用中與當前請求相關(guān)聯(lián)的ServletContext對象。其簽名如下:
public abstract Object getContext()
● getRequest 此方法可獲取代表當前正在處理的請求的ServletRequest對象。其簽名如下:
public abstract Object getRequest()
● getResponse 此方法可獲取代表當前正在呈現(xiàn)的響應的ServletResponse對象。其簽名如下:
public abstract Object getResponse()
這些方法都是返回一個java.lang.Object對象,不是servlet特有的類型,這樣就可以使JSF實現(xiàn)獨立于其運行的環(huán)境。比如,JSF既可用于Web容器,也可以用于其他容器,如portlet等。
3.3.2 獲取ServletContext特性
getApplicationMap方法返回一個包含ServletContext對象里全部特性名/值對的Map對象。下面是此方法的簽名:
public abstract java.util.Map getApplication()
作為一個例子,下面的代碼可獲取一個名叫databaseUtility的特性:
Object contextAttribute = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map contextMap = externalContext.getApplicationMap();
if (contextMap!=null)
contextAttribute = contextMap.get("databaseUtility");
3.3.3 獲取Session對象及其特性
通過ExternalContext對象可訪問與當前請求相關(guān)聯(lián)的Session對象。getSession方法可取回當前用戶的javax.servlet.http.HttpSession對象,如果當前用戶沒有相應的Session對象,此方法的行為由傳入的參數(shù)決定:如果為該方法傳入了一個true值,它會創(chuàng)建一個Session對象;否則,它會返回null。下面是getSession方法的簽名:
public abstract Object getSession(boolean create)
此方法其實是javax.servlet.http.HttpServletRequest接口中g(shù)etSession方法的包裝器。
getSessionMap方法返回一個包含與當前請求相關(guān)聯(lián)的Session對象里所有特性名/值對的Map對象。下面是它的方法簽名:
public abstract java.util.getSessionMap()
要獲取Session對象里的特性,可調(diào)用Map類的get方法,傳入要獲取的特性名即可。文檔中沒有指明在當前請求沒有相應Session對象的情況下,此方法是返回null還是一個空的Map對象。所以在調(diào)用Map的get方法之前,需要先檢查Map是否為null。下面的代碼是獲取Session特性的例子:
Object sessionAttribute = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map sessionMap = externalContext.getSessionMap();
if (sessionMap!=null)
sessionAttribute = sessionMap.get(key);
最后一行的key是一個包含特性名的字符串。
3.3.4 獲取ServletContext對象的初始參數(shù)
getInitParameter方法是ServletContext對象的getInitParameter方法的包裝器,用這個方法可以提取在部署描述符(web.xml文件)里用context-init元素指定的初始參數(shù)值。此方法的簽名如下:
public abstract String getInitParameter(String parameterName)
舉例來說,如果在部署描述符聲明了如下context-init元素:
<context-param>
<param-name>contactPerson</param-name>
<param-value>Scott Jobim</param-value>
</context-param>
下面代碼中的字符串變量initParam的值會是Scott Jobim。
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
String initParam = externalContext.getInitParameter("contactPerson");
getInitParameterMap方法返回一個包含ServletContext對象中全部初始參數(shù)的Map對象。其簽名如下:
public abstract java.util.Map getInitParameterMap()
為了獲取一個初始參數(shù)的值,使用Map對象的get方法,同時傳遞初始參數(shù)的名稱。比如,下面的代碼把初始參數(shù)databaseName的值輸出到控制臺。
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map paramMap = externalContext.getInitParameterMap();
if (paramMap!=null) {
System.out.println(paramMap.get("databaseName"));
}
3.3.5 獲取Request對象的特性
getRequestMap方法返回一個包含當前Request對象中全部特性名/值對的Map對象。其方法簽名如下:
public abstract java.util.Map getRequestMap()
作為一個例子,下面的代碼可用來提取Request對象里的特性:
Object requestAttribute = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map requestMap = externalContext.getRequestMap();
if (requestMap!=null)
requestAttribute = requestMap.get(key);
最后一行里的Key是一個包含要提取的屬性名的字符串。
3.3.6 訪問Request對象里的參數(shù)名和值
getRequestParameterMap、getRequestParameterNames和getRequestParameterValuesMap方法可用來訪問Request對象里的參數(shù)名和值。
getRequestParameterMap返回一個包含Request對象里全部參數(shù)名/值對的Map對象。其簽名如下:
public abstract java.util.Map getRequestParameterMap()
作為一個例子,下面的代碼可用來提取名為id的請求參數(shù)的值:
String id = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map requestParameterMap = externalContext.getRequestParameterMap();
if (requestParameterMap!=null)
id = (String) requestParameterMap.get("id");
getRequestParameterNames方法返回一個包含全部請求參數(shù)名的Iterator。此方法其實是ServletRequest.getParameterNames方法的包裝器。不同的是,ExternalContext類的getRequestParameterNames返回一個Iterator,而不是java.util.Enumeration。此方法的簽名如下:
public abstract java.util.Iterator getRequestParameterNames()
作為一個例子,下面的代碼把所有的請求參數(shù)名/值對輸出到控制臺。
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map requestParameterMap = externalContext.getRequestParameterMap();
Iterator parameterNames = externalContext.getRequestParameterNames();
while (parameterNames.hasNext()) {
String parameterName = (String) parameterNames.next();
String parameterValue =
(String) requestParameterMap.get(parameterName);
System.out.println(parameterName + " : " + parameterValue);
}
getRequestParameterValuesMap方法返回一個包含Request對象里全部參數(shù)名/值對的Map對象。此方法與getRequestParameterMap方法很相似,但getRequestParameterValuesMap可返回全部相同參數(shù)名的值。在此方法返回的Map對象上調(diào)用get(key)方法,這一點等同于獲取當前請求的ServletRequest并在其上調(diào)用getParameterValues(key)。也就是說,Map對象返回的是一個字符串數(shù)組。GetRequestParameterValuesMap方法的簽名如下:
public abstract java.util.Map getRequestParameterValuesMap()
下面例子中的代碼把請求參數(shù)id的全部值輸出到控制臺。
String[] id = null;
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map requestParameterValuesMap = externalContext.getRequestParameterValuesMap();
if (requestParameterValuesMap!=null) {
id = (String[]) requestParameterValuesMap.get("id");
// print all values of id
for (int i=0; i<id.length; i++) {
System.out.println(id[i]);
}
}