struts2 與 OGNL 運算式,jsp中 利用ognl 在valuestack中取值

來源:互聯網
上載者:User

標籤:struts2   ognl   valuestack   

在Struts2中,一個請求在最終到達Action的方法之前,Action對象本身會被壓入ValueStack(實際上就是放到ValueStack的CompoundRoot中),所以Action對象是CompoundRoot中的一個元素。看下面的代碼:

public class UserAction {

    private String username;

    private Integer age;

    private boolean valid;

   

    //查看使用者的詳細資料

    public String detail(){

      

       username = "張三";

       age = 18;

       valid = true;

      

       return "detail";

    }
 

在Action中,給Action的username/age/valid賦值。Detail頁面如下:

username:<s:property value="username"/> <br/>

valid:<s:property value="valid"/> <br/>

age:<s:property value="age"/> <br/>
 

上述JSP頁面將能正確將它們的值取出。<s:property value=”ognl運算式”/>。在s:property標籤中的OGNL運算式,最終會交給ValueStack來解釋。username就是一個OGNL運算式,意思是調用root對象的getUsername()方法。Struts2將自動搜尋CompoundRoot中有哪些元素(從第0個元素開始搜尋),檢測這些元素是否有getUsername()方法,如果第0個元素沒有getUsername()方法,將繼續搜尋第1、2、3……個元素是否有getUsername()方法。

 

在上面的例子中,CompoundRoot中只有一個對象,就是userAction對象,而這個對象中正好有getUsername()方法,所以,上述JSP代碼將能夠將值正確取出。

 

再看下面的例子:

public class UserAction {

    private String username;

    private String name;

   

    //查看使用者的詳細資料

    public String detail(){

       username = "張三";

       name = "王五";

      

       User u = new User();

       u.setUsername("趙毅");

       ActionContext.getContext().getValueStack().push(u);

      

       return "detail";

    }
 

在上面這個UserAction的代碼中,我們直接調用ActionContext.getContext().getValueStack().push()方法,把一個User對象(這個對象擁有getUsername()和setUsername()方法)直接壓入到ValueStack中,這時候,在ValueStack的CompoundRoot中將有兩個元素:第0個元素是剛剛壓入的user對象[趙毅],而第1個元素是userAction對象[張三],如果在JSP中使用下面的運算式來取值:

<s:property value=”username”/> ,那麼輸出的值將是“趙毅”!道理上面已經講過了,struts2將會從第0個元素開始搜尋CompoundRoot中的對象,第0個元素正是剛剛壓入的那個user對象!

如果在JSP中使用<s:property value=”name”/>來取值,將取出“王五”,因為第0個元素user對象沒有name屬性,所以,會繼續搜尋第1個元素userAction對象,在這個對象中就有name屬性了!


再看下面的代碼:

public class UserAction {

    private String username;

   

    //查看使用者的詳細資料

    public String detail(){

       username = "張三";

      

       List list = new ArrayList();

       for(int i=0; i<10; i++){

           User user = new User();

           user.setUsername("User"+i);

           list.add(user);

       }

       ActionContext.getContext().put("users", list);

      

       User u = new User();

       u.setUsername("趙毅");

       ActionContext.getContext().getValueStack().push(u);

      

       return "detail";

    }
 

 

對應的JSP如下:

1:  <s:property value="username"/> <br/>

2:  <s:iterator value="#users">

3:     <s:property value="username"/>

4:     <s:property value="#root[2].username"/><br/>

5:  </s:iterator>

6:  <s:property value="username"/>

7:  <s:property value="#root[1].username"/> <!-- 張三 -->
 

 

根據剛才的樣本,我們知道,第1行的username是“趙毅”(因為JSP在執行這行代碼的時候,CompoundRoot中有兩個元素:第0個是“user對象趙毅”,第1個是“userAction對象張三”),因此第1行的username將取出CompoundRoot中第0個元素的username屬性:趙毅

 

第2行代碼是iterator標籤,只定義了一個value屬性,iterator標籤將逐一查看users這個List中的User對象,並把當前迴圈的user對象壓入到CompoundRoot中!所以,在第3行和第4行代碼被執行的時候,CompoundRoot中總共有3個元素:第0個元素是被iterator標籤壓入的當前迴圈的user對象;第1個元素是“user對象趙毅”;第2個元素是“userAction對象張三”,因此第3行代碼的執行結果就是輸出“UserX”,即當前迴圈的user對象的username屬性!iterator標籤將會依次取出List中的user對象,並不斷壓入/彈出user對象(每次迴圈,都將執行一遍壓入/彈出)。而第4行代碼取第2個元素的username屬性,即userAction對象的username屬性:張三。

 

第5行代碼執行完成之後,在CompoundRoot中將剩下2個元素,與第2行代碼被執行之前一樣。所以,第6行代碼的輸出和第1行代碼的輸出結果是一樣的,而第7行代碼將取出userAction對象的username屬性:張三


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.