How to use parameters, request and session objects present in ActionContext?

大城市里の小女人 提交于 2019-11-26 15:31:44
Andrea Ligios

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.

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

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


To get the Request Map wrapper, use RequestAware

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

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

To get the Session Map wrapper, use SessionAware

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

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

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();
    }
}

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.

First

ActionContext's Request object is equivalent to the Request object in Servlets

Second

If you are using framework like struts. It is a bad practice. You need not create HttpServletRequest objects from ServletActionContext for getting request parameters. Just declare request parameters in action class and write getters and setters for them will get you values in them.

UPDATE

If you wanted your request parameter in action class you can do it like this:

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


    //Getter and setters
    public String getOperatorId() {
            return operatorId;
        }

        public void setOperatorId(String operatorId) {
            this.operatorId = operatorId;
        }

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

    }
    }

So now if I wanted to use operatorId anywhere all I will do is getOperatorId() or use operatorId directly. :)

If find implementing SessionAware in Action class more reasonable as I can directly access session objects like @Andrea has mentioned.. So now I can directly use session.put("OperatorId", operatorId); and session.put("AnalysisNames",analysisNames);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!