Announcement Announcement Module
Collapse
No announcement yet.
Boolean value in SpringMVC form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Boolean value in SpringMVC form

    I have a boolean attribute in one of my classes that appears web form as a radio group. I'm using MySQL as the database so the value is store as a
    BIT but when the form is displayed the value displayed in
    the form is either true or false. The code for my form is as follows:

    <spring:bind path="conditionType.manditory">
    <label><input type="radio" name="manditory" value="false" <c:if test="${conditionType.manditory == status.value}">checked="checked"</c:if> />False</label>
    <label><input type="radio" name="manditory" value="true" <c:if test="${conditionType.manditory == status.value}">checked="checked"</c:if> />True</label>
    <span class="fieldError"><c:out value="${status.errorMessage}"/></span>
    </spring:bind>

    This populates the webForm correctly but when I try to edit this value... by replacing true with
    false or visa versa then try to save the form the error message

    Cannot convert property value of type [java.lang.String] to
    required type [java.lang.Boolean] for property 'manditory'

    I have read through the documentation but I think I must be doing something incorrectly because I'm still getting the same error. Can anyone point me in the right direction or point out where Im going wrong... I've over written the onBind method for my Controller class to what is below. Any ideas?

    protected void onBind(HttpServletRequest request,Object command, BindException errors)
    throws java.lang.Exception {

    ConditionType conditionType = (ConditionType) command;

    String value = request.getParameter("manditory");

    if ( value.trim().equalsIgnoreCase(TRUE) ) {
    conditionType.setManditory(Boolean.TRUE);
    }
    else {
    conditionType.setManditory(Boolean.FALSE);
    }
    }

  • #2
    I think you can use a propertyEditory for boolean values.
    Spring automatically converts from Boolean to the String representation of Boolean.
    See, for example, this thread: http://forum.springframework.org/showthread.php?t=10243

    Hope this helps,
    regards,
    Luigi
    Last edited by Rod Johnson; Jan 18th, 2006, 11:00 AM.

    Comment


    • #3
      I also think that there is a problem on your JSTL test condition, that won't let display the correct value after saving. This is however not related to your bind issue. I will definitly use a propertyEditor registered in your initBinder method because onBind, is for post bind processing, so it is executed after the bind spring process.

      Code:
      <c&#58;if test="$&#123;conditionType.manditory == status.value&#125;">
      It means that if conditionType.manditory is defined for the JSTL scope, which I am not so sure, that conditionType.manditory = value of your bind tag, which is also conditionType.manditory. So it will always return true.

      I will use something like this inside your bind tag:

      Code:
       <c&#58;if test="$&#123;status.value==true&#125;"> checked </c&#58;if>

      Comment

      Working...
      X