Announcement Announcement Module
Collapse
No announcement yet.
#SpringBind on dynamically created fields Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #SpringBind on dynamically created fields

    I'm working on a small Spring MVC application and I'm trying to create a dynamic form using Velocity, Spring and jQuery.

    I followed that article, of course with some changes :

    http://eggsylife.co.uk/2009/11/30/sp...ists-and-ajax/


    My Person class has a List of phones (it's a LazyList, with AutoPopulatingList it doesn't work)

    Code:
    ....
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "person", fetch = FetchType.EAGER, targetEntity = Phone.class)
    private List<Phone> phones;
    	
    	public Person() {
            phones = LazyList.decorate
                    (new ArrayList<Phone>(),
                     new InstantiateFactory(Phone.class));
            }
    ....
    My PersonFormController method

    Code:
    @RequestMapping(method = RequestMethod.GET)
    	public String showPersonForm(ModelMap modelMap) {
    		Person person = new Person();
    		modelMap.addAttribute(person);
    		return "personForm";
    	}
    and in my personForm.vm file in one form there are :

    1. Person adding #springBind

    HTML Code:
    <td>Name</td>
    					<td>
    						#springBind("person.name")
    						<input type="text" name="name" value="$!person.name">
    						<td width="60%">
             			   		<font color="red">$!{status.errorMessage}</font>
          				    </td>
    					</td>
    2.And Phone adding

    HTML Code:
    <table id="tablePhones">
    				<tr>
    					<td>Number</td>
    					<td>
    						#springBind("person.phones[0].number")
    						<input name="person.phones[0].number" size="40" />
    					</td>			
    					<td><a href="#" id="add">Add new phone</a></td>		
    				</tr>
    </table>
    I am able to produce new inputs for phone adding with AJAX and jQuery just like in an article posted above and it works fine.

    So now when I fill all fields in my PersonFormController there is a method

    Code:
    @RequestMapping(method = RequestMethod.POST)
    	public String addPerson(@ModelAttribute("person") Person person,
    			BindingResult result) {
    		personValidator.validate(person, result);
    		if (result.hasErrors()) {
    			return "personForm";
    		} else {
    			personDao.saveOrUpdate(person);
    			return "redirect:index.htm";
    		}
    	}
    Validation is fine and... only the Person is persisted and always one Phone with all null values. What do I need to change to fully persist all phones with current person? Could anyone help me with this? I am trying to figure it out for a long time..

    Maybe You need more information or my code?

    Thanks

    Dawid

  • #2
    Ok, I had to change the name attribute in my inputs to ${status.expression} and it works correctly now

    HTML Code:
    <table id="tablePhones">
    				<tr>
    					<td>Number</td>
    					<td>
    						#springBind("person.phones[0].number")
    						<input name="person.phones[0].number" size="40" />
    					</td>			
    					<td><a href="#" id="add">Add new phone</a></td>		
    				</tr>
    </table>

    Comment

    Working...
    X