File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Calling a JavaScript function after the completion of a Struts action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Calling a JavaScript function after the completion of a Struts action" Watch "Calling a JavaScript function after the completion of a Struts action" New topic
Author

Calling a JavaScript function after the completion of a Struts action

J Chung
Greenhorn

Joined: Jan 25, 2006
Posts: 9
Here's what I'm trying to do:

Window A displays a list of objects, and window B is the detail page for an object that the user selects. The user is able to edit the detail page and to save his changes. When the user clicks save, I want window B to close and window A to refresh.

The save button on window B submits the form to a save action. I tried including an onclick event on this button to call the following JavaScript...

opener.location.reload(true);
window.close();

...but it appears that doing so prevents the action from being called, so the detail page doesn't save.

I'd appreciate any ideas. Thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Since you need to perform some javaScript tasks when you submit the form for Window B, I'd suggest changing your <html:submit> to an <html:button>. Then include submitting the form as part of your onclick tasks (this.form.submit()).

I've had problems, though, with closing the window immediately after submitting a form. Sometimes the form doesn't get submitted before the window closes. There are two ways to handle this. One is to use the setTimeoutValue() javascript method to wait a second or two before submitting. The other is not to close the window at all in the submitting JSP, and to have the action forward to a page that does nothing but close the window in the onload method of the <body> tag.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
In my project, we use the second approach mentioned by Merrill ("have the action forward to a page that does nothing but close the window in the onload method of the <body> tag"). This also give you the ability to display validation error messages on the popup window.

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I just thought of something else on this problem: If the action for Window B puts something in the HttpSession, and you're expecting the document in Window A to show that change when it's refreshed, you probably want to use the second method of closing the window, and also refresh Window A from the forwarded-to JSP, rather than from the submitting JSP.

If you do it this way, you are assured that the action that puts something in the session has completed before Window A gets refreshed. Otherwise, you have no guarantees that Window A will show the new data when it gets refreshed.

so, you'd have something like this

<body on load="opener.location.reload(true);window.close()">
J Chung
Greenhorn

Joined: Jan 25, 2006
Posts: 9
Thanks for your replies. Calling the opener.location.reload from the Window B JSP (as part of an onlick event for the save button) appears to refresh Window A with the updated information as desired.

In regard to your comments related to the session, wouldn't making this JavaScript call from the forwarded-to JSP--i.e., the JSP that closes itself upon loading--refresh Window B instead of Window A?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a JavaScript function after the completion of a Struts action