W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
為了讓開(kāi)發(fā)人員能夠隨時(shí)隨地獲取和使用Request、Response、Session等Web容器對(duì)象,YMP框架在WebMVC模塊中提供了一個(gè)名叫WebContext的Web環(huán)境上下文封裝類,簡(jiǎn)單又實(shí)用,先了解一下提供的方法:
直接獲取Web容器對(duì)象:
獲取ServletContext對(duì)象:
WebContext.getServletContext();獲取HttpServletRequest對(duì)象:
WebContext.getRequest();獲取HttpServletResponse對(duì)象:
WebContext.getResponse();獲取PageContext對(duì)象:
WebContext.getPageContext();
獲取WebMVC容器對(duì)象:
獲取IRequestContext對(duì)象:
WebContext.getRequestContext();WebMVC請(qǐng)求上下文接口,主要用于分析請(qǐng)求路徑及存儲(chǔ)相關(guān)參數(shù);
獲取WebContext對(duì)象實(shí)例:
WebContext.getContext();
WebContext將Application、Session、Request等Web容器對(duì)象的屬性轉(zhuǎn)換成Map映射存儲(chǔ),同時(shí)向Map的賦值也將自動(dòng)同步至對(duì)象的Web容器對(duì)象中,起初的目的是為了能夠方便代碼移植并脫離Web環(huán)境依賴進(jìn)行開(kāi)發(fā)測(cè)試(功能參考Struts2):
WebContext.getContext().getApplication();
WebContext.getContext().getSession();
WebContext.getContext().getAttribute(Type.Context.REQUEST);
原本可以通過(guò)WebContext.getContext().getRequest方法直接獲取的,但由于設(shè)計(jì)上的失誤,方法名已被WebContext.getRequest()占用,若變更方法名受影響的項(xiàng)目太多,所以只好委屈它了:D,后面會(huì)介紹更多的輔助方法來(lái)操作Request屬性,所以可以忽略它的存在!
WebContext.getContext().getAttributes();
WebContext.getContext().getLocale();
WebContext.getContext().getOwner();
WebContext.getContext().getParameters();
WebContext操作Application的輔助方法:
boolean getApplicationAttributeToBoolean(String name);
int getApplicationAttributeToInt(String name);
long getApplicationAttributeToLong(String name);
String getApplicationAttributeToString(String name);
<T> T getApplicationAttributeToObject(String name);
WebContext addApplicationAttribute(String name, Object value)
WebContext操作Session的輔助方法:
boolean getSessionAttributeToBoolean(String name);
int getSessionAttributeToInt(String name);
long getSessionAttributeToLong(String name);
String getSessionAttributeToString(String name);
<T> T getSessionAttributeToObject(String name);
WebContext addSessionAttribute(String name, Object value)
WebContext操作Request的輔助方法:
boolean getRequestAttributeToBoolean(String name);
int getRequestAttributeToInt(String name);
long getRequestAttributeToLong(String name);
String getRequestAttributeToString(String name);
<T> T getRequestAttributeToObject(String name);
WebContext addRequestAttribute(String name, Object value)
WebContext操作Parameter的輔助方法:
boolean getParameterToBoolean(String name);
int getParameterToInt(String name)
long getParameterToLong(String name);
String getParameterToString(String name);
WebContext操作Attribute的輔助方法:
<T> T getAttribute(String name);
WebContext addAttribute(String name, Object value);
WebContext獲取IUploadFileWrapper上傳文件包裝器:
IUploadFileWrapper getUploadFile(String name);
IUploadFileWrapper[] getUploadFiles(String name);
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: