使用struts2中的ognl運算式調用類方法

來源:互聯網
上載者:User

struts標籤中value都會被解析,如,<s:property value="foo"/> 會被解析成getFoo()

我想問一下,還有沒有其它的屬性會解析?
另外就是<s:hidden id="_r" value="form.r"/>我試這個時,發現form.r並沒有被解析?換成<s:hidden id="_r" value="%{form.r}"/>才可以。

 

Struts2的 OGNL運算式中三個符號的用法。 OGNL運算式中可以使用$、#、和%三個符號。
通常時候,%很少使用,除非極個別的時候。

但#的用法非常廣泛,
1.直接在運算式中產生Map對象時就是用該符號。
2.訪問Stack Context中的值都需要使用。

$的用法:
1.取出集合元素的最後一個元素。
2.直接存取ValueStack根的屬性。

使用<s:debug/>可以看一下這個調試頁,然後決定用#或$吧。

Struts2還提供了一些命名物件,這些命名物件與根對象無關,它們只是存在於Stack Context中。所以,訪問這些對象時需要使用#首碼來指明。
1.parameters對象:用於訪問HTTP請求參數。例如#parameters['foo']或#parameters.foo,用於返回調用HttpServletRequest的getParameter("foo")方法的傳回值。
2.request對象:用於訪問HttpServletRequest的屬性。例如# request ['foo']或# request.foo,用於返回調用HttpServletRequest的getAttribute("foo")方法的傳回值。
3.session對象:用於訪問HttpSession的屬性。例如# session ['foo']或# session.foo,用於返回調用HttpSession的getAttribute("foo")方法的傳回值。
4.application對象:用於訪問ServletContext的屬性。例如# application ['foo']或# application.foo,用於返回調用ervletContext的getAttribute("foo")方法的傳回值。
5.attr對象:該對象將依次搜尋如下對象:PageContext、HttpServletRequest、HttpSession、ServletContext中的屬性。
注意:當系統建立了Action執行個體後,該Action執行個體已經被儲存到ValueStack中,故無需書寫#即可訪問Action屬性。

提供多一種訪問靜態方法和變數的方式.
@className@variableName
@className@methodName()
如:<s:property value="@java.lang.Math@random()"/>
@examples.chap8.Muppet@OG_MUPPET
@examples.chap8.Muppet@getOgMuppet()

 

在資料庫的設計中,字典項是經常使用的技巧。
比如在一個圖書館系統中,書籍表(Book)會有一個類別欄位,這時候我們一般會單獨建立一張分類表(Category),在書籍表只儲存分類表的ID。
在使用者介面上顯示書籍明細的時候,會要求顯示CategoryID在Category表中對應的名稱。
這樣通常的做法是把Book和Category兩張表進行關聯。
但在實際應用中,Category一般都是Cache在應用伺服器端,再使用資料表的串連就不夠高效。
我的做法是這樣的:在iBatis中使用SqlMap從表中將資料取出,此時不使用資料表的串連。

package com.demo;
public class Book {
      
        private int id;
        private String name;
        private int categoryId;
        private String author;
}

package com.demo;

public class Category {
        private static Map<Integer, Category> cacheMap;

        

        private int id;
        private String name;
        public static Category getCategory(int id) {
             init();
             return cacheMap.get(id);
        }
 
        public static Map<Integer, Category> getCategoryMap() {
            init();
            return cacheMap();
        }

        private init() {
             if ( cacheMap != null ) return;

             // the code to load category from datebase
             // 在這裡為了示範的需要,使用以下代碼

             cacheMap = new HashMap<Integer, Category>();
             Category category = new Category();
             category.setId(1);
             category.setName("Fiction");
             cacheMap.put(1, category);
 
             category = new Category();
             category.setId(2);
             category.setName("Cartoon");
        }
}

package com.demo;

public class BookAction  {
        
        Book book;

        public String execute() {
                book = new Book();
                book.setId(1);
                book.setName("Thinking in java");
                book.setCategoryId(1);
                bookList.add(book);
                
                return SUCCESS;                
        }
}

JSP:

<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <s:head />
 </head>

 <body>

  <table border="1">
    <tr>
     <td>
      <s:text name="page.label.userName" />
     </td>
     <td>
      <s:property value="book.name" />
     </td>
    </tr>
    <tr>
     <td>
      <s:text name="page.label.category" />
     </td>
     <td>
       <s:property value="@com.demo.Category@getCategory(book.categoryId).getName()"/></td>
    </tr>
 </body>
</html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.