如何将Action类变量值传递给Struts 2中的另一个Action类 [英] How to pass Action class variable value into another Action class in Struts 2

查看:127
本文介绍了如何将Action类变量值传递给Struts 2中的另一个Action类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Action 类变量值传递到Struts 2中的另一个 Action 类?

How to pass Action class variable value into another Action class in Struts 2?

我想在另一个动作类的查询变量中使用它。

I wanted to use that retrieved in the query variable in another action class.

推荐答案

那里是动作可以彼此通信的不同方式,以及它们在不同线程中运行并且不共享动作上下文的方式。最流行的方法是使用请求或URL传递参数,XWork转换器将在OGNL的帮助下将它们转换为动作属性。

There are different ways the actions could communicate with each other as well as they running in different threads and don't share the action context. The most popular way is to pass parameters with the request or in the URL and XWork converter will convert them to the action properties with the help of OGNL.

但我认为 LoginAction 的目的是通过凭证验证用户 (电子邮件,用户名,密码)并将此信息保存在会话映射中。它是可以在操作之间共享的公共资源。要使会话映射可用于操作和其他操作,他们应该实现 SessionAware 。它将帮助Struts将会话映射注入到action属性中。如果您想在应用程序的许多操作中使用会话,那么在每个操作中都不能实现此接口,您可以创建基本操作。

But I think the purpose of the LoginAction is to authenticate the user by their credentials (email, username, password) and save this information in the session map. It is a common resource that could be shared between actions. To get the session map available to the action and other actions they should implement the SessionAware. It will help the Struts to inject the session map to the action property. If you want to use the session in many actions over the application then to not implement this interface in every action you could create a base action.

public class BaseAction extends ActionSupport implements SessionAware {

  private Map<String, Object> session;

  public setSession(Map<String, Object> session){
    this.session = session;
  }

  public Map<String, Object> getSession(){
    return session;
  }
}

然后行动将扩展基本行动以获得会话兼容性。

then actions will extend the base action to get the session compability.

public class LoginAction extends BaseAction {

  @Override
  public String execute() throws Exception {
    User user = getUserService().findBy(username, email, password);
    getSession().put("user", user);

    return SUCCESS;
  }

}

现在用户在会话中您可以从会话地图中的其他操作或JSP和用户对象获取会话。

Now the user in the session and you could get the session from other action or JSP and user object from the session map.

public class InboxAction extends BaseAction {

  @Override
  public String execute() throws Exception {
    User user = (User) getSession().get("user");

    return SUCCESS;
  }

} 

这篇关于如何将Action类变量值传递给Struts 2中的另一个Action类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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