File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes parameters have to be class variables of Action class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "parameters have to be class variables of Action class ?" Watch "parameters have to be class variables of Action class ?" New topic
Author

parameters have to be class variables of Action class ?

mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
I created a struts 2 action class and proper mapping in struts.xml.

1) In my Action class, I have "name", "state", etc class variables.

execute() {
HttpServletRequest request = ServletActionContext.getRequest();
request.getAttribute("name");
....
}
2) action /myAction is mapped to this action class in struts.xml and it has

the method 'test()" is a json type

<result type="json">

Now,

I noticed when I access URL like

/myAction?name=john&state=ca

it works fine.

But if I try

/myAction?county=clouders

it fails in

request.getAttribute("county");

because it shows it can't find anything for "county" attribute ALTHOUGH I supplied it in the access URL.

If I am accessing a regular HttpServlet (instead of Astruts 2) then in servlet's "get" method I was able to get it. But in action class, why can my
HttpServletRequest only retrieve variables that are sdefined in the class ? Please help explain. Thanks.




Orton K Randy
Ranch Hand

Joined: Aug 12, 2011
Posts: 41

Firstly, was an attribute named 'county' set somewhere that you're trying to get through getAttribute method?

The part of the URL:


signifies county is a request parameter and NOT a request attribute. In Struts2, since you don't have an ActionForm, parameters and their getters/setters are coded in the Action class. I'd sugest understanding the differences between attributes and parametes in various scopes. These links could be helpful in understanding them:
Link1 (remove %20) in the URL.
Link2

Help us help you by providing us with actual error messages.


Coderanch, best ever forum on the net. Hands down.
mark I thomas
Ranch Hand

Joined: Apr 07, 2008
Posts: 86
yes "county" is passed as parameter. In this case, should the action class extends "ParamterAware" stuff ?
what's the syntax to retrieve this parameter value in action's method ?
Orton K Randy
Ranch Hand

Joined: Aug 12, 2011
Posts: 41

You don't have to. I believe you had gone through the methods(well there's only one I believe) of ParameterAware. I'd recommend implementing ServletRequestAware instead which would give you the access to request object. You guessed it, there's ServletResponseAware and SessionAware interfaces too. Thing is the request(and response/session) object(s) would be injected through respective setters. This example should give you more insight into using these objects. Hope that helps.
 
Consider Paul's rocket mass heater.
 
subject: parameters have to be class variables of Action class ?
 
Similar Threads
prepare()
Interceptors in Struts 2
open a pdf inline using MIME in ie7
Struts2-json plugin not serializing action class to json
struts2 + json plugin problems