Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Action class passing back parameters

 
Bloo Barton
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone?
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in action:

request.setAttribute( "newsLinks", newsLinksList );

in jsp:

<logic:iterate name="newsLinks" id="newsLink">
<bean:write name="newsLink" />
</logic:iterate>
 
Bloo Barton
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try
"${sessionScope.pdfUrl}" as you are setting attribute in session

Padma
 
Bloo Barton
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bloo Barton
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,

How do I set the DTO to the session or request?
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic