initialize a nested object inside command bean, Simpleformcontroller
I have a class Account
public class Account {
private int id;
private String name;
//get开发者_Python百科ters and setters
}
and a class Contact
private class Contact {
private int contactid;
private Account account;
//getters and setters
}
In a simple form controller, we initialize the command object through setCommandName method. Now my question is how should i initialize the account object that is related to this contact?
Actually, you initialize your command object in the formBackingObject() method, which typically involves actually calling new Contact()
or else using some type of factory object.
To initialize the nested objects, you pretty much have to do it by hand. Options include:
within your formBackingObject method:
Contact contact = new Contact();
contact.setAccount(new Account());
or, within the Contact object itself:
private Account account = new Account();
For more discussion, including a description of the rather more involved way that I ended up dealing with this, see my question Best Practice for Spring MVC form-backing object tree initialization
精彩评论