【问题标题】:OGNL setValue target is nullOGNL setValue 目标为空
【发布时间】:2011-09-08 15:15:21
【问题描述】:
公共类客户{ 私人用户用户; 私有字符串名称; 公共用户 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自动创建用户。

【问题讨论】:

    标签: java ognl


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多