Thanks , i didn't want to have the request parameters as javabeans within
the Action because they are used only for one-time checks and based on
their values, some other Action properties are set. The parameter names
could change later, and i didn't want that to affect the Action.
Now, i tried the following to retrieve the params from ActionContext (did
not implement ParametersAware or SessionAware)
Map requestParams = ActionContext.getContext().getParameters();
productCodes = new String[] {(String) requestParams.get("param1"), (String)
requestParams.get("param2")},
This gives the following exception :
java.lang.ClassCastException: [Ljava.lang.String; incompatible with
java.lang.String
can't figure what's going on here..
On Feb 10, 2008 2:10 PM, Dave Newton <[EMAIL PROTECTED]> wrote:
> --- j alex <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>