OGNL setValue目标为null

public class Customer {
    private User user;
    private String name;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}


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

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

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

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

ognl.OgnlException: target is null for setProperty(null, "name", hello)

how to let ognl to create user auto.

尝试这个

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。

链接地址: http://www.djcxy.com/p/54069.html

上一篇: OGNL setValue target is null

下一篇: How to deserialize a list using GSON or another JSON library in Java?