更新時(shí)間:2020-08-24 11:10:50 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1658次
有些朋友對(duì)于EL表達(dá)式取值的范圍不是很了解,在Struts2中對(duì)request進(jìn)行了裝飾,增強(qiáng)了getAttribute()方法,改變了EL該方法的查找范圍,具體為,查找request域,不存在,查找值棧,不存在,查找ContextMap,還是不存在,則返回null。

因?yàn)?{對(duì)象名},會(huì)使用findAttribute的查找,其順序是page域->request域->session域->application域原理如下:
public class PageContext{
//在page域中根據(jù)name獲取value
public Object getAttribute(String name){
return "找到了返回對(duì)象" | "沒找到返回null";
}
//從四個(gè)域中逐個(gè)搜索,只要在其中一個(gè)找到,就不再繼續(xù)尋找
public Object findAttribute(String name){
Object value = null;
//搜page域
value = this.getAttribute(name);
if(value != null){
return value;
}
//搜request域
value = this.getRequest().getAttribute(name);
if(value != null){return value;}//搜session域value = this.getSession().getAttribute(name);if(value != null){return value;}//搜a(bǔ)pplication域value = this.getServletContext().getAttribute(name);if(value != null){return value;}return value;}}PS:由上述原因,Struts2對(duì)request進(jìn)行了裝飾,那么訪問順序變?yōu)閜age域->request域->值棧->ContextMap->session->application。
如果還想深究源碼,可以參考ServletConfigInterceptor攔截器
public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {
private static final long serialVersionUID = 605261777858676638L;
public String intercept(ActionInvocation invocation) throws Exception {
final Object action = invocation.getAction();
final ActionContext context = invocation.getInvocationContext();
if (action instanceof ServletRequestAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
((ServletRequestAware) action).setServletRequest(request);
}
if (action instanceof ServletResponseAware) {
HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
((ServletResponseAware) action).setServletResponse(response);
}
if (action instanceof ParameterAware) {
((ParameterAware) action).setParameters((Map)context.getParameters());
}
if (action instanceof ApplicationAware) {
((ApplicationAware) action).setApplication(context.getApplication());
}
if (action instanceof SessionAware) {
((SessionAware) action).setSession(context.getSession());
}
if (action instanceof RequestAware) {
((RequestAware) action).setRequest((Map) context.get("request"));
}
if (action instanceof PrincipalAware) {
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
if(request != null) {
// We are in servtlet environment, so principal information resides in HttpServletRequest
((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
}
}
if (action instanceof ServletContextAware) {
ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
((ServletContextAware) action).setServletContext(servletContext);
}
return invocation.invoke();
}
}以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“EL表達(dá)式取值的范圍”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問,請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
Java實(shí)驗(yàn)班
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
Java就業(yè)班
有基礎(chǔ) 直達(dá)就業(yè)
Java夜校直播班
業(yè)余時(shí)間 高薪轉(zhuǎn)行
Java在職加薪班
工作1~3年,加薪神器
Java架構(gòu)師班
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)