Initialize a nested object inside command bean, Simpleformcontroller?

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.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I have a class Account public class Account { private int id; private String name; //getters 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? Java spring-mvc link|improve this question edited Oct 26 '10 at 19:14skaffman114k8136227 asked Oct 26 '10 at 18:38mahati483 67% accept rate.

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 stackoverflow.com/questions/378066/best-....

Thanks for the quick reply! – mahati Oct 26 '10 at 18:49 Instead of initializing it using the 'new', cant we do it through bean injection? What I am trying to say is, I have a contactController bean with the following definition – mahati Oct 26 '10 at 18:51 contact com.advanstar.datatrax.model.

Contact which initializes the command class, now can't I say – mahati Oct 26 '10 at 18:52 to inject the account object into the contact object? – mahati Oct 26 '10 at 18:53 Specifying the commandClass property doesn't refer to configured beans, it just tells the controller what class the command object is (behind the scenes it just calls BeanUtils. InstantiateClass() on that class).

And you wouldn't want to simply inject a new Contact object anyway; a lot of the time you want to initialize the command bean yourself based on data in the request or in the session (e.g. Someone wants to edit a Contact that already exists). That's what formBackingObject() is for. – JacobM Oct 26 '10 at 19:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions