aspose file tools*
The moose likes Struts and the fly likes Getting Params from JSP to Action Class? (Struts 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Getting Params from JSP to Action Class? (Struts 2)" Watch "Getting Params from JSP to Action Class? (Struts 2)" New topic
Author

Getting Params from JSP to Action Class? (Struts 2)

Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
Hey, these forums have been great so far but I'm hoping to get some more help.

I have a jsp page that prints out the results from a DB as a link (navigation)


which generates a link like:
http://localhost:8080/StrutsBoard/disp3.action?contId=0 "General"
http://localhost:8080/StrutsBoard/disp3.action?contId=1 "Africa"
http://localhost:8080/StrutsBoard/disp3.action?contId=2 "Antarctica"
etc etc

disp3 is mapped in my struts.xml to go to this method in my action class:

No matter what I do, I cannot figure out what connection I need to make to get that contId from the link and pass it to the Threads.GetSome() method.

Once it passes the value, the end results work out fine (I've tested it be hardcoding)

here's the full pages if that'll help:
ThreadAction.java

Threads.java

Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
Hi Daniel try this


I think this should work.
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
That seems to do about the same, the navigation links display fine, but I'm still not getting the value in the Action Class.

It ends up getting null or 0 every time instead of what contId is passing.

Is there a getter/setter I need to setup on ThreadAction.java?
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
Yes you will need getter/setters for flist and what is contId? where are you declaring it?
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
contId is an int from the table forumDB, set to the int forumId (in the "public String setPostForum(int forumNum)" method)

And just in case anyone's curious, all of my System.out.println are just for debugging purposes.
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
Do 1 thing separtely in that iteration of your's try to print the value of contId using <s:property value="contId"/> just to check whether you are able to access the variable or not.
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
doing <s:property value="contId"/>

Yes, the contId does list it out properly from the DB, I even just changed one int in the table from 8 to 9 to be sure and it works fine.
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
so instead of %{contId} use #contId.
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
can you put the <s:property> tag in between <s:param> tag
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9

?

That doesn't work, it makes the link
http://localhost:8080/StrutsBoard/disp3.action?contId=#contId

where as before the contId was actually the correct int.
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
Try this
<s:param name="contId" value="#contId"/>

This should work
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9


works fine
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
cool. Is that all or you need more help?
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
Well that part was already working

But those values are not being read into the ThreadAction.java file

My links generate dynamically awesomely, but that int temp (ThreadAction.java) needs to somehow get the contId (jsp). That's where I'm having problems.
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
This is what you need to do
1. Make your Action implement the ParameterAware interface.

2. Define a class variable for parameters Map:
private Map parameters;

3. Create getter and setter methods for the above defined parameters Map.

4. Calling getParameters() gives you access to the request parameters through the above defined Map.

5. To make things simple, you can create a getParameterValue() method to return value of a request parameter by its name:

public String getParameterValue(String param) {
Object varr = getParameters().get(param);
if (varr == null) return null;
return ((String[]) varr)[0];
}

Regards
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
You need to extend extends ActionSupport for your Struts Action class.
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
Ok, just a tiny bit lost, so that would go like so:



so I would just make it getParameters().get(contId)?
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
yaah you can
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
but remember it will give you an object and not String when you do so.
Daniel Foley
Greenhorn

Joined: Jun 29, 2012
Posts: 9
I'm lost now... still not sure how that is looking for the param from the jsp

Also, the only value being passed via that param will be an int so can I change around the String in there to int?
Jinal Prakas Shah
Ranch Hand

Joined: Jun 20, 2012
Posts: 39
Behind the scene it is accessing the HttpServletRequest object and getting the parameters from the URL for you as a name value fashion in the Map class object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Params from JSP to Action Class? (Struts 2)