3
class SampleAction extends ActionSupport {
private Map<String,String> circleIdNameMap;

public String preprocess(){
--logic for populating value of MAP
}
--getters and setters
}

Now my problem is on page load I call preprocess function and populate the value of Map. After page submit 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 drop-down tag in Struts2.

My preprocess is associated in the link like:

href="/gma/preprocessConfigureTspThreshold?operatorId=5102&sessionId=12332"‌`​

So only first time when the link is clicked preprocess is called, after that when I redirect to my JSP so its not called then, so second time the value of Map is empty.

  1. Shall I put the map in a session so that it is retained? Or can do something else?

  2. I read that don't use preprocess function, use Preparable interface. But as per docs:

    The prepare method will always be called by the Struts 2 framework's prepare 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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101
  • How are you calling method `on page load`? If it is get called on page load then how map is empty? You can simply call `preprocess()` just before `return ERROR;` then on JSP map will not be empty. – Aniket Kulkarni Jan 06 '14 at 07:27
  • prepare method will get called when there is a request to you action..so be it page load or anything use case – Umesh Awasthi Jan 06 '14 at 08:12
  • @Aniket: My preprocess is associated in the link like: href="/gma/preprocessConfigureTspThreshold?operatorId=5102&sessionId=12332"! So only first time when the link is clicked preprocess is called, after that when i redirect to my JSP so it;s not called – Siddharth Trikha Jan 06 '14 at 08:14
  • "After page submit" means there's another request. Actions are instantiated per-request; any data in a previous action will obviously not be there in subsequent requests unless it's filled somehow. – Dave Newton Jan 06 '14 at 12:54

1 Answers1

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176