使用struts2中的ognl表达式调用类方法(转)

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>

猜你喜欢

转载自zfei.iteye.com/blog/1487000