设置表达式’spa.amount’时出错,值为'[Ljava.lang.String; @ 10dd65e’)

我正在尝试使用代码来获取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代码:

   0">  
Amount
Payment Date
Payment Mode

我的动作类

 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 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所有字段,包括set( supplierPaidOnlinessupplierPaidCashessupplierPaidChecks这些可以包含许多实例)和来自jsp的supplierPaymentDetails

如何解决上述错误。

根据错误

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

似乎OGNL试图将类型String[]的值设置为spa bean的属性。

进一步阅读

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

看起来像spa引用是null ,因此OGNL无法将值设置为null因为它将抛出空指针exception。

要解决此问题,您应该初始化spa的引用。

接下来出现问题是因为您使用了模型驱动。 模型驱动的拦截器将模型推送到值堆栈的顶部,这是一个OGNL根。 你已经初始化了spa的实例。 似乎OGNL即使从堆栈顶部搜索堆栈也无法找到spa的实例。 因此,要直接访问操作属性,您可以使用本答案中描述的技术将参数传递给Struts 2.3.16中的ModelDriven 。

OGNL对spa参考预期方法是

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

你没有这样的方法,你可能不希望收到一个字符串数组。 但问题是你在做这件事。 因为具有相同名称的参数被打包到String[] 。 要使用与每个迭代对象相对应的不同名称,应使用索引属性名称。 您可以在此答案中找到索引属性的示例。 使用Struts 2从JSP重新填充ArrayList 。

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

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