struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <>
Subject Re: S2 Action : request params and session attributes
Date Sun, 10 Feb 2008 19:10:38 GMT
--- j alex <> wrote:
> HttpServletRequest request = ServletActionContext.getRequest();
> HttpSession session = request.getSession(true);
> if (null != request.getParameter("param1"))
> session.setAttribute("attr1" , true);
> else
> session.setAttribute("attr1" , false);
> This works, but we now have references to HttpServletRequest and
> HttpSession
> which goes against the S2 "POJO" action / testability benefits etc. (even
> though i may not write a TestCase at all)
> What's the best way to do this, so that the Action remains independent of
> http ?

There are several ways to do this, depending on your needs.

Request parameters are set on your actions if you have JavaBean-style
properties exposed via names the same as the parameter name. In the above
example you could have a String property named "param1" with its associated
setter, setParam1(). Then you don't have to do *anything*; it's just set
(assuming you're using the default interceptor stack).

This is pretty basic S2 functionality, so you might want to check out the
tutorials and guides on the S2 wiki [1,2].

You can also access the request or session parameter maps by implementing the
ParameterAware or SessionAware interfaces [3,4].

You can also access the request or session parameter maps via the
ActionContext class using the getParameters() and getSession() methods,
avoiding all references to Servlet Spec-specific classes [5].

The S2 wiki and the JavaDocs are your friends.


[1] S2 Tutorials:
[2] S2 Guides:
[4] SessionAware:
[5] ActionContext:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message