【问题标题】:ognl.OgnlException: source is null for getProperty(null, "0") Errorognl.OgnlException: source is null for getProperty(null, "0") 错误
【发布时间】:2013-11-07 23:57:42
【问题描述】:

我可以显示值并对其进行编辑,但是当我提交表单时会遇到以下错误。

Java

public Class Baskets {
  private Basket basket;
  ...

  public String edit(){
    System.err.println("item>>>" + this.basket.getItems().get(0).getProduct().getPrice());
    ...
  }
  getter and setters
}

JSP

 <form name="edit" method="POST" action="edit">
      <table border="4">
        <thead><td>Product</td><td>Price 2</td><td>User</td><td>Time</td></thead>  
        <tbody>
           <s:iterator value="basket.items" var="item" status="cur">
             <tr>
                <s:textfield type="hidden" id="item[%{#cur.index}].id" name="item[%{#cur.index}].id" value="%{id}" theme="simple"/>
                  <td>

                      Price : <s:textfield id="item[%{#cur.index}].product.price" name="item[%{#cur.index}].product.price" value="%{prodcut.price}" theme="simple"/>
                      <br/>
                      Name: <s:label value="%{product.name}" theme="simple"/> 

                  </td>

                  <td>
                      <s:textfield name="item[%{#cur.index}].product.price2" 
                                   id="item[%{#cur.index}].product.price2" 
                                   value="%{product.price2}" theme="simple"/>
                  </td>

                  <td><s:label value="%{name}" theme="simple"/> </td>
                  <td><s:label value="%{time}" theme="simple"/> </td>

            </tr>
         </s:iterator>
       </tbody>
       </table>        
    <input id="edit" type="submit" name="action" value="Edit"/>
 </form>

控制台出错

   ognl.OgnlException: source is null for getProperty(null, "0")

浏览器出错

java.lang.NullPointerException

com.myproject.controller.Baskets.edit(Baskets.java:100)

第 100 行如下

 System.err.println("item>>>" + this.basket.getItems().get(0).getProduct().getPrice());

HTML

 <form name="edit" method="POST" action="edit">
    <table border="4">
           <thead><td>Product</td><td>Price 2</td><td>User</td><td>Time</td></thead>
                <tbody>
                  <tr>
                    <input type="hidden" name="item[0].id" value="16" id="item[0].id"/>
                    <td>Price : <input type="text" name="item[0].product.price"    
                                   value="55" id="item[0].product.price"/>
                        <br/>
                       Name: <label id="">Product 1</label> </td>

                   <td><input type="text" name="item[0].product.price2" value="1000" 
                                          id="item[0].product.price2"/></td>

                            <td><label id="">User1</label> </td>
                            <td><label id="">9:31:03 AM</label> </td>

                  </tr>

                <tr>  
                   <input type="hidden" name="item[1].id" value="17" id="item[1].id"/>
                   <td>Price : <input type="text" name="item[1].product.price" 
                                  value="60" id="item[1].product.price"/>
                    <br/>
                                Name: <label id="">Product 2</label> </td>

                   <td><input type="text" name="item[1].product.price2" value="1000" 
                             id="item[1].product.price2"/></td>

                            <td><label id="">User1</label> </td>
                            <td><label id="">11:25:55 AM</label> </td>

                </tr>

                </tbody>
            </table>
            <label id="balance"></label>        
            <input id="edit" type="submit" name="action" value="Edit"/>
        </form>

【问题讨论】:

  • "item[ {#cursor.index}].product.price" 丢失 %
  • @coding_idiot 如果我这样说,生成的 HTML 将是 %item[0].product.price 并且会遇到同样的错误

标签: java jsp struts2 iterator ognl


【解决方案1】:
<s:textfield id="item[ {#cursor.index}].product.price" 
                               name="item[%{#cursor.index}].product.price" 
                               value="%{product.price}" /> 

好像漏掉了%basket.item,请指正

<s:textfield id="basket.item[%{#cursor.index}].product.price" 
                               name="basket.item[%{#cursor.index}].product.price" 
                               value="%{product.price}" /> 

【讨论】:

  • 当我添加时,生成的 HTML 是 并且错误是 ognl.OgnlException: source is null for getProperty(null, "0")
  • 生成的 html will beis ?您是否尝试过进行更改并提交表格?你仍然得到同样的错误吗?如果是,那么很确定一个字段的名称是错误的,请发布完整的表格和完整的html。
  • this.basket. - 第 100 行 - 什么是篮子?
  • basket 是购物车对象,每个购物车有不同的商品,每个商品中是一个产品和一个时间和用户名,每个产品有两个价格。
  • 那么是字段名不对,我们需要把名字改成basket.item[%{cursor.index}].id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-20
  • 2022-01-15
  • 2020-09-24
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
相关资源
最近更新 更多