aspose file tools*
The moose likes Struts and the fly likes Action class passing back parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action class passing back parameters" Watch "Action class passing back parameters" New topic
Author

Action class passing back parameters

Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
I know that I should be using my action classes to interface with my beans for the model component of my application. This is fine for lookups in the database (user validation, etc...) then I just do a
if(bean_returns_true)
{
mapping.findforward("validated")}
else{
mapping.findforward("not_validated")....
}

But what about when I want to return a value pulled out of the database to my JSP?

For example, I pull back links to news stories from my database. I then want the action class to pass back an array containing these links to NewsStory.jsp for display.

How can I do this? The documentation I have seen so far glosses right over this, despite it's obvious importance

Thanks.
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
Anyone?
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
in action:

request.setAttribute( "newsLinks", newsLinksList );

in jsp:

<logic:iterate name="newsLinks" id="newsLink">
<bean:write name="newsLink" />
</logic:iterate>
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
Ok, I have tried this now, but with no success.

This is in my action class' execute method.
(Right now I'm just using a static string for testing purposes)

HttpSession session = request.getSession();
session.setAttribute( "PdfUrl", "/pages/authorization.pdf" );


Here are the corresponding methods from my bean.
public String getPdfUrl(){
return pdfURL;
}
public void setPdfUrl(String pdfURL){
this.pdfURL = pdfURL;
}

And here is where I try to output to my JSP.
<c ut value="${selectForm.pdfUrl}"/><br/>

No value is being output to the JSP! What am I doing wrong?
Padma Lalwani
Ranch Hand

Joined: Nov 02, 2004
Posts: 49
Try
"${sessionScope.pdfUrl}" as you are setting attribute in session

Padma
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
I went ahead and tried that, but it had no affect. selectForm is the name of my bean though. That is where I should be retrieving the value from. The problem is that I don't seem to be getting the value into the bean correctly in the first place.
Sree Jag
Ranch Hand

Joined: Oct 14, 2003
Posts: 77
session.setAttribute( "PdfUrl", "/pages/authorization.pdf" );
change it to:
session.setAttribute( "pdfUrl", "/pages/authorization.pdf" );

(notice that the capital 'P' is changed to 'p')

and now since the attribute is in session scope, you need not refer to it with your form bean. So, try this
<c ut value="${pdfUrl}"/>

Lemme know if this works.

Hope this helps,
Seshu


Sree Jag<br />SCJP 1.4
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
Seshagiri,

That apparently works. I have some confusion here though. Some of the data I want to pull from a database and is more complex than a simple string. For that data I would like to be creating DTOs and then accessing the DTO from my JSP. I'm not sure how setting this data directly in the session scales to using DTO beans for complex data?
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Set the DTO to the session or request. If your page is a form, then set the data to the ActionForm.
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
Jason,

How do I set the DTO to the session or request?
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
The same way you set anything else to the session or request. If you look at the API you'll notice that the signature is:

setAttribute(java.lang.String name, java.lang.Object o)

...where name is the key used to store the object in and retrieve the object from session or request, and o is the object to store in session or request. The name attribute can be any arbitrary string. With that in mind you would do something like the following:

 
Don't get me started about those stupid light bulbs.
 
subject: Action class passing back parameters