Results 1 to 9 of 9

Thread: An example of binding a value radio buttons please

  1. #1

    Default 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. #2
    Join Date
    Oct 2004
    Location
    Rotterdam, Netherlands
    Posts
    90

    Default

    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

  3. #3

    Default

    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.

  4. #4
    Join Date
    Aug 2004
    Location
    Atlanta, GA
    Posts
    129

    Default

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

  5. #5

    Default

    Quote 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

  6. #6

    Default

    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)

  7. #7

    Default

    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.

  8. #8
    Join Date
    Aug 2004
    Location
    Germany, Magdeburg
    Posts
    279

    Default

    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)

  9. #9

    Default

    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!)

Similar Threads

  1. Enum binding not showing default
    By ge0ffrey in forum Swing
    Replies: 3
    Last Post: Jan 23rd, 2006, 09:08 AM
  2. Replies: 7
    Last Post: Sep 13th, 2005, 01:45 AM
  3. Replies: 1
    Last Post: May 16th, 2005, 08:48 PM
  4. Replies: 0
    Last Post: Jan 6th, 2005, 08:19 AM
  5. Replies: 2
    Last Post: Aug 17th, 2004, 04:16 PM

Posting Permissions

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