Announcement Announcement Module
Collapse
No announcement yet.
Error when submitting form using command containing Integer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error when submitting form using command containing Integer

    I have an edit form with a hidden integer id value like so:

    <form:form method="post">
    ...
    <form:hidden path="id"/>
    </form:form>

    The command object associated with this page has an id property of type Integer.

    When I submit the form with an id value of 13, I get the following error:

    Failed to convert property value of type [java.lang.String[]] to required type [java.lang.Integer] for property id; nested exception is java.lang.NumberFormatException: For input string: "13,13"

    Makes sense as I would have difficulty converting 13,13 to an Integer as well!

    Viewing the page source shows a value of 13 as expected so where is it getting the value of 13,13?

    I've searched around and could only find one post related to Integers and command objects but it seemed like a different issue than this one.

    Thanks.

  • #2
    some more info:

    i changed the hidden field to a text field. when the page loads it displays a value of 13. i then change the value to 14 and submit. the page comes back with a value of {13,14} in the text field. i submit without changing any values and it comes back with {13, {13, 14}}. Something strange is happening during the attempt to bind.

    here's the complete jsp:

    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <html>
    <body>
    <form:form method="post">
    <form:input path="id"/>
    <input type="submit" name="submit" value="Save"></input>
    </form:form>
    </body>
    </html>

    I'm using a SimpleFormController which overrides formBackingObject() to set the initial text value. I don't have any CustomPropertyEditor classes.

    Anyone?

    Comment


    • #3
      Guess I'll be the only one posting in my own thread

      Here's my reasoning and fix for what seems to be happening:

      - an edit page is GET with a url like this: edit.jsp?id=13
      - formBackingObject is called to retrieve the object from the database
      - a jsp page is displayed with a hidden input field named id

      When this form is submitted back to the controller, it receives two request parameters named id (querystring and the hidden form field) and is subsequently converted into a String array during bind.

      I suppose that in this case, I could fix things by removing the hidden field. Instead I decided to rename the querystring parameter as I don't want to expose the key field in such a public place which could invite tampering. Now only the hidden id field is bound.

      All seems well now.

      Thanks to Srivathsa's timely post on How to avoid binding of Query Parameters.

      Comment

      Working...
      X