簡體   English   中英

OGNL setValue 目標是 null

[英]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.

嘗試這個

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

您的示例代碼的問題是您的實例“客戶”有一個 null 用戶。 所以OGNL本質上是調用customer.getUser().setName("hello"),其中"customer.getUser()"返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM