如何使用ActionContext中的参数,请求和会话对象? [英] How to use parameters, request and session objects present in ActionContext?

查看:107
本文介绍了如何使用ActionContext中的参数,请求和会话对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这段代码中,我使用ActionContext从Request对象获取Session和ServletActionContext。我觉得这是不好的做法,因为必须只对Request对象使用ActionContext。

Here in this code I am using ActionContext to get Session and ServletActionContext from Request object. I feel this is bad practice, as one must use ActionContext only for Request object.

ActionContext的Request对象是否等同于Servlets中的Request对象?如果是,如何使用它来获取请求参数?

Is ActionContext's Request object equivalent to the Request object in Servlets ? If yes, how to get request parameters using it ?

Map session = (Map) ActionContext.getContext().getSession();
HttpServletRequest request = ServletActionContext.getRequest();
String operatorId = request.getParameter("operatorId");
session.put("OperatorId", operatorId);
// getting hashmap from Bean
analysisNames= slsLoginDetailsRemote.getAnalysisNamesIdMap(); 
// sending map for multiselect
session.put("AnalysisNames",analysisNames); 


推荐答案

在Struts2中,会话映射和请求映射是包装器底层的HttpServletRequest和Session对象。

In Struts2, Session Map and Request Map are wrappers for the underlying HttpServletRequest and Session objects.

如果您只需要访问属性,请使用包装器。

If you only need to access attributes, use the wrappers.

使用ActionContext获取它们(两者都是包装器和底层HTTP对象)如果你在拦截器 POJO 内。

Use ActionContext to get them (both the wrappers and the underlying HTTP objects) only if you are inside an Interceptor or a POJO.

如果您在 Action 中,最佳做法是实现一个自动填充Action的接口对象:

If you are inside an Action, the best practice is to implement an Interface that will automatically populate your Action's object:

要获取 请求地图包装 ,< a href =http://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/interceptor/RequestAware.html =nofollow>使用 RequestAware

public class MyAction implements RequestAware {
    private Map<String,Object> request;

    public void setRequest(Map<String,Object> request){ 
        this.request = request;
    }
}

获取 会话地图包装 使用 SessionAware

public class MyAction implements SessionAware {
    private Map<String,Object> session;

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

获取基础 HttpServletRequest HttpSession 对象,使用 ServletRequestAware

To get the underlying HttpServletRequest and HttpSession objects, use ServletRequestAware:

public class MyAction implements ServletRequestAware {
    private javax.servlet.http.HttpServletRequest request;

    public void setServletRequest(javax.servlet.http.HttpServletRequest request){ 
        this.request = request;
    }

    public HttpSession getSession(){
        return request.getSession();
    }
}

也就是说,JSP页面之间的标准数据流和动作或动作和动作是通过Accessors / Mutators获得的,更好地称为Getters和Setters。不要重新发明轮子。

That said, the standard data flow between JSP pages and Actions, or Actions and Actions, is obtained through Accessors / Mutators, better known as Getters and Setters. Don't reinvent the wheel.

这篇关于如何使用ActionContext中的参数,请求和会话对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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