2

I have two methods in my Action class preprocess() and getThresholdData():

  • I set a List<String> variable in the preprocess() method (called when the page loads);

  • Then from the JSP page, a form is submitted and getThresholdData() is called.

JSP:

<body>    
    <s:form action="getThresholdDataConfigureTspThreshold">
        <s:select list="tspNames" label="Select TSP:" name="tspName"></s:select>
        <s:radio list="{'Default', 'Latest'}" label="Select Threshold type:"
                 name="thresholdType"></s:radio>
        <s:submit value="Submit"></s:submit>
    </s:form>       
</body>

tspNames (the list to iterate over) is set in the preprocess() method of the action class as soon as page loads, like follows:

<a href="/gma/preprocessConfigureTspThreshold" /> 

Action class:

public class ConfigureTspThresholdAction extends ActionSupport{
    private static final String DISPLAY = "display";
    private Map session;

    private List<String> tspNames;
    private List<String> thresholdParametres;

    private String tspName;
    private String thresholdType;

    public String preprocess() {
        // Get tspNames from server after Table creation
        tspNames = new ArrayList<String>();
        tspNames.add("RELIANCE");
        tspNames.add("AIRTEL");
        tspNames.add("TATA");
        tspNames.add("BSNL");
        tspNames.add("MTNL");
        session.put("tspNames", tspNames);
        return DISPLAY; 
    }

    public String getThresholdData(){
        // Get parametres from server after creating table
        thresholdParametres = new ArrayList<String>();
        thresholdParametres.add("1");
        thresholdParametres.add("2");
        thresholdParametres.add("3");
        thresholdParametres.add("4");
        thresholdParametres.add("5");
        System.out.println("************"    +           tspNames);
        return DISPLAY;
    }
    /** GETTER AND SETTERS*/
}

struts.xml:

<action name="*ConfigureTspThreshold"
       class="gma.struts.ConfigureTspThresholdAction" method="{1}">
    <result name="display">pages/ConfigureTspThresholdInput.jsp</result>
</action>

The flow is:

  1. JSP loads preprocess is called where list is set.
  2. User fills and submits a form, some work is done serverside and the user redirected to same JSP.

However the error comes as JSP is not able to be displayed as the list tspNames which was set in preprocess() method is coming null.

Here, when I try to print the list

System.out.println("************" + tspNames);

which I had set in the first function it's value is null.

Why is it so? Is the variable value lost after form is submitted? Is there to do something with session concept?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101
  • 1
    @SiddharthTrikha every variable has their scope.so when you call preprocess function and set value of list it is accesible for that function only. out of this scope ,its value is null. for example just assume you know "java" language very well but don't know any thing about ".net". so when some one ask you any question about java you know the answer.. but not same for .NET.. you don't know the answer because its out of your scope. so first you have to learn it and after that you can answer it. – Pratik Shah Nov 20 '13 at 09:41
  • 1
    @SiddharthTrikha For each action call a new instance of the action class is created and if you don't populate that instance with the data you get lost with rendering JSP without it. – Roman C Nov 20 '13 at 11:54

3 Answers3

2

You are trying to reinvent the wheel; what you need is to implement the Preparable Interface, to fully exploit the framework capabilities;

your initialization code will be placed in the prepare() method (it'ds like your custom preprocess() method, but managed by the framework, that is aware of it):

Action class that implements this interface must override the prepare method. The prepare method will always be called by the Struts 2 framework's prepare interceptor whenever any method is called for the Action class and also when validation fails before the view is rendered.

You don't need session either, but that's your choice.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Then put it in `execute()` method. P.S: Don't call Actions from JSP to execute code (as normal flow, there are cases where it can be handy but not your from what I can see) – Andrea Ligios Nov 20 '13 at 10:13
0

In the above scenario you have put some variable tspNames to the session on the first action call preprocess. Note, to work with the session your action class should implement SessionAware as one of the alternatives. Another approach is to set the scope of the variable to "session". You can do it with interceptor scope or scopedModelDriven, or if you'd like extend struts2 framework with "session" scope you should implement the scope strategy like described in this question.

The errors in your code:

  1. Not implemented SessionAware.
  2. If #1 is not true, then you only put the variable to the session, but didn't get it from session on the second action execution.

You should add the code:

public String getThresholdData() {
  tspNames = (List<String>) session.get("tspNames");
  ...
}    
Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

Try to use private static to define those variables if you want those variables shared between those action call in the same action class.

user1006080
  • 75
  • 1
  • 4
  • 11