Announcement Announcement Module
Collapse
No announcement yet.
An example of binding a value radio buttons please Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • An example of binding a value radio buttons please

    Please, Ive looked everywhere but cant find an example on doing this correctly.

    I have a class/bean which contains a property of type which can be 0,1,2 mapping to Type0,Type1,Type2 I want to render three radio buttons to allow the user to choose the required value.

    Dont understand how to do the mapping and whether or not this is reference data. (I do understand how to bind simple propertys such as name)

  • #2
    Paul.

    mapping radio buttons should be done like this:

    Code:
    <spring&#58;bind path="command.gender">
         <input type="radio" name="gender" value="M" <c&#58;if test='$&#123;status.value == "M"&#125;'>checked</c&#58;if> > Male
         <span class="error"><c&#58;out value="$&#123;status.errorMessage&#125;"/></span>
         <br />
         <input type="radio" name="gender" value="F" <c&#58;if test='$&#123;status.value == "F"&#125;'>checked</c&#58;if> > Female
         <br />
    </spring&#58;bind>
    The conversion between the String values and your custom types can be done by creating and registering custom editors, of type java.beans.PropertyEditor. This mechanism is described extensively in the Spring reference documentation and in other posts on this forum.

    Good luck!

    -- Thomas

    Comment


    • #3
      Thanks but this does not seem like a very good example, it is hardcoded to only show 2 radio buttons and their values are hardcoded.

      I have almost managed to get things working with the following code which is flexible regarding number of radio buttons and their value. The trouble i can never get the if statement to evaluate to true even though the values appear equal I have printed the value of status.value to confirm this.
      Code:
       <tr>
                   <spring&#58;bind path="Portfolio.type">              
                   <td class="tablecolheader">Portfolio Type</td>             
      	           <td><c&#58;out value="$&#123;status.value&#125;"/>
      	             <c&#58;forEach items="$&#123;portfoliotypes&#125;" var="portfoliotype">               
      	                <input type="radio" 
      	                    name="<c&#58;out value="$&#123;status.expression&#125;"/>"
      	                    value="<c&#58;out value="$&#123;portfoliotype.id&#125;"/>"	                    
      	                    <c&#58;if test='$&#123;status.value&#125; =="$&#123;portfoliotype.id&#125;'>checked</c&#58;if>>	                
      	                <c&#58;out value="$&#123;portfoliotype.name&#125;"/>	                   
      	             </c&#58;forEach>
      	           </td>
      	         </spring&#58;bind>
                 </tr>
      this is rendered to
      Code:
       <td class="tablecolheader">Portfolio Type</td>             
      	           <td>2
      	                            
      	                <input type="radio" 
      	                    name="type"
      	                    value="1"	                    
      	                    >	                
      	                Long	                   
      	                            
      	                <input type="radio" 
      	                    name="type"
      	                    value="2"	                    
      	                    >	                
      	                Short	                   
      	                            
      	                <input type="radio" 
      	                    name="type"
      	                    value="3"	                    
      	                    >	                
      	                Long/Short	                   
      	             
      	           </td>
      Note there is no checked.

      Comment


      • #4
        try this:
        Code:
                        
        <c&#58;if test="$&#123;status.value == portfoliotype.id&#125;">checked</c&#58;if>>
        HTH

        Comment


        • #5
          Originally posted by rstearns01
          try this:
          Code:
                          
          <c&#58;if test="$&#123;status.value == portfoliotype.id&#125;">checked</c&#58;if>>
          HTH
          Thanks that works

          Comment


          • #6
            Now got round to actually using this radio control, but it does not appear to be binding. After submitting the form the value entered for portfolio.type is lost and having now added validation it complains that no value has been entered. Any idea why this would be ?

            (Portfolio.type is an instance of a PortfolioType class within an instance of Portfolio class and contains and id and name field, I initilise it within formBackingObject so it is not null)

            Comment


            • #7
              FYI it wasnt working because my setID() method on type class was private. However it would be nice if Spring could have reported to me in some way that it had a found a matching property and getter but not setter to aid debugging.

              Comment


              • #8
                This sounds like being a good add. Can you check the JIRA and if no feature request exist, can you please raise one?


                Cheers,

                Martin (Kersten)

                Comment


                • #9
                  OK Done

                  http://opensource.atlassian.com/proj...browse/SPR-936

                  (BTW first time Ive looked at the JIRA Bug Tracking System, looks very good indeed!)

                  Comment

                  Working...
                  X