You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Saurabh Bobde <sb...@cisco.com> on 2006/03/10 10:27:48 UTC

Struts Action returning null - Design query?

Hello.

I tried my best before posting this and searched through the archives, but
couldn't find anything on this.

I recently came across a team which was doing something which seemed a
little "weird" more from a designer's (purist?) perspective. 

After processing a request, the Action would simply return a null
ActionForward,  like so: return (null);

One of the scenarios why/where they were doing this was, they needed to
write to the response directly. (For eg, returning the contents of a file
dynamically)
Also, the behavior they are expecting is, that by simply returning a null,
the page stays on the browser/client, and the returned response, may be
opened in another window, depending on the content-type returned.

Questions that I would like to ask are:
A. Is this the right way to be using Struts? In my opinion, this kind of
procssing should really be handled using simple servlets (along with other
helper classes)  rather than Struts-Action-type classes.
B. Is Struts intended to be used as above, by returning a null action
forward?
	By design, when a method has a return type R, that becomes part of
the contract, and the caller may typically assume that to be the returned
type. Yes, checking for nulls would be a good thing - and necessary. But, it
becomes the responsibility of the implementor to adhere to that contract.
	However, from the point of view of someone only looking at the
Action which returns a null, things will be very fuzzy unless they know how
Struts is likely to handle it: some folks may assume, it could result in an
exception while others may think it is perfectly ok.
	What is a better way to achieve the same thing?

Any thoughts and discussion much appreciated.
 
Rgds,

Saurabh Bobde 
~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

"DISCLAIMER: The information contained in this e-mail message may be
privileged, confidential, and protected from disclosure. If you are not the
intended recipient, any further disclosure or use, dissemination,
distribution, or copying of this message or any attachment is strictly
prohibited. Please ensure you have adequate virus protection before you open
or detach any documents from this transmission. Zensar Technologies Inc.
does not accept any liability for virus. If you think that you have received
this e-mail message in error, please delete it and notify the sender." 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts Action returning null - Design query?

Posted by Antonio Petrelli <br...@tariffenet.it>.
Saurabh Bobde ha scritto:
> After processing a request, the Action would simply return a null
> ActionForward,  like so: return (null);
>
> One of the scenarios why/where they were doing this was, they needed to
> write to the response directly.

I've seen this in Struts-Layout, where they send the elements of a 
dynamic tree to the caller by writing the response directly.

> (For eg, returning the contents of a file
> dynamically)
>   

For this particular case, I suggest to use DownloadAction:
http://struts.apache.org/struts-extras/apidocs/org/apache/struts/actions/DownloadAction.html

> Also, the behavior they are expecting is, that by simply returning a null,
> the page stays on the browser/client, and the returned response, may be
> opened in another window, depending on the content-type returned.
>   
Uh? Isn't it better to open the window with the URL connected to your 
action? I mean:
window.open('action.do');

Ciao
Antonio

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Struts Action returning null - Design query?

Posted by Saurabh Bobde <sb...@cisco.com>.
Thanks a lot,  for all the responses.

That helps!

Saurabh Bobde 
~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

"DISCLAIMER: The information contained in this e-mail message may be
privileged, confidential, and protected from disclosure. If you are not the
intended recipient, any further disclosure or use, dissemination,
distribution, or copying of this message or any attachment is strictly
prohibited. Please ensure you have adequate virus protection before you open
or detach any documents from this transmission. Zensar Technologies Inc.
does not accept any liability for virus. If you think that you have received
this e-mail message in error, please delete it and notify the sender." 


-----Original Message-----
From: Wendy Smoak [mailto:wsmoak@gmail.com] 
Sent: Friday, March 10, 2006 6:35 PM
To: Struts Users Mailing List
Subject: Re: Struts Action returning null - Design query?

On 3/10/06, Saurabh Bobde <sb...@cisco.com> wrote:

> After processing a request, the Action would simply return a null 
> ActionForward,  like so: return (null);
>
> One of the scenarios why/where they were doing this was, they needed 
> to write to the response directly. (For eg, returning the contents of 
> a file
> dynamically)

Okay.

> Also, the behavior they are expecting is, that by simply returning a 
> null, the page stays on the browser/client, and the returned response, 
> may be opened in another window, depending on the content-type returned.

(I don't think you can open new windows from the server side.  But they
could have opened the new window from the client side and and made the
request from there, so I'll ignore this point.)

> Questions that I would like to ask are:
> A. Is this the right way to be using Struts? In my opinion, this kind 
> of procssing should really be handled using simple servlets (along 
> with other helper classes)  rather than Struts-Action-type classes.

A matter of opinion.   If everything is done with Struts, it's just
natural to write another Action to do whatever it is you want to do. 
And you may want to take advantage of some framework features that wouldn't
be available in a servlet.  I do use simple servlets for things that have
nothing to do with Struts, (converting some text to PDF based on a session
attribute,) but I wouldn't call it wrong to do otherwise.

> B. Is Struts intended to be used as above, by returning a null action 
> forward?

Yes, in fact it's mentioned in the Javadocs for Action.execute(...). 
The framework "knows" that a null return value means that the response has
been dealt with, and it shouldn't do anything further.
 * http://tinylink.com/?PHJLxqCMze

HTH,
--
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Struts Action returning null - Design query?

Posted by Wendy Smoak <ws...@gmail.com>.
On 3/10/06, Saurabh Bobde <sb...@cisco.com> wrote:

> After processing a request, the Action would simply return a null
> ActionForward,  like so: return (null);
>
> One of the scenarios why/where they were doing this was, they needed to
> write to the response directly. (For eg, returning the contents of a file
> dynamically)

Okay.

> Also, the behavior they are expecting is, that by simply returning a null,
> the page stays on the browser/client, and the returned response, may be
> opened in another window, depending on the content-type returned.

(I don't think you can open new windows from the server side.  But
they could have opened the new window from the client side and and
made the request from there, so I'll ignore this point.)

> Questions that I would like to ask are:
> A. Is this the right way to be using Struts? In my opinion, this kind of
> procssing should really be handled using simple servlets (along with other
> helper classes)  rather than Struts-Action-type classes.

A matter of opinion.   If everything is done with Struts, it's just
natural to write another Action to do whatever it is you want to do. 
And you may want to take advantage of some framework features that
wouldn't be available in a servlet.  I do use simple servlets for
things that have nothing to do with Struts, (converting some text to
PDF based on a session attribute,) but I wouldn't call it wrong to do
otherwise.

> B. Is Struts intended to be used as above, by returning a null action
> forward?

Yes, in fact it's mentioned in the Javadocs for Action.execute(...). 
The framework "knows" that a null return value means that the response
has been dealt with, and it shouldn't do anything further.
 * http://tinylink.com/?PHJLxqCMze

HTH,
--
Wendy

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org