• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel try this


I think this should work.
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you will need getter/setters for flist and what is contId? where are you declaring it?
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so instead of %{contId} use #contId.
 
Jinal Prakas Shah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you put the <s:property> tag in between <s:param> tag
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

?

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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this
<s:param name="contId" value="#contId"/>

This should work
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


works fine
 
Jinal Prakas Shah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cool. Is that all or you need more help?
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to extend extends ActionSupport for your Struts Action class.
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaah you can
 
Jinal Prakas Shah
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but remember it will give you an object and not String when you do so.
 
Daniel Foley
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic