2

我正在尝试以下代码来获取spa对象字段中的数据。因此,我正在使用以下代码,但它显示null值并给出以下错误

Error setting expression 'spa.amount' with value '[Ljava.lang.String;@10dd65e'
ognl.OgnlException: target is null for setProperty(null, "amount", [Ljava.lang.String;@10dd65e) 

JSP代码:

<s:form action="UpdatedPaid" method="post">  
        <s:if test="paidList.size >  0"> 
            <s:iterator value="paidList" var="pdlst">
                <div class="box">
                    <span class="label">Amount</span>
                    <span class="ib"> 
                        <s:hidden name="spa.id" id="paidId">
                            <s:param name="value">
                                <s:property value="paidId"/>
                            </s:param>
                        </s:hidden>
                        <s:textfield name="spa.amount" id="amount">
                            <s:param name="value">
                                <s:property value="amount"/>
                            </s:param>
                        </s:textfield>
                    </span>
                </div>
                <div class="box">
                    <span class="label">Payment Date</span>
                    <span class="ib"> <s:textfield name="spa.paymentDate" id="paymentDate">
                            <s:param name="value">
                                <s:property value="paymentDate"/>
                            </s:param>
                        </s:textfield>
                    </span>
                </div>
                <div class="box">
                    <span class="label">Payment Mode</span>
                    <span class="ib">
                        <s:textfield name="spa.paymentMode" id="payment_mode" readonly="true">
                            <s:param name="value">
                                <s:property value="payment_mode"/>
                            </s:param>
                        </s:textfield>
                    </span>
                </div>
            </s:iterator>
        </s:if>
    </s:form>

我的动作课:

package iland.payment;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import iland.hbm.SupplierPaidDetails;

public class hbmCashAction extends ActionSupport implements ModelDriven<SupplierPaidDetails> {

    SupplierPaidDetails spa = new SupplierPaidDetails();

    public SupplierPaidDetails getSpa() {
        return spa;
    }

    public void setSpa(SupplierPaidDetails spa) {
        this.spa = spa;
    }

    public String update() {
        System.out.println("--------");
        System.out.println(spa.getId() + " " + spa.getPaymentMode() + " " + spa.getAmount());
        System.out.println("--------");
        return SUCCESS;
    }

    @Override
    public SupplierPaidDetails getModel() {
        return spa;
    }

}

SupplierPaidDetails班级:

public class SupplierPaidDetails  implements java.io.Serializable {


     private Long id;
     private SupplierPaymentDetails supplierPaymentDetails;
     private Float amount;
     private String paymentMode;
     private Date paymentDate;
     private Date addDate;
     private String status;
     private Set supplierPaidOnlines = new HashSet(0);
     private Set supplierPaidCashes = new HashSet(0);
     private Set supplierPaidChecks = new HashSet(0);

    public SupplierPaidDetails() {
    }

    
    public SupplierPaidDetails(SupplierPaymentDetails supplierPaymentDetails, Date addDate) {
        this.supplierPaymentDetails = supplierPaymentDetails;
        this.addDate = addDate;
    }
    public SupplierPaidDetails(SupplierPaymentDetails supplierPaymentDetails, Float amount, String paymentMode, Date paymentDate, Date addDate, String status, Set supplierPaidOnlines, Set supplierPaidCashes, Set supplierPaidChecks) {
       this.supplierPaymentDetails = supplierPaymentDetails;
       this.amount = amount;
       this.paymentMode = paymentMode;
       this.paymentDate = paymentDate;
       this.addDate = addDate;
       this.status = status;
       this.supplierPaidOnlines = supplierPaidOnlines;
       this.supplierPaidCashes = supplierPaidCashes;
       this.supplierPaidChecks = supplierPaidChecks;
    }
   
    public Long getId() {
        return this.id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    public SupplierPaymentDetails getSupplierPaymentDetails() {
        return this.supplierPaymentDetails;
    }
    
    public void setSupplierPaymentDetails(SupplierPaymentDetails supplierPaymentDetails) {
        this.supplierPaymentDetails = supplierPaymentDetails;
    }
    public Float getAmount() {
        return this.amount;
    }
    
    public void setAmount(Float amount) {
        this.amount = amount;
    }
    public String getPaymentMode() {
        return this.paymentMode;
    }
    
    public void setPaymentMode(String paymentMode) {
        this.paymentMode = paymentMode;
    }
    public Date getPaymentDate() {
        return this.paymentDate;
    }
    
    public void setPaymentDate(Date paymentDate) {
        this.paymentDate = paymentDate;
    }
    public Date getAddDate() {
        return this.addDate;
    }
    
    public void setAddDate(Date addDate) {
        this.addDate = addDate;
    }
    public String getStatus() {
        return this.status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    public Set getSupplierPaidOnlines() {
        return this.supplierPaidOnlines;
    }
    
    public void setSupplierPaidOnlines(Set supplierPaidOnlines) {
        this.supplierPaidOnlines = supplierPaidOnlines;
    }
    public Set getSupplierPaidCashes() {
        return this.supplierPaidCashes;
    }
    
    public void setSupplierPaidCashes(Set supplierPaidCashes) {
        this.supplierPaidCashes = supplierPaidCashes;
    }
    public Set getSupplierPaidChecks() {
        return this.supplierPaidChecks;
    }
    
    public void setSupplierPaidChecks(Set supplierPaidChecks) {
        this.supplierPaidChecks = supplierPaidChecks;
    }
}

编辑:

我想设置SupplierPaidDetails类的所有字段,包括(supplierPaidOnlines, supplierPaidCashessupplierPaidChecks这些可以包含许多实例)和supplierPaymentDetailsJSP 的设置。

如何解决上述错误?

4

1 回答 1

1

根据错误

Error setting expression 'spa.amount' with value '[Ljava.lang.String;@10dd65e'

似乎 OGNL 正在尝试将类型的值设置为beanString[]amount属性。spa

进一步阅读

ognl.OgnlException: target is null for setProperty(null, "amount", [Ljava.lang.String;@10dd65e)  

看起来spareference is null,所以 OGNL 无法将值设置为 anull因为它会抛出空指针异常。

要解决此问题,您应该初始化spa.

接下来问题出现了,因为您使用了模型驱动。模型驱动拦截器将模型推送到值堆栈顶部,这是一个 OGNL 根。并且您已经初始化了spa. 似乎 OGNL 即使从值堆栈的顶部向下搜索堆栈也找不到spa. 因此,要直接访问动作属性,您可以使用此答案Passing parameters to action through ModelDriven in Struts 2.3.16 中描述的技术。

OGNL 所期望的方法spa

public void setAmount(String[] amounts) {
   this.amounts = amounts;
}

你没有这样的方法,你可能不希望收到一个字符串数组。但问题是你正在这样做。因为同名的参数被打包成String[]. 要为每个迭代对象设置不同的名称,您应该使用索引属性名称。您可以在此答案中找到索引属性的示例Repopulate an ArrayList from JSP with Struts 2

您的属性可以直接从模型中获得,因为您的操作是模型驱动的,并且它应该具有List<SupplierPaymentDetails> paidList迭代的。您可以使用此属性来填充提交的值或使用另一个具有相同类型的值。

您可以学习ModelDriven 示例,但它不使用索引属性。要正确使用它,您可以使用此答案How to pass a Map<ObjectA, List<ObjectB>> to action in Struts 2

于 2014-05-02T11:25:42.133 回答