EL取得action 屬性的原理

2021-07-08 17:37:25 字數 2084 閱讀 9984

今天乙個哥們學習struts2,嘗試使用ognl,由於關聯性吧,就又跳到el上面,結果神奇的事情發生,他用el表示式從request域裡面居然拿到了action鐘的屬性,這個天崩地裂的,大家都知道struts2中存值物件都放在actioncontext中,如下圖:

這幅截圖來自於sturts2 in actin一書中,由於action是放到valuestack中的,但是valuestack與request貌似沒有交集吧。

他居然拿到了,疑惑開始了,通過在頁面中新增標籤發現了乙個問題,居然在request裡面有struts2.valuestack這麼個變數吧,疑團沒有散去,卻變得更加大了,怎麼搞的?

最後從網上發現了一篇相似的帖子:

帶著這個疑問,我google了一下,很快找到答案:

提問:在struts2中,如 何使用jstl來讀取action中的變數?這是乙個歷史悠久的問題。因為事實上,很多朋友(包括我在內)是不使用struts2自身的標籤庫,而是使用jstl的,可能因為jstl標籤庫比 較少,簡單易用的原因吧。

不過隨著時代的發展,上面的這種方式,已經不再被推薦使用了。(雖然如此,我們依然可以學習它的乙個解決問題的思路)目前來說,自從 webwork2.2以後,包括struts2,都使用另外一種整合方式:對httpservletrequest進行裝飾。讓我們來看一下原始碼:

123

4567

891011

1213

1415

1617

1819

2021

2223

2425

2627

2829

3031

3233

3435

3637

3839

4041

4243

4445

4647

48

public

/*** the constructor

* @param req the request

*/super(req);

}/**

* gets the object, looking in the value stack if not found

** @param s the attribute key

*/public object getattribute(string s)

actioncontext ctx = actioncontext.getcontext();

object attribute = super.getattribute(s);

boolean alreadyin = false;

if (b != null)

// note: we don't let # come through or else a request for

// #attr.foo or #request.foo could cause an endless loop

if (!alreadyin && attribute == null && s.indexof("#") == -1)

} finally

}return attribute;}}

看到了嘛?這個類會在struts2初始化的時候,替換httpservletrequest,執行於整個struts2的執行過程中,當我們試 圖呼叫request.getattribute()的時候,就會執行上面的這個方法。(這是乙個典型的裝飾器模式)在執行上面的方法時,會首先呼叫 httpservletrequest中原本的request.getattribute(),如果沒有找到,它會繼續到valuestack中去查詢, 而action在valuestack中,所以action中的變數通過ognl表示式,就能找到對應的值了。

在這裡,在el表示式廣泛使用的今天,jstl1.1以後,也支援直接使用el表示式。注意與直接使用struts2的tag的區別,這裡需要使用 el的表示符號:${}

例如:$,

el屬性和template屬性的關係

一 簡介 本篇文章主要介紹el屬性和template屬性的關係 一句話來概括,當el和template同時出現的時候,template中的 會替代el掛載的dom元素。data template 按鈕 看例子 1.這是index.html中,el屬性掛載的dom元素 2.這是template的模板 ...

el表示式需要action的get方法

controller value highchartaction public class highchartaction extends actionsupport private static final long serialversionuid 1l private string displ...

6 3Action的呼叫與屬性

action的呼叫與屬性 1 喚起action route找到action 喚起action controlleractioninvoker 1 找到對應的action 2 找到當前請求發生的引數,匹配 3 呼叫action方法所有的filters 4 呼叫excuteresult 2 action...