Announcement Announcement Module
Collapse
No announcement yet.
SimpleFormController creates bean with twice the value during form sumission Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SimpleFormController creates bean with twice the value during form sumission

    I am using SimpleFormController for creating a form and then submitting the form.

    I have a FBO that is initialized by formBackingObject()

    Code:
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
     Order order = new Order();
     order.setSymbol("CSCO");
     return order;
    }
    Lets say, At this point the value of symbol is "CSCO"

    The JSP looks like this,

    Code:
    <form:form method="post" action="" commandName="Order" >   
        <tr>
        		<td>Enter Symbol</td>
            	<td>
            		<form:input  path="symbol"  />
            	</td>
                <td>
                    <font color="red"><form:errors path="symbol" /></font>
                </td>
        </tr>
     </form:form>
    When the form is displayed, the input text box is pre-populated with text "CSCO"

    Until now everything is fine...

    Now when the user submits the form.

    In the onSubmit() method, The value of symbol is "CSCO,CSCO"

    This is very bad.

    In case the user changes the input text box value to "XXX",
    I get "CSCO,XXX" for symbol.

    Is this expected behavior?
    If so, what is the right approach for handling the form?

  • #2
    Read this.

    Comment


    • #3
      It doesn't help if clear the formbacking object, because I the command already contains what the user supplied + value from formbacking object.

      The issue is, I am using the same controller for displaying the form and process the form. The URL is also the same.

      Code:
      BaseCommandController{
         bindAndValidate(){
      		ServletRequestDataBinder binder = createBinder(request, command);
      		BindException errors = new BindException(binder.getBindingResult());
      		if (!suppressBinding(request)) {
      			binder.bind(request);
      			onBind(request, command, errors);
      			if (this.validators != null && isValidateOnBinding() && !suppressValidation(request, command, errors)) {
      				for (int i = 0; i < this.validators.length; i++) {
      					ValidationUtils.invokeValidator(this.validators[i], command, errors);
      				}
      			}
      			onBindAndValidate(request, command, errors);
      		}
      		return binder;
         }
      }
      So, the binder.bind() method actually adds the value from request again!

      Once solution would be to create a different url for posting the form,

      Comment


      • #4
        The command object IS the formBackingObject. The first time the form is submitted, the value of the command object is saved in session, then when the second time the form is displayed (after onSubmit) the value of the text field is picked up from the command object which is picked up from the session which is why it is neccessary to clear it.

        Comment

        Working...
        X