I am developing Spring 3 + Struts2 application and I configure my Actions in Spring as follows:
<bean id="patientSearchAPIClass" class="com.axiohelix.nozoki.web.action.api.PatientSearch">
<property name="searchService" ref="searchService"/>
</bean>
But in my Action class I keep fields to store Request parameters ,
public class PatientSearch extends ActionSupport {
public String getPharumoId() {
return pharumoId;
}
public void setPharumoId(String pharumoId) {
this.pharumoId = pharumoId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
private String pharumoId;
..
public String execute(){
searchResults=searchService.searchPatients(pharumoId, name,
birthday,
pharmacyId,
clinic,
doctorName,
drugName,
supplyDate,
offset,
pageSize);
return Action.SUCCESS;
}
This Action returns a JSON output and I access it using URL like:
http://localhost/app/searchAPI.action?name=UserName
then next time if I access using URL :
http://localhost/app/searchAPI.action
The field ‘name’ is till set to previous “UserName” value.
1.How to reset these values per request ?
2.I thought Action classes are instantiated per request,is it not ?
Problem was with the way Action classes are getting created by Spring.By default Spring creates singleton instances and for Struts2 ,Action classes also work as Model, due to this framework create a new instance of Action and place it in to values stack.
While creating action class using Spring make sure to define scope as prototype like
So that new instance of Action should get created by Spring for each request.