Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Struts Wiki" for change 
notification.

The following page has been changed by FrankZammetti:
http://wiki.apache.org/struts/OpenWindowFromAction

New page:
= How to open a new window from an Action =

Another frequent question on the mailing lists is how to open a new browser 
window from an Action.  This one won't take too long:

YOU CAN'T!

:)

Strictly-speaking, this is true... code running on the server has no way to 
tell the browser to open the response in a new window.  That being said, there 
'''ARE''' a couple of ways to get the desired effect.

 * Perhaps the easiest way is to set the target of the form you submit to 
"_new".  This will open a new window in which the results returned by the 
server will be shown.  Note that this will ALWAYS happen, there is no standard 
way to only use this target some of the time.  You can however use Javascript 
to variably set and clear the target attribute if you need to sometimes open a 
window and sometimes not, perhaps depending on whether a given checkbox is 
checked or not.

 * You can manually open a window using Javascript, and from there submit the 
form.  This will usually involve copying the values from the form in the parent 
document to a copy of it in the child window, and then programmatically 
submitting it.  This can be a good option if you are essentially spawning a 
navigational subbranch in your application and not simply showing results 
(i.e., a parallel task the user can perform vs. just showing a generated PDF).

 * A third option is to use AJAX.  You may not know it, but you can return 
Javascript from the server via an AJAX call and execute it.  What you can do is 
return something along these lines (this would literally be the HTTP response 
text):

{{{
   s = "";
   s += "<html><head><title>Results</title></head><body>";
   s += "These are my results";
   s += "</body></html>";
   w = window.open("", "Results", "resizable,height=200,width=200");
   w.document.open();
   w.document.write(s);
   w.documenmt.close();
}}}

Imagine generating this in an Action, where the string "These are my results" 
is replaced by a full HTML page (properly escaped of course).  Yes, you could 
generated the response with a JSP, which is what I would recommend.  That might 
be confusing, so let me explain... a JSP does '''NOTE''' have to render a 
complete HTML document.  It can instead render just a snippet of HTML, or just 
some Javascript, or just some comma-separated data, there is no limitation.  If 
you are doing AJAX, whatever is most appropriate can be done via JSPs (like XML 
too!).  This saved you from writing a lot of println's in your Actions to 
generate a response.

Now, if you make an AJAX call to this Action, and return this content and 
execute it, you will get your response opened in a new window.  Neat, huh?!?  
If you decide to go this route, I suggest checking out !AjaxTags in the Java 
Web Parts project (http://javawebparts.sourceforge.net).  It will allow you to 
put a single custom tag on your page and save you from having to write the AJAX 
code or the code to execute the returned results (although you will still have 
to write the code in the Action).

= Takeaway point =

The important point to take away from all this is that aside from the target 
attribute on the form tag, Javascript is involved.  There is currently no good 
way to avoid that (and someone can correct me if I missed something).  If you 
are working in an environment where scripting is a problem, you may want to 
revisit your design and see if you really need a new window at all.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to