【问题标题】:What is the right way to manage data edits in struts2-hibernate?在 struts2-hibernate 中管理数据编辑的正确方法是什么?
【发布时间】:2012-05-29 05:16:56
【问题描述】:

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

【问题讨论】:

    标签: java hibernate struts2 ognl


    【解决方案1】:

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

    【讨论】:

    • 你有例子吗?
    • 抱歉,我现在没有任何示例,但您可以在 Google 上找到将表单元素与 bean 相关的文章。你肯定会找到一些例子。
    【解决方案2】:

    您的 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));
    
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2012-11-01
      相关资源
      最近更新 更多