PDA

View Full Version : Access to nested object properties on jsp


Taras
Aug 9th, 2007, 09:59 AM
Hi
I am new to Spring and I decide to develop simple test application using this framework.
Application has 2 business objects
ApplicationUsers
public class ApplicationUsers implements java.io.Serializable {
private int userId;
private String username;
private String password;
private Set<UserContacts> userContactses = new HashSet<UserContacts>(0);

//common getters and setters
}

UserContacts
public class UserContacts implements java.io.Serializable {
private int contactId;
private ApplicationUsers applicationUsers;
private String contactName;
private String contactDescription;

//common getters and setters
}

So I need to implemet user registration functionality

This is Controller
public class RegisterFormController extends SimpleFormController {

private IApplicationUsersDAO userManager;

public RegisterFormController() {
setCommandName("applicationUsers");
setCommandClass(ApplicationUsers.class);
}

protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,
BindException errors) throws Exception {

ApplicationUsers inputUser = (ApplicationUsers) command;
try {
userManager.createUser(inputUser);
} catch (Exception e) {
return showForm(request, response, errors);
}
return new ModelAndView(new RedirectView(getSuccessView()));
}

protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
ApplicationUsers user = new ApplicationUsers();
return user;
}
//common getters and setters for userManager

}

And I have creazted JSP form
it has next fields
<form:form commandName="applicationUsers" method="post" id="userForm" >

<form:input path="username" id="username"/>
<form:input path="password" id="password"/>
<form:input path="userContactses[0].contact_name" id="email"/>
<input type="submit" name="save" value="Save" />

</form:form>

So when I am trying to access to this page - i recieve an Exception
org.springframework.beans.InvalidPropertyException : Invalid property 'userContactses[0]' of bean class [com.romexsoft.spring.domain.model.ApplicationUsers]: Cannot get element with index 0 from Set of size 0, accessed using property path 'userContactses[0]'

Is it problem in data binding? Plese help to resolve this problem...

dr_pompeii
Aug 9th, 2007, 10:12 AM
first time that i see something like the bold part

private Set<UserContacts> userContactses = new HashSet<UserContacts>(0);


and

protected Object formBackingObject(HttpServletRequest arg0) throws Exception {
ApplicationUsers user = new ApplicationUsers();
return user;
}

no initialize the userContactses (it would be null)??


<form:input path="userContactses[0].contact_name" id="email"/>

check if your sintaxis is right, (the way of use the index)

HTH

regards

Jörg Heinicke
Aug 10th, 2007, 12:00 AM
no initialize the userContactses (it would be null)??

It IS initialized and not null! The Set is only empty - which is quite useless. On the first element that gets added to it, the internal store needs to be resized.

An since this set remains empty you can of course not add the first element in the set - and that's what Spring is complaining about. There is no userContactses[0]. Don't mix size of and index in a collection.

Joerg