Struts2 OGNL

来源:互联网 发布:分销平台软件 编辑:程序博客网 时间:2024/06/07 00:27

Struts2 OGNL

//实体类public class User {    private String name;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public User(){}    public User(String name,int age){        this.name = name;        this.age = age;    }    @Override    public String toString() {        return "对象_" + this.name;    }}
//Action类public class action extends ActionSupport{    public static String STATICSTR="静态属性";    private User user;    private String forward;    private List<User> userList;    private Set<User> userSet;    private Map<String,Object> userMap;    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    public String getForward() {        return forward;    }    public void setForward(String forward) {        this.forward = forward;    }    public List<User> getUserList() {        return userList;    }    public void setUserList(List<User> userList) {        this.userList = userList;    }    public Set<User> getUserSet() {        return userSet;    }    public void setUserSet(Set<User> userSet) {        this.userSet = userSet;    }    public Map<String, Object> getUserMap() {        return userMap;    }    public void setUserMap(Map<String, Object> userMap) {        this.userMap = userMap;    }    public action(){    }    @Override    public String execute() throws Exception {        this.user = new User("张三",18);        this.userList = new ArrayList<User>();        this.userList.add(new User("张三",18));        this.userList.add(new User("李四",19));        this.userList.add(new User("王五",20));        this.userSet = new HashSet<User>();        this.userSet.add(new User("张三",18));        this.userSet.add(new User("李四",19));        this.userSet.add(new User("王五",20));        this.userMap = new HashMap<String,Object>();        this.userMap.put("key1",new User("张三",18));        this.userMap.put("key2",new User("李四",19));        this.userMap.put("key3",new User("王五",20));        return super.execute();    }    public String method_1(){        return "方法1";    }    public static String staticMethod(){        return "静态方法";    }}
<!-- 页面 -->    <p>访问Action类中的成员: <s:property value="user"/></p>    <p>访问Action类中的成员: <s:property value="user.name"/></p>    <p>访问Action类中的方法: <s:property value="method_1()"/> </p>    <p>访问Action类中的静态方法: <s:property value="@action.action@staticMethod()" /></p>    <p>访问Action类中的静态属性: <s:property value="@action.action@STATICSTR" /></p>    <!-- <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant> -->    <p>访问Math类中的静态方法: <s:property value="@@max(5,3)" /></p>    <p>访问普通类的构造方法: <s:property value="new action.action()" /></p>    <hr />    集合(List/Set/Map)都有的伪属性    <p>List中的大小: <s:property value="userList.size" /></p>    <p>访问Map是否空: <s:property value="userMap.isEmpty" /></p>    <hr />    <p>访问List: <s:property value="userList" /></p>    <p>访问List中某个元素: <s:property value="userList[0].name" /></p>    <p>访问List中某个元素的属性集合: <s:property value="userList.{name}" /></p>    <p>访问List中某个元素的属性集合中的某个值: <s:property value="userList.{name}[0]" /></p>    <hr />    <p>访问Set: <s:property value="userSet" /></p>    <p>访问Set的迭代器: <s:property value="userSet.iterator" /></p>    <hr />    <p>访问Map/keys/values: <s:property value="userMap" /></p>    <p>访问Map中某个元素: <s:property value="userMap.key1" /></p>    <p>访问Map中的keys Set: <s:property value="userMap.keys" /></p>    <p>访问Map中的values Set: <s:property value="userMap.values" /></p>    <hr />    <p>?投影:<s:property value="userList.{?#this.age>0}.{name}" /></p>    <p>^投影:<s:property value="userList.{^#this.age>0}.{name}" /></p>    <p>$投影:<s:property value="userList.{$#this.age==19}.{name}" /></p>    <p>[]:<s:property value="[0]" /></p>

最后的结果:

访问Action类中的成员: 对象_张三访问Action类中的成员: 张三访问Action类中的方法: 方法1访问Action类中的静态方法: 静态方法访问Action类中的静态属性: 静态属性访问Math类中的静态方法: 5访问普通类的构造方法: action.action@415fe218集合(List/Set/Map)都有的伪属性List中的大小: 3访问Map是否空: false访问List: [对象_张三, 对象_李四, 对象_王五]访问List中某个元素: 张三访问List中某个元素的属性集合: [张三, 李四, 王五]访问List中某个元素的属性集合中的某个值: 张三访问Set: [对象_王五, 对象_李四, 对象_张三]访问Set的迭代器: java.util.HashMap$KeyIterator@5e4ebd37访问Map/keys/values: {key3=对象_王五, key2=对象_李四, key1=对象_张三}访问Map中某个元素: 对象_张三访问Map中的keys Set: [key3, key2, key1]访问Map中的values Set: [对象_王五, 对象_李四, 对象_张三]?投影:[张三, 李四, 王五]^投影:[张三]$投影:[李四][]:[action.action@76fdfb0c, com.opensymphony.xwork2.DefaultTextProvider@1e2e9d9f]

ognl 详解
OGNL使用详解

0 0
原创粉丝点击