Accessing the action class variable in a second action

梦想的初衷 提交于 2020-02-24 14:55:31

问题


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?


回答1:


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.




回答2:


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");
  ...
}    



回答3:


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



来源:https://stackoverflow.com/questions/20089382/accessing-the-action-class-variable-in-a-second-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!