在第二个动作中访问动作类变量 [英] Accessing the action class variable in a second action

查看:42
本文介绍了在第二个动作中访问动作类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Action类preprocess()getThresholdData()中有两个方法:

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

  • 我在preprocess()方法中设置了List<String>变量(在页面加载时调用);

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

然后从JSP页面提交表单并调用getThresholdData().

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>

页面加载后,立即在操作类的preprocess()方法中设置

tspNames(要迭代的列表),如下所示:

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" /> 

动作类:

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>

流为:

  1. JSP加载preprocess在设置列表的地方被调用.
  2. 用户填写并提交表单,一些工作在服务器端完成,并且用户重定向到同一JSP.
  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.

但是,由于在preprocess()方法中设置的列表tspNames将要显示null,因此无法显示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);

我在第一个函数中设置的

值是null.

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?

推荐答案

您正在尝试重新发明轮子;您需要实施 Preparable Interface ,充分利用框架功能;

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

您的初始化代码将放置在prepare()方法中(它与您的自定义preprocess()方法类似,但是由框架进行管理,对此有所了解):

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):

实现此接口的操作类必须重写prepare 方法. prepare方法将始终由Struts 2调用 每当为框架调用任何方法时,框架的准备拦截器 动作类,以及在视图之前验证失败的情况下 呈现.

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.

这篇关于在第二个动作中访问动作类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆