在struts2-hibernate中管理数据编辑的正确方法是什么?

时间:2012-05-29 05:16:56

标签: java hibernate struts2 ognl

我正在使用Struts2,Hibernate 3.5。我有复杂的对象图。因此,每次在编辑模式下提交数据时,我都需要确保请求中所有对象节点的ID都可用。因此,每次在UI上显示对象时,我都会将所有id作为jsp中的隐藏文件进行维护。这是管理数据编辑的正确方法吗?

2 个答案:

答案 0 :(得分:0)

您可以创建一个bean类并使用jsp表单注册它。这个bean类将包含jsp所具有的所有字段。每当你的jsp被修改时,这个bean类会自动更新自己。因此,您不必在隐藏字段中维护值。

答案 1 :(得分:0)

您的bean类将扩展ActionForm    公共类HolidayBookingForm扩展了ActionForm     {

    private String entryId;

    private String title;

    private String startDate;

    private String days;

    //getters and setters omitted

    public void readFrom(HolidayBooking booking)
    {
        if (booking != null)
        {
            if (booking.getEntryId() != 0)
                this.entryId = String.valueOf(booking.getEntryId());
            if (booking.getEntryId() != 0)
                this.days = String.valueOf(booking.getDays());
            if (booking.getStartDate() != null)
                this.startDate = new java.sql.Date(booking.getStartDate().getTime()).toString();
            this.title = booking.getTitle();
        }
    }

    public void writeTo(HolidayBooking booking)
    {
        if (booking == null)
            throw new IllegalArgumentException("Booking cannot be null");

        if (this.days != null)
            booking.setDays(Integer.parseInt(this.days));
        if (this.entryId != null)
            booking.setEntryId(Long.parseLong(this.entryId));

        // don't accept empty Strings
        if (this.title != null && this.title.trim().length() > 0)
            booking.setTitle(title);

        // assume validation has been handled
        if (this.startDate != null && this.startDate.trim().length() > 0)
            booking.setStartDate(java.sql.Date.valueOf(startDate));

    }


}