Results 1 to 3 of 3

Thread: Boolean value in SpringMVC form

  1. #1

    Default 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. #2
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    6

    Default

    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 at 11:00 AM.

  3. #3
    Join Date
    Aug 2004
    Location
    Paris
    Posts
    43

    Default

    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>

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 17
    Last Post: Jan 2nd, 2007, 01:43 PM
  3. Replies: 9
    Last Post: May 4th, 2006, 09:53 AM
  4. Replies: 0
    Last Post: Jun 10th, 2005, 08:22 AM
  5. Boolean, Date, Integer Fields on Form
    By steve_smith in forum Swing
    Replies: 2
    Last Post: Oct 27th, 2004, 01:34 AM

Posting Permissions

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