Results 1 to 10 of 10

Thread: Problem with binding the form with ValueObject

  1. #1
    Join Date
    Sep 2005
    Posts
    9

    Default Problem with binding the form with ValueObject

    My form is with 5 textfields, 4 dropdownbuttons. When i filled the data in all the fields and submit it, then it is creating a new record in the corresponding table. If i leave any of the field blank then it throwing org.springframework.validation.BindException. Please let me know that how can i submit the form by leaving required fields blank.
    The corresponding code:

    public ModelAndView saveNewDevice(HttpServletRequest request,
    HttpServletResponse response) throws Exception {
    List list1 = new ArrayList();
    List list2 = new ArrayList();
    Object command = newCommandObject(DeviceInstanceVO.class); // ValueObject class to bind to the form
    bind(request, command);
    DeviceInstanceDaoJdbc dao = getDAO();
    String deviceTypeStr = request.getParameter("devType");
    Integer deviceTypeId = new Integer(deviceTypeStr);
    dao.setDevices(command);
    list1 = dao.getCreatedDevice(); // to retrieve the created record
    list2 = dao.getSelectedDeviceTypes(deviceTypeId); // to retrieve the created record

    Map model = new HashMap();
    model.put("deviceData", list1);
    model.put("deviceDataType", list2);

    return new ModelAndView("device/SaveDevice", model);
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default

    You want to leave required fields blank???

    Erwin

  3. #3
    Join Date
    Sep 2005
    Posts
    9

    Default

    Quote Originally Posted by klr8
    You want to leave required fields blank???

    Erwin
    I want to send the form data to database to create a new record of the related table in which some of the columns are defined as not null. I am doing this with the help of the valueobject . Initially the valueobject have to bind the form data, then this object sends to database to create new record.

    For Example:

    One of the Form Field
    <td><input type="text" name="maxConnPerNode" value="<core:out value="${status.value}"/>" maxlength="5"></td>

    The data member of the ValueObject is:

    private Integer maxConnPerNode;

    If i send null value to this field with then it is throwing org.springframework.validation.BindException with message

    [typeMismatch.command.maxConnPerNode,typeMismatch.m axConnPerNode,typeMismatch.java.lang.Integer,typeM ismatch];

    But i want to submit the form with some of the fields as null.

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

  5. #5
    Join Date
    Sep 2005
    Posts
    9

    Default

    Quote Originally Posted by yatesco
    I doen't want the messages to be displayed properly, but how to store data throught dataobject to the database table.
    Once again my code details are:

    JSP
    Code:
    <form name="NewDevice" method="post">
    <table border="1" width="600">
    <caption><b> NEW DEVICE </b></caption>
    <tr>
    <tD>Device Name</tD>
    <tD>
    <input type="text" name="devName" value="<core&#58;out value="$&#123;status.value&#125;"/>"/>
    </tD>
    </tr>
    <tr>
    <tD>Max Connections PerNode in a cluster</tD>
    <tD>
    <input type="text" name="maxConnPerNode" value="0" maxlength="5"/>
    </tD>
    </tr>
    <tr>
    <tD>Max connections per node</tD>
    <tD>
    <input type="text" name="maxConnPerNode" value="<core&#58;out value="$&#123;status.value&#125;"/>" maxlength="5"/>
    </tD>
    </tr>
    <tr>
    <tD>Maintainace Start Time <br>&#40;In minutes start from day&#41;</tD>
    <tD>
    <input type="text" name="mainStartTime" value="<core&#58;out value="$&#123;status.value&#125;"/>" maxlength="9"/>
    </tD>
    </tr>
    <tr>
    <tD>Maintainace End Time <br>&#40;In minutes start from day&#41;</tD>
    <tD>
    <input type="text" name="mainEndTime" value="<core&#58;out value="$&#123;status.value&#125;"/>" maxlength="9"/>
    </tD>
    </tr>
    </table>
    <input type="submit" name="save" value="Save" onClick="doAction&#40;'save'&#41;"/> 
    </form>
    DataObject to bind from form:
    Code:
        private String devName;    
        private Integer maxConnPerNode;
        private Integer mainEnabledFlag;
        private Integer mainStartTime;
        private Integer mainEndTime;   
    
         public void setDevName&#40;String devName&#41; &#123;
            this.devName = devName;
        &#125;
        public String getDevName&#40;&#41; &#123;
            return this.devName;
        &#125;
       public void setMainEndTime&#40;Integer mainEndTime&#41; &#123;
            this.mainEndTime = mainEndTime;
         &#125;
        public Integer getMainEndTime&#40;&#41; &#123;
            return mainEndTime;
        &#125;
        public Integer getMainStartTime&#40;&#41; &#123;
            return mainStartTime;
        &#125;
        public void setMainStartTime&#40;Integer mainStartTime&#41; &#123;
            this.mainStartTime = mainStartTime;
        &#125;
        public void setMainEnabledFlag&#40;Integer mainEnabledFlag&#41; &#123;
            this.mainEnabledFlag = mainEnabledFlag;
        &#125;
        public Integer getMainEnabledFlag&#40;&#41; &#123;
            return mainEnabledFlag;
        &#125;
        public Integer getMaxConnPerNode&#40;&#41; &#123;
            return maxConnPerNode;
        &#125;
        public void setMaxConnPerNode&#40;Integer maxConnPerNode&#41; &#123;
            this.maxConnPerNode = maxConnPerNode;
        &#125;
    Here is the method to bind form to ValueObject:
    Code:
    public ModelAndView saveNewDevice&#40;HttpServletRequest request,
            HttpServletResponse response&#41; throws Exception &#123;
            List list1 = new ArrayList&#40;&#41;;
            List list2 = new ArrayList&#40;&#41;;
           Object command = newCommandObject&#40;DeviceInstanceVO.class&#41;;
    
            bind&#40;request, command&#41;;  // i think here i am getting problem
    
            DeviceInstanceDaoJdbc dao = getDAO&#40;&#41;;
            String deviceTypeStr = request.getParameter&#40;"devType"&#41;;
            Integer deviceTypeId = new Integer&#40;deviceTypeStr&#41;;
            dao.setDevices&#40;command&#41;; //sends valueobject to insert as new record
            return new ModelAndView&#40;"device/SaveDevice", null&#41;;
        &#125;
    The related Table:

    Code:
    SQL> desc device_instance;
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     NAME                                      NOT NULL VARCHAR2&#40;128&#41; 
     MAX_CONN_PER_NODE                                  NUMBER&#40;9&#41;
     MAINTENANCE_ENABLED                                NUMBER&#40;1&#41;
     MAINTENANCE_START                                  NUMBER&#40;9&#41;
     MAINTENANCE_END                                    NUMBER&#40;9&#41;
    please let me know how can i submit form by leaving some of the fields null.

  6. #6

    Default

    Hi

    Did you get your answer. I'm facing the same problem now so if you resolved it let me know how.

    Thank you

  7. #7
    Join Date
    Aug 2004
    Posts
    26

    Default

    you need to register a custom number editor to allow for null numbers...

    Code:
        protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41;
                throws Exception &#123;
            binder.registerCustomEditor&#40;Integer.class, new CustomNumberEditor&#40;Integer.class, true&#41;&#41;;
        &#125;

  8. #8
    Join Date
    Sep 2005
    Posts
    9

    Default

    Quote Originally Posted by gcosgrave
    you need to register a custom number editor to allow for null numbers...

    Code:
        protected void initBinder&#40;HttpServletRequest request, ServletRequestDataBinder binder&#41;
                throws Exception &#123;
            binder.registerCustomEditor&#40;Integer.class, new CustomNumberEditor&#40;Integer.class, true&#41;&#41;;
        &#125;
    Thanks for your reply. After adding the given in the subclass of MultiActionController, the method is not invoking. Please let me have one full example.

  9. #9
    Join Date
    Aug 2004
    Posts
    26

    Default

    sorry I assumed you were using a subclass of AbstractCommandController.
    The following will work for MultiActionController if yare using Spring 1.2 or greater.

    Code:
        protected ServletRequestDataBinder createBinder&#40;ServletRequest request, Object command&#41;
                throws Exception &#123;
            ServletRequestDataBinder binder = null;
            if &#40;command instanceof XXXX&#41; &#123;
                binder = new ServletRequestDataBinder&#40;command, "command"&#41;;
                binder.registerCustomEditor&#40;Long.class, new CustomNumberEditor&#40;Long.class, true&#41;&#41;;
            return binder;
        &#125;
    [/code]

  10. #10
    Join Date
    Sep 2005
    Posts
    9

    Default

    Quote Originally Posted by gcosgrave
    sorry I assumed you were using a subclass of AbstractCommandController.
    The following will work for MultiActionController if yare using Spring 1.2 or greater.

    Code:
        protected ServletRequestDataBinder createBinder&#40;ServletRequest request, Object command&#41;
                throws Exception &#123;
            ServletRequestDataBinder binder = null;
            if &#40;command instanceof XXXX&#41; &#123;
                binder = new ServletRequestDataBinder&#40;command, "command"&#41;;
                binder.registerCustomEditor&#40;Long.class, new CustomNumberEditor&#40;Long.class, true&#41;&#41;;
            return binder;
        &#125;
    [/code]
    Thank you very much. Now it is working.

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  3. Replies: 0
    Last Post: Feb 22nd, 2005, 11:26 PM
  4. Replies: 9
    Last Post: Dec 8th, 2004, 03:12 PM
  5. Replies: 2
    Last Post: Aug 17th, 2004, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •