1
公共类客户{
    私人用户用户;
    私有字符串名称;

    公共用户 getUser() {
        返回用户;
    }

    公共无效setUser(用户用户){
        this.user = 用户;
    }

    公共字符串 getName() {
        返回名称;
    }

    公共无效集合名称(字符串名称){
        this.name = 名称;
    }

}

公共类用户{
    私有字符串名称;

    公共字符串 getName() {
        返回名称;
    }

    公共无效集合名称(字符串名称){
        this.name = 名称;
    }

}


公共静态无效主要(字符串[]参数){
        尝试 {
            客户客户=新客户();

            对象树 = Ognl.parseExpression("user.name");

            Ognl.setValue(树,客户,“你好”);

        } 捕捉(OgnlException e){
            e.printStackTrace();
        }
}

ognl.OgnlException: setProperty(null, "name", hello) 的目标为空

如何让ognl自动创建用户。
4

1 回答 1

1

尝试这个

public static void main(String[] args) {
    try {
        Customer customer = new Customer();
        User user = new User();
        customer.setUser(user);

        Object tree = Ognl.parseExpression("user.name");

        Ognl.setValue(tree, customer, "hello");

    } catch (OgnlException e) {
        e.printStackTrace();
    }
}

您的示例代码的问题是您的实例“客户”有一个空用户。所以 OGNL 本质上是调用 customer.getUser().setName("hello"),其中 "customer.getUser()" 返回 null。

于 2011-06-05T06:37:08.630 回答