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

18,106

Solution 1

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.

Solution 2

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

Share:
18,106
Siddharth Trikha
Author by

Siddharth Trikha

Updated on June 04, 2022

Comments

  • Siddharth Trikha
    Siddharth Trikha almost 2 years

    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.

    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); 
    
  • Siddharth Trikha
    Siddharth Trikha over 10 years
    If i implement the RequestAware interface like shown in the above answer, i can get the request parametres by doung request.get("paramName") ???
  • DarkHorse
    DarkHorse over 10 years
    Yes you can.. but you can avoid that implementation if you are writing your logic in Action class. You can avoid RequestAware interface for Action class... See my update for example..