aspose file tools*
The moose likes Struts and the fly likes variable in request in jsp, not in Action ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "variable in request in jsp, not in Action ?" Watch "variable in request in jsp, not in Action ?" New topic
Author

variable in request in jsp, not in Action ?

Niall McLoughlin
Greenhorn

Joined: Mar 11, 2002
Posts: 15
All

I've got a simple Struts app. My first.

I'm linking to the first .jsp of the struts app from a static HTML page on a separate server with :


This opens a new pop up window of a fixed size.

When mailin.jsp is displayed, I can access the appName variable in the request using :
<%= request.getParameter("appName") %>

Which is as I expected.

However, when I try to access the same variable in the Action mapped to the mailin.jsp with :


The variable is null. In debug, the request contains parameters for all of the ActionForm variables, but not appName.

mailing.jsp has an Action mapped:


I can think of shonky ways around this, but I'm obviously missing something Struts related in order to make this work cleanly. Any guidance would be appreciated.

Regards
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I can think of shonky ways around this, but I'm obviously missing something Struts related in order to make this work cleanly. Any guidance would be appreciated.

It's not actually a Struts issue you are encountering, rather the standard HTTP request-response cycle.

Your first link adds the parameter appName to the request. When the destination is rendered that request no longer exists. So while it was available to mailin.jsp when that jsp was being rendered (since at that time the request was still active), once it's finished, the request is gone along with any information it contained. When you submit mailin.jsp to an Action, a completely new request is generated, and this is why appName is null.

Let me add another comment which will help you out in the long run with Struts. You should never be linking directly to the JSPs that make up your application. All requests should go through the controller (the ActionServlet) and therefore all links to views in your application should be mapped to Actions, even if that Action is a simple ForwardAction.

So in your case, instead of linking directly to mailin.jsp it would have been better to link to an Action. That Action could have placed appName into Session in order to be retrieved later, or if you wanted to keep it in the request, it could have populated an appName field for the mailDetailsForm therefore making appName available in the request when that form was submitted.
Niall McLoughlin
Greenhorn

Joined: Mar 11, 2002
Posts: 15
Thank's for that Jason. I appreciate it.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The problem here is that Parameters last through only one request. When you displayed mailin.jsp, you are in the same request context where the parameter was passed so there you have access to it. When you submit mailin.jsp, thereby calling MailAction you have started a new request context, and the parameter associated with the previous request is gone. If you want to pass the paraemeter from the jsp on to the action it calls, I'd suggest using a hidden field as in:

<bean:parameter name="application" id="application"/>
<input type="hidden" name="application" value="<bean:write name="application" />" >

This way, the parameter is passed on to the action. If you add application as one of the properties on your form bean, struts will populate it just as it does the rest of your form controls.


Merrill
Consultant, Sima Solutions
Niall McLoughlin
Greenhorn

Joined: Mar 11, 2002
Posts: 15
I just wanted to test my thinking.

This is going to be a generic mail in database that will be linked from several parts of a website. Depending on which part of the site it was initiated from, I was intending to pass a request variable to determine which content the requester was asking for.

As Jason has pointed out above, I should be linking to an Action and setting a session attribute there. So, should I have a separate Action for each parameter I should be passing ?

ie:

becomes

and ColourAction sets a session attribute "appName" to "colour" before forwarding to the original mailin.jsp.

Then, to link from a second part of the site, which would have been

becomes :

and BWAction sets "appName" to "bw" etc.

Do I have to generate a superfluous ActionForm for each of these Actions ?

Thanks again.

[ March 20, 2005: Message edited by: Niall McLoughlin ]
[ March 20, 2005: Message edited by: Niall McLoughlin ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can still append a parameter to a call to an action as in mailin.do?appname=colour. You are free to make as many or few action classes and ActionForms as make sense for your application. If the various jsp's to be displayed by the mailin action are similar, you could easily make each of them use the same ActionForm. If they're substantially different, you could make them use different ActionForms.

The point here is that the Action class should get called first, and based on input received from the user, make the decision as to what jsp should be displayed next.
Niall McLoughlin
Greenhorn

Joined: Mar 11, 2002
Posts: 15
Thanks Merrill.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: variable in request in jsp, not in Action ?