2012-05-29 63 views
0

我正在使用Struts2,Hibernate 3.5。我有複雜的對象圖。所以每次在編輯模式下提交數據時,我都需要確保所有對象節點的id在請求中可用。所以每次在UI上顯示對象時,我都會將所有ID作爲jsp中的隱藏文件保存。這是管理數據編輯的正確方法嗎?在struts2-hibernate中管理數據編輯的正確方法是什麼?

回答

0

你可以創建一個bean類並用jsp表單註冊它。這個bean類將擁有jsp所有的字段。每當你的jsp被修改時,這個bean類自動更新自己。所以你不必在隱藏字段中維護值。

+0

你是否有任何的例子嗎? – kunal

+0

對不起,現在我沒有任何例子,但你可以找到綁定表單元素與谷歌有關豆的文章。當然你會找到一些例子。 –

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)); 

    } 


} 
相關問題