绑定错误与列表

时间:2014-02-05 14:02:32

标签: java spring-mvc

我生成了一个表单:

<form:form action="${contextPath}/draw/constraints.do" method="post" modelAttribute="order"> <c:forEach items="${order.myDrawsAsArray}" var="draw" varStatus="status">
<label class="radio-inline"><form:radiobutton path="myDrawsAsArray[${status.index}].readable" value="true" /> yes</label>
<label class="radio-inline"><form:radiobutton path="myDrawsAsArray[${status.index}].readable" value="false" /> no</label>
</c:forEach></form:form>

当我提交它来更新我的实体时,我得到以下异常:

  

org.springframework.web.util.NestedServletException:Request   处理失败;嵌套异常是   org.springframework.beans.InvalidPropertyException:无效的属性   bean类的[myDrawsAsArray [0]'[com.entity.Order3d]:Getter   属性'myDrawsAsArray'抛出异常;嵌套异常是   java.lang.reflect.InvocationTargetException   org.springframework.beans.InvalidPropertyException:无效的属性   bean类的[myDrawsAsArray [0]'[com.entity.Order3d]:Getter   属性'myDrawsAsArray'抛出异常;嵌套异常是   java.lang.reflect.InvocationTargetException   java.lang.reflect.InvocationTargetException   显示java.lang.NullPointerException     com.entity.Order3d.getMyDrawsAsArray(Order3d.java:121)

我的控制器是这样的:

@Controller
@RequestMapping("/draw")
public class PrintingController {
    @RequestMapping(value="/constraints")
    public String constraints(
                @ModelAttribute Order3d order,
                @RequestParam("order") int id,
                @RequestParam(value="save", required=false) String save,
                Model m) {
        Session s=HibernateUtils.getSessionFactory().openSession();
        if(save!=null) {
            System.out.println(order.getMyDraws());
            for(DrawFile df : order.getMyDraws())
                s.saveOrUpdate(df);
        }
        Order3d o=(Order3d)s.createCriteria(Order3d.class).add(Restrictions.eq("id", id)).uniqueResult();
        m.addAttribute("order", o);
        s.close();
        return "3dconstraints";
    }
}

如果您需要,我也会发布我的实体:

@Entity
@Table (name="order3d")
public class Order3d implements Serializable {
    private static final long serialVersionUID = -2241346447352903470L;
    public enum State {DEMAND, ESTIMATED, PAYED, PENDING, PRODUCED, SENT, DELIVERED};
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column (name="id")
    private int id;
    @OneToMany(mappedBy="order3d", fetch = FetchType.EAGER, cascade=CascadeType.ALL)
    private Set<DrawFile> myDraws;
public Set<DrawFile> getMyDraws() {
    return myDraws;
}
public List<DrawFile> getMyDrawsAsList() {
    return new ArrayList<DrawFile>(myDraws);
}
public Object[] getMyDrawsAsArray() {
    return myDraws.toArray(); //line 121
}
    //other getters & setters
    public Order3d() {}
}
@Entity
@Table (name="draw", uniqueConstraints=@UniqueConstraint(columnNames="hashname"))
public class DrawFile implements Serializable {
    private static final long serialVersionUID = -9024754876558087847L;
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column (name="id")
    private int id;
    @Column (name="hashname", columnDefinition="CHAR(64)")
    private String hashname;
    @Column (name="filename")
    private String filename="";
    @Column (name="readable", columnDefinition = "BIT", length = 1)
    private Boolean readable;
    //getters & setters
    public DrawFile() {}
}

我在网上搜索但我找不到解决方案。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试将getter / setter添加到Order3d和DrawFile类的字段中怎么样?

如果字段为

,则getter和setter必须匹配字段名称
List<DrawFile> myDraws;

那么getter / setter必须是:(它不能是getMyDrawsAsList())

public List<DrawFile> getMyDraws() {
    return myDraws;
}


public void setMyDraws(List<DrawFile> myDraws) {
    this.myDraws = myDraws;
}