How do I use the parameters, request and session objects present in the ActionContext?

Here in this code, I am using ActionContext to get Session and ServletActionContext from Request object. I think this is bad practice, as you only need to use ActionContext on the Request object.

Is ActionContext request object equivalent to Request object in Servlets? If so, how do I get the query 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); 

      

+2


source to share


2 answers


In Struts2, the session map and request map are wrappers for the underlying HttpServletRequest and Session objects.

If you only need to access attributes, use wrappers.

Use ActionContext to get them (both wrappers and HTTP base objects) only if you are inside Interceptor

or POJO

.

If you're inside Action

, your best bet is to implement an interface that will automatically populate your Action object:


To get the shell the Request the Map , useRequestAware



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

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

      

To get a session card shell , useSessionAware

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

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

      

To get the basic facilities HttpServletRequest and the HttpSession , useServletRequestAware

:

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

      

Thus, the standard flow of data between JSP pages and actions, or actions and activities, is obtained through Accessors / Mutators, better known as Getters and Setters. Don't reinvent the wheel.

+7


source


The first

ActionContext Request object is equivalent to the Request object in Servlets

      

Second

If you are using a framework like struts. This is bad practice. You don't need to create HttpServletRequest objects from ServletActionContext to get request parameters. Just declare the request parameters in the action class and write that the recipients and setters for them will receive values โ€‹โ€‹in them.

UPDATE



If you need your request parameter in the 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

, all I would do is getOperatorId()

either use operatorId

directly. :)

If I find the implementation SessionAware

in the Action class smarter, since I can access session objects directly, like @Andrea. Now I can directly use session.put("OperatorId", operatorId);

andsession.put("AnalysisNames",analysisNames);

+1


source







All Articles