Rachit Agrawal
Rachit Agrawal

Reputation: 735

Maintaining values of action variables in between different requests

I am struts2 for developing my application. Sample code of action class would be

class sampleAction extends Action {
private List<Employee> employee;
public validate(){
--logic for validation
}
public String prepopulate(){
--logic for populating value of employee list
}
--getters and setters
}

Now my problem is on page load i call prepopulate function and populate the value of employee list. After page submit validate method is called and during that if some error happens control redirects to jsp. but this time the value of employee list is empty. I am using this list for autocompleter tag in struts2.

Upvotes: 0

Views: 2036

Answers (1)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

I have never used Struts 2 built-in validation mechanism, as I prefer client-side validation to avoid an extra round trip. This is purely a personal choice and not a standard.

First I will suggest you not to use Action and use ActionSupport: ActionSupport provides a lot of functionality out of the box and you need not to do everything yourself.

I am assuming that you are using defaultStack and if this is the case than it provides out of the box Prepare Interceptor which takes care of preparing any values before the action itself is called.

In your case, validate is called before the execute method, so you never will get a chance to re-fill the values you need in your JSP.

All you need to make sure that you have prepare() method in your action class. Here are more details for this interceptor:

Prepare Interceptor
FAQ: How do we repopulate controls when validation fails

Upvotes: 4

Related Questions