维护行动变量的值? [英] Maintaining values of action variables?

查看:96
本文介绍了维护行动变量的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在我的问题是页面加载我调用预处理函数并填充 Map 的值。在页面提交之后调用另一个方法,在此之后,在一些数据库交互之后,它会重定向到JSP,但这次 Map 的值为空。我在Struts2中使用此 Map 作为下拉标记。

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"‌`​

所以只有在第一次点击链接时才调用 preprocess ,之后当我重定向到我的JSP所以它没有被调用,所以第二次地图的值为空。

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. 我要放会话中的地图,以便保留?或者可以做点什么?

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

我读过不要使用预处理函数,使用可预删除界面。但是根据文档:

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


Struts 2框架的准备拦截器将始终调用prepare方法

为Action类调用方法。

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.

推荐答案

<$ h $ => http://struts.apache.org/maven/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html prepare 方法每次执行动作时都会调用rel =nofollow> 可预留的 动作类,这是正确的。这可能是您为预处理方法中的下拉列表准备地图的原因。

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
}

JSP for drop-down

the JSP for drop-down

<s:select name="circleId" list="circleIdNameMap" listKey="key" listValue="value"/>

此代码的含义是:您不应该返回结果 SUCCESS INPUT 如果JSP中的字段未初始化。

The meaning of this code is: you should not return result SUCCESS or INPUT if fields in JSP aren't initialized.

这篇关于维护行动变量的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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