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

查看:26
本文介绍了如何使用 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 中,Session Map 和 Request Map 是底层 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.

仅当您位于 InterceptorPOJO 中时,才使用 ActionContext 获取它们(包装器和底层 HTTP 对象).

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:

要获取请求映射包装器使用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;
    }
}

要获取底层的 HttpServletRequestHttpSession 对象,使用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 页面和操作或操作和操作之间的标准数据流是通过访问器/修改器(更广为人知的 Getter 和 Setter)获得的.不要重新发明轮子.

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天全站免登陆