1)OGNL (Object Graph Navigation Language)
访问值栈中的action的普通属性: username = <s:property value="username"/>
访问值栈中对象的普通属性(get set方法): <s:property value="user.age"/>
| <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/>
访问值栈中对象的普通属性(get set方法): <s:property value="cat.friend.name"/>
访问值栈中对象的普通方法: <s:property value="password.length()"/>
访问值栈中对象的普通方法: <s:property value="cat.miaomiao()" />
访问值栈中action的普通方法: <s:property value="m()" />
访问静态方法: <s:property value="@com.bjsxt.struts2.ognl.S@s()"/>
访问静态属性: <s:property value="@com.bjsxt.struts2.ognl.S@STR"/>
访问Math类的静态方法: <s:property value="@@max(2,3)" />
访问普通类的构造方法: <s:property value="new com.bjsxt.struts2.ognl.User(8)"/>
访问List: <s:property value="users"/>
访问List中某个元素: <s:property value="users[1]"/>
访问List中元素某个属性的集合: <s:property value="users.{age}"/>
访问List中元素某个属性的集合中的特定值: <s:property value="users.{age}[0]"/>
| <s:property value="users[0].age"/>
访问Set: <s:property value="dogs"/>
访问Set中某个元素: <s:property value="dogs[1]"/>
访问Map: <s:property value="dogMap"/>
访问Map中某个元素: <s:property value="dogMap.dog101"/>
| <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/>
访问Map中所有的key: <s:property value="dogMap.keys"/>
访问Map中所有的value: <s:property value="dogMap.values"/>
访问容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/>
投影(过滤):<s:property value="users.{?#this.age==1}[0]"/>
投影:<s:property value="users.{^#this.age>1}.{age}"/>
投影:<s:property value="users.{$#this.age>1}.{age}"/>
投影:<s:property value="users.{$#this.age>1}.{age} == null"/>
users.{?#this.age==1}[0] 表示取出users里age=1的那些user,再取出下标为0的那个
users.{^#this.age>1}.{age} 表示取出users里age>1那些user的第一个,^表示第一个
users.{$#this.age>1}.{age} 表示取出users里age>1那些user的最后一个,$表示最后一个
注意this不能省略
[]: <s:property value="[0].username"/>
<s:debug></s:debug>
//OgnlAction.java
public class OgnlAction extends ActionSupport {
private Cat cat;
private Map<String, Dog> dogMap = new HashMap<String, Dog>();
private Set<Dog> dogs = new HashSet<Dog>();
private String password;
private User user;
private String username;
private List<User> users = new ArrayList<User>();
public OgnlAction() {
users.add(new User(1));
users.add(new User(2));
users.add(new User(3));
dogs.add(new Dog("dog1"));
dogs.add(new Dog("dog2"));
dogs.add(new Dog("dog3"));
dogMap.put("dog100", new Dog("dog100"));
dogMap.put("dog101", new Dog("dog101"));
dogMap.put("dog102", new Dog("dog102"));
}
......(setters and getters)
}
//cat.java
public class Cat {
private Dog friend;
public Dog getFriend() {
return friend;
}
public void setFriend(Dog friend) {
this.friend = friend;
}
public String miaomiao() {
return "miaomiao";
}
}
//Dog.java
public class Dog {
private String name;
public Dog() {
}
public Dog(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "dog: " + name;
}
}