struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <newton.d...@yahoo.com>
Subject Re: S2 Action : request params and session attributes
Date Sun, 10 Feb 2008 19:10:38 GMT
--- j alex <strutstwouser@gmail.com> 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.

Dave

[1] S2 Tutorials: http://struts.apache.org/2.x/docs/tutorials.html
[2] S2 Guides: http://struts.apache.org/2.x/docs/guides.html
[3]
ParameterAware:http://struts.apache.org/2.0.11/struts2-core/apidocs/org/apache/struts2/interceptor/ParameterAware.html
[4] SessionAware:
http://struts.apache.org/2.0.11/struts2-core/apidocs/org/apache/struts2/interceptor/SessionAware.html
[5] ActionContext: http://struts.apache.org/2.0.11/struts2-core/apidocs/com/opensymphony/xwork2/ActionContext.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message