Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parameters have to be class variables of Action class ?

 
mark I thomas
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
mark I thomas
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic