Struts2-OGNL
OGNL表示式訪問值棧:
public class User { private int age = 8; public User() { } public User(int age) { super(); this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "user" + age; } }
Cat:
public class Cat {
//cat有一個朋友是dog
private Dog friend;
public Dog getFriend() {
return friend;
}
public void setFriend(Dog friend) {
this.friend = friend;
}
public String miaomiao() {
return "miaomiao";
}
}
Dog:
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; } @Override public String toString() { return "dog: " + name; } }
Action:
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")); } public String execute() { return SUCCESS; } public Cat getCat() { return cat; } public Map<String, Dog> getDogMap() { return dogMap; } public Set<Dog> getDogs() { return dogs; } public String getPassword() { return password; } public User getUser() { return user; } public String getUsername() { return username; } public List<User> getUsers() { return users; } public String m() { return "hello"; } public void setCat(Cat cat) { this.cat = cat; } public void setDogMap(Map<String, Dog> dogMap) { this.dogMap = dogMap; } public void setDogs(Set<Dog> dogs) { this.dogs = dogs; } public void setPassword(String password) { this.password = password; } public void setUser(User user) { this.user = user; } public void setUsername(String username) { this.username = username; } public void setUsers(List<User> users) { this.users = users; } }
struts.xml配置檔案:
<struts>
<package name="ognl" extends="struts-default">
<action name="ognl" class="com.mengxiangyue.struts2.ognl.OgnlAction">
<result>/ognl.jsp</result>
</action>
</package>
</struts>
通過OGNL在jsp頁面去訪問這些屬性:
<li>訪問值棧中的action的普通屬性: username = <s:property value="username"/> </li> <li>訪問值棧中物件的普通屬性(get set方法):<s:property value="user.age"/> | <s:property value="user['age']"/> | <s:property value="user[\"age\"]"/> | wrong: <%--<s:property value="user[age]"/>--%></li> <li>訪問值棧中物件的普通屬性(get set方法): <s:property value="cat.friend.name"/></li> <li>訪問值棧中物件的普通方法:<s:property value="password.length()"/></li> <li>訪問值棧中物件的普通方法:<s:property value="cat.miaomiao()" /></li> <li>訪問值棧中action的普通方法:<s:property value="m()" /></li>
OGNL訪問靜態方法,集合類:
S類:
public class S {
public static String STR = "STATIC STRING";
public static String s() {
return "static method";
}
}
<!--在我們訪問靜態方法的時候,需要使用@符號,跟上完整的類名,然後在跟@符號,加上具體的靜態方法或者屬性 在OGNL中使用@@符號可以直接使用Math類的方法,使用方法是@@+對應的Math方法 --> <li>訪問靜態方法:<s:property value="@[email protected]()"/></li> <li>訪問靜態屬性:<s:property value="@[email protected]"/></li> <li>訪問Math類的靜態方法:<s:property value="@@max(2,3)" /></li> <li>訪問普通類的構造方法:<s:property value="new com.mengxiangyue.struts2.ognl.User(8)"/></li> <li>訪問List:<s:property value="users"/></li> <li>訪問List中某個元素:<s:property value="users[1]"/></li> <li>訪問List中元素某個屬性的集合:<s:property value="users.{age}"/></li> <li>訪問List中元素某個屬性的集合中的特定值:<s:property value="users.{age}[0]"/> | <s:property value="users[0].age"/></li> <li>訪問Set:<s:property value="dogs"/></li> <li>訪問Set中某個元素:<s:property value="dogs[1]"/></li> <li>訪問Map:<s:property value="dogMap"/></li> <!-- 訪問Map中key或者value的時候都是使用的複數 --> <li>訪問Map中某個元素:<s:property value="dogMap.dog101"/> | <s:property value="dogMap['dog101']"/> | <s:property value="dogMap[\"dog101\"]"/></li> <li>訪問Map中所有的key:<s:property value="dogMap.keys"/></li> <li>訪問Map中所有的value:<s:property value="dogMap.values"/></li> <li>訪問容器的大小:<s:property value="dogMap.size()"/> | <s:property value="users.size"/> </li>
OGNL投影:
<!-- ?表示選擇某一個,這個表示選擇user.age=1的所有的user中的第一個 --> <li>投影(過濾):<s:property value="users.{?#this.age==1}[0]"/></li> <!-- ^ 表示第一個,這個表示選擇user.age>1的第一個user的age屬性 --> <li>投影:<s:property value="users.{^#this.age>1}.{age}"/></li> <!-- $ 表示最後一個,這個表示選擇user.age>1的最後一個user的age屬性 --> <li>投影:<s:property value="users.{$#this.age>1}.{age}"/></li> <li>投影:<s:property value="users.{$#this.age>1}.{age} == null"/></li>