Announcement Announcement Module
Collapse
No announcement yet.
Newbie Spring MVC problem: Collections doesn't binding from form to model Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    anyone have a idea? i need to fix this problem

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          You might find http://forum.springframework.org/sho...ght=checkboxes useful.

          Comment


          • #6
            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

            Comment


            • #7
              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

              Comment


              • #8
                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.

                Comment


                • #9
                  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>.

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      thanx for the tip!

                      Comment

                      Working...
                      X