Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Newbie Spring MVC problem: Collections doesn't binding from form to model

  1. #1
    Join Date
    Nov 2005
    Location
    Valparaiso, Chile
    Posts
    12

    Unhappy Newbie Spring MVC problem: Collections doesn't binding from form to model

    Hi!
    I'm newbie in Spring MVC, and i have a problem that making me crazy!
    I have a form with a <select> multiple (the values of a Collection), but when i view the database, there are all the fields of the class, but not the Collection. My code is this:

    Bean: Libro, parent of the relationship
    Code:
    public class Libro {
    
    	
        private Long isbn;
        private String titulo = "";
          .......  
       private Set<Autor> autores = null;  }

    Bean: Autor, childs

    Code:
    public class Autor {
    
        private String apellidoPaterno = "";
        private String apellidoMaterno = "";
        private String nombres = ""; 
        private Long id; }
    Controller: AdminLibroRegisterFormController
    Code:
    public class AdminLibroRegisterFormController extends
    		AbstractWizardFormController {
    .....
    	
    
        protected void initBinder(HttpServletRequest request,
    			ServletRequestDataBinder binder) {
    
        binder.registerCustomEditor(Autor.class, new AutorSupport());
    
    	}
    
    .....
    
    }
    Support for Autor: AutorSupport
    Code:
    
    public class AutorSupport extends PropertyEditorSupport {
    
    	private ICatalogoService catalogoService;
    .....
    
        public void setAsText(String string)
              throws IllegalArgumentException {
     
        	try {
           
            	Long id = Long.valueOf(string);
            	Autor a = catalogoService.findAutorById(id);
            	
            	if(a==null){
            		throw new IllegalArgumentException("Id de Autor Inválido");
            	}
            	
            	setValue(a);
    
            } catch (NumberFormatException ex) {
              throw new IllegalArgumentException("Invalid id for Autor: " + string);
            } 
          }
    
    .....    
        }

    JSP Page

    Code:
    	
    
    ....
    <tr>
    		<th><label for="autor" class="required"> * <fmt:message
    			key="libro.autor" />:</label></th>
    		<td><spring:bind path="libro.autores">
    			<select name="autor" multiple>
    				<c:forEach var="autor" items="${autores}">
    					<option value="<c:out value="${autor.id}"/>"><c:out
    						value="${autor.nombres}  ${autor.apellidoPaterno}  ${autor.apellidoMaterno}" /></option>
    				</c:forEach>
    			</select>
    		</spring:bind></td>
    	</tr>
    ....

    Help me please!!!!!

  2. #2
    Join Date
    Nov 2005
    Location
    Valparaiso, Chile
    Posts
    12

    Default

    anyone have a idea? i need to fix this problem

  3. #3
    Join Date
    Nov 2005
    Location
    Valparaiso, Chile
    Posts
    12

    Default

    somebody help me please, i work for 5 days in this problem and i test whith all the implementations i've found in the forum, but nothing fix my problem :S

  4. #4
    Join Date
    Aug 2004
    Posts
    15

    Default

    I *think* your problem lies here in your controller custom editor registration:

    Code:
    binder.registerCustomEditor(Autor.class, new AutorSupport());
    When it should be:

    Code:
    binder.registerCustomEditor(Set.class, new AutorSupport());
    Since your property is not Autor but a Collection of Autor objects, Spring will only set a field of your command object as type Set and not find a match.

    Try that.

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

  6. #6
    Join Date
    Nov 2005
    Location
    Valparaiso, Chile
    Posts
    12

    Default

    Quote Originally Posted by jlee
    I *think* your problem lies here in your controller custom editor registration:

    Code:
    binder.registerCustomEditor(Autor.class, new AutorSupport());
    When it should be:

    Code:
    binder.registerCustomEditor(Set.class, new AutorSupport());
    Since your property is not Autor but a Collection of Autor objects, Spring will only set a field of your command object as type Set and not find a match.

    Try that.
    I change that, but the SetValue method gets a empty HashSet :s

  7. #7
    Join Date
    Nov 2005
    Location
    Valparaiso, Chile
    Posts
    12

    Default

    Quote Originally Posted by yatesco
    i've cheked that topic, but i couldn't find the taglib where is contains(status.value, role)

    Thanks for your replies

    Pd: sorry if my english isn't not the best... saludos desde Chile

  8. #8
    Join Date
    Aug 2004
    Posts
    15

    Default

    Quote Originally Posted by dcasanueva
    I change that, but the SetValue method gets a empty HashSet :s
    Well, that's because setValue gets a String[]. You'll have to then take each String, lookup you object (usually the String is a id of some sort) and then add that to a new Set. The fact that you're getting an entry into the setValue method means that your custom binder registration is working, so you're almost there!

    What i did was to keep a local instance of a HashSet in my class. I then got the String[] via the setValue method, which I then did my id lookups, added those objects to the HashSet, and then in the getValue method, I return that HashSet. If you see this post http://forum.springframework.org/showthread.php?t=19603 and scroll down towards the bottom, you'll see what I'm doing with Set.

  9. #9
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    I am not sure what spring does with Sets, but if you use an array then you do *not* need to register a property editor against the Array.class, merely against your domain class (i.e. Author).

    As mentioned in the linked article, this works with Author[], but I am not sure about Set<Author>.

  10. #10
    Join Date
    Nov 2005
    Location
    Valparaiso, Chile
    Posts
    12

    Default

    Quote Originally Posted by jlee
    Well, that's because setValue gets a String[]. You'll have to then take each String, lookup you object (usually the String is a id of some sort) and then add that to a new Set. The fact that you're getting an entry into the setValue method means that your custom binder registration is working, so you're almost there!

    What i did was to keep a local instance of a HashSet in my class. I then got the String[] via the setValue method, which I then did my id lookups, added those objects to the HashSet, and then in the getValue method, I return that HashSet. If you see this post http://forum.springframework.org/showthread.php?t=19603 and scroll down towards the bottom, you'll see what I'm doing with Set.

    Thanks! I understand the problem now ! I will fix it now jejeje

Posting Permissions

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