user962206
user962206

Reputation: 16117

Struts2 Populate form bean value from an Object located in the Session

I want to update a table in my database, but before that I will show the user his current information. now I have this form bean.

I have a form bean in my action class called.

public void setProfileBean(UserProfile profileBean) {
    this.profileBean = profileBean;
}

public UserProfile getProfile() {
    return profileBean;
}

And I want its value to be populated first from another bean inside the session scope. How would I achieve that using the model driven interface?

Upvotes: 1

Views: 3370

Answers (2)

arvin_codeHunk
arvin_codeHunk

Reputation: 2390

well first of all ,why you need in use of model-driven interface, in your case you can simply render the bean on your view using action-support.,you have to something like this:

1: first you should populate the view before you want to update, i mean ,at the time you are redirected to your page from which you are going to update your database,on that time you should populate the data on view.

2: You can achieve this by making some changes in your action:

in your action

public void setProfileBean(UserProfile profileBean) {
    this.profileBean = profileBean;
}

public UserProfile getProfile() {
    return profileBean;
}

**i assume this is your target method**

public string execute()
{
UserProfile user=new UserProfile();

user.setProfileBean("whatever you want to set");
......
......

return SUCCESS;

}

Thats it, doing this struts2 will automatically populate the data on your view, make sure you have defined properties like this:

<s:property value="UserProfile .xyz">

Upvotes: 1

CodeHunter
CodeHunter

Reputation: 368

As @roman suggested Struts2 ModelDriven interface works fine to solve such situations for some code help regarding how to use it you can refer to This link

Upvotes: 1

Related Questions