OGNL setValue target is 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.

Try this

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();
    }
}

The problem with your sample code is that your instance "customer" has a null user. So OGNL is essentially calling customer.getUser().setName("hello"), where "customer.getUser()" returns null.

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

上一篇: Hibernate错误会使@PostPersist方法中的实体存在

下一篇: OGNL setValue目标为null