Reputation: 2506
class SampleAction extends ActionSupport {
private Map<String,String> circleIdNameMap;
public String preprocess(){
--logic for populating value of MAP
}
--getters and setters
}
My problem is: when the page is loading, I call preprocess()
function and populate the value of Map
.
After the page is submitted, another method is called, and during that, after some DB interaction, it redirects to JSP, but this time the value of Map
is empty. I am using this Map
for dropdown tag in Struts 2.
My preprocess()
is associated in the link like:
href="/gma/preprocessConfigureTspThreshold?operatorId=5102&sessionId=12332"`
So, only first time when the link is clicked the preprocess()
is called, after that, when I redirect to my JSP, so it's not called then, the second time the value of Map
is empty.
Shall I put the map in a session, so that it will be retained? Or can do something else?
I read that I shouldn't use preprocess()
function, and use a Preparable
interface instrad. But as per docs:
The
prepare()
method will always be called by the Struts 2 framework'sprepare
interceptor
whenever any method is called for the
Action
class.
So, it will be called for every method. I want preprocess()
to be called only when page loads.
Upvotes: 2
Views: 809
Reputation: 1
The prepare
method of the Preparable
action class is called on every action execution, that's right. That might be the reason why you prepare the map for a drop-down in the preprocess
method.
public class SampleAction extends ActionSupport {
private Map<String,String> circleIdNameMap;
private String circleId;
//getters and setters here
protected boolean reload = false;
private void preprocess(){
// Get the Map by calling a stateless Session bean
circleIdNameMap = remoteInterface.getMap();
}
public String action1(){
preprocess();
Map session = ActionContext.getContext().getSession();
session.put("circleIdNameMap ", circleIdNameMap );
return SUCCESS;
}
public String action2(){
Map session = ActionContext.getContext().getSession();
circleIdNameMap = (Map<String,String>)session.get("circleIdNameMap");
if (circleIdNameMap == null){
if (reload) {
preprocess();
Map session = ActionContext.getContext().getSession();
session.put("circleIdNameMap ", circleIdNameMap );
} else {
addActionError("circleIdNameMap is null");
return ERROR;
}
}
return SUCCESS;
}
...//other actions
}
the JSP for drop-down
<s:select name="circleId" list="circleIdNameMap" listKey="key" listValue="value"/>
The meaning of this code is: you should not return result SUCCESS
or INPUT
if fields in JSP aren't initialized.
Upvotes: 1