如何将 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 中获取会话,并从 session
映射中获取 user
对象.
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 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!