0

I am using Struts2, Hibernate 3.5. I have complex object graph. So every time data is submitted in a edit mode, i need to make sure id of the all the object nodes are available in the request. So every time object are displayed on the UI, i am mainting the all the ids as a hidden fileds in the jsp. Is this correct approach for managing data edits?

kunal
  • 779
  • 6
  • 25

2 Answers2

0

You can make a bean class and register that with a jsp form. This bean class will have all the fields which jsp has. Whenever your jsp will be modified this bean class automatically update itself. So you will not have to maintain values in hidden fields.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
0

Your bean class will extend ActionForm public class HolidayBookingForm extends 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));

    }


}
Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37