myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Myfaces Wiki] Update of "JavascriptOpenNewWindow" by Rene Grob
Date Sun, 25 Feb 2007 15:35:23 GMT
Dear Wiki user,

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

The following page has been changed by Rene Grob:
http://wiki.apache.org/myfaces/JavascriptOpenNewWindow

------------------------------------------------------------------------------
  With this solution we got control over the appearance of the new browser window. And since
there is no postback, there is no validation at all.
  This is the easiest way to open a new browser window when no model update is needed and
no action has to be executed.
  
+ == Advances techniques ==
+ In order to implement a proper action handling we need to move the decision wether to open
a new window to the action listener.
+ {{{
+ <h:commandLink actionListener="#{bean.openPopupClicked}" value="open popup" />
+ }}}
+ 
+ {{{
+ public void openPopupClicked(ActionEvent event) {
+     // code to open a new browser window goes here
+ }
+ }}}
+ === How to initiate a Javascript call from server side? ===
+ Well, unfortunately there's no standardized way to do so yet. 
+ However there are two ways to initiate a Javascript call from server side:
+  * Create a JSF page containing the script and navigate to that page. (Will not be described
in this tutorial.)
+  * Use the Tomahawk's Extestion''''''Filter to inject the Javascript in the rendered page.
(Needs an initial setup but is easier to maintain afterwards.)
+ Information about how to setup the Tomahawk's Extestion''''''Filter can be found here:
+ http://myfaces.apache.org/tomahawk/extensionsFilter.html
+ 
+ Now we can inject the Javascript using Tomahawk's Extestion''''''Filter Add''''''Resource
class:
+ {{{
+ public void openPopupClicked(ActionEvent event) {
+     // View's id in the same form as used in the navigation rules in faces-config.xml
+     // This value could be passed as parameter (using <f:param>)
+     final String viewId = "/popup.faces"; 
+     
+     FacesContext facesContext = FacesContext.getCurrentInstance();
+ 
+     // This is the proper way to get the view's url
+     ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
+     String actionUrl = viewHandler.getActionURL(facesContext, viewId);
+     
+     String javaScriptText = "window.open('"+actionUrl+"', 'popupWindow', 'dependent=yes,
menubar=no, toolbar=no');";
+     
+     // Add the Javascript to the rendered page's header for immediate execution
+     AddResource addResource = AddResourceFactory.getInstance(facesContext);
+     addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, javaScriptText);
        
+ }
+ }}}
+ 

Mime
View raw message