Struts2 OGNL

本文详细介绍了Struts2框架中OGNL表达式的应用,包括如何通过OGNL访问Action类成员、方法及静态资源,操作集合类型的伪属性,并展示了OGNL在处理List、Set和Map等集合类型数据时的强大功能。

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使用详解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值