对于OGNL的介绍读者自己去百度,里面讲的还是比较详细的,这里只是列出一些简单的使用能够。
首先将使用源码贴出来:这里面从Action类开始看就可以了,其他的类都是一些属性加上getter和setter方法,虽然比较长,但是没什么内容。
package com.mengxiangyue.struts2.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类:
package com.mengxiangyue.struts2.ognl;
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类:
package com.mengxiangyue.struts2.ognl;
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类:
package com.mengxiangyue.struts2.ognl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
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配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<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>