8 Replies Latest reply on Aug 23, 2008 9:15 AM by Terry

    a4j:commandLink opening in a new window

    Terry Apprentice

      Is it possible to redirect request submitted with a4j:commandLink to a new window?

      I have a xhtml search page with a4j:form on it; when user types in some data and click Submit (a4j:commandLink), the form should submit to update backing bean and open results in a new window.

      is it possible with aj4/richfaces?

      I tried using a4j:commndLink's target="myWindowName" tag and onlick="window.open('','myWindowName')". It does submit a form, and open new window. but new window is blank and results get rendered in the same window.

      RichFaces 3.2.1.GA
      Facelets
      JBoss Seam 2.0.1GA

      t="return validateForm('unpaidApplication');" reRender="unpaidApplicationSearchResults, globalMessageRerenderer" rendered="#{!unpaidApplication.printable}">
      
       ...
      
       <a4j:commandLink id="printView" value="Print Preview" action="#{unpaidApplication.printViewAction('/report/unpaid-application.xhtml')}"
       target="myWindowName" styleClass="button float-left" onclick="window.open('','myWindowName');"/>
       </a4j:commandLink>
      
      </a4j:form>
      
      The unpaidApplication.printViewAction loads data to display and retruns the url passed to it.
      
      There is no page.xml navigation for action unpaidApplication.printViewAction.
      
      
      






        • 1. Re: a4j:commandLink opening in a new window
          shadowcreeper Apprentice

          I think you need to put the URL in the first arg to window.open() and make printViewAction() stop returning a string and put the window.open() in the oncomplete instead of onclick.

          • 2. Re: a4j:commandLink opening in a new window
            shadowcreeper Apprentice

            If the action returns a string, the page that it was in will reload according to the rules you setup.

            Also, I'm not sure when onclick happens in relation to action or actionListener, but the timing of oncomplete is fairly obvious.

            • 3. Re: a4j:commandLink opening in a new window
              Terry Apprentice

              yes tried all those, but can't seem to get the results Im after.

              putting URL in window.open does open it new window; but it does not fetch data returned by form submitted by a4j:commandLink.

              I think a4j:commandLink's target attribute is supposed to do this. but it appears it just gets ignored.

              I am not sure if there is BUG in a4j:commandLink or Im doing it wrong.

              It would be really nice if some AJAX DEVELOPER can reply on this.



              • 4. Re: a4j:commandLink opening in a new window
                Rob Hills Newbie

                I am working with Terry on this and we've tried the suggestions you made, as well as some variations. None produced a useful outcome.

                We've changed our unpaidApplication.printViewAction to return null.

                Added the URL to window.open() and moved the window.open() to the oncomplete attribute:

                t="return validateForm('unpaidApplication');" reRender="unpaidApplicationSearchResults, globalMessageRerenderer" rendered="#{!unpaidApplication.printable}">
                
                 ...
                
                 <a4j:commandLink id="printView" value="Print Preview" action="#{unpaidApplication.printViewAction}"
                 target="myWindowName" styleClass="button float-left" oncomplete="window.open('/report/unpaid-application.seam','myWindowName');"/>
                 </a4j:commandLink>
                 ...
                </a4j:form>
                


                This causes a popup window to appear, however, it doesn't render any results, it simply displays the search fields and buttons. Furthermore, any changes to search parameters made in the original (parent) window are lost (ie replaced with default values). It looks exactly the same as the original search window when it's freshly opened.

                Then we tried adding a reRender attribute to the a4j:commandLink - this causes the search results to be rendered in the parent window and the popup window remains as before (ie looks like a freshly opened search page).

                The "target" attribute on the a4j:commandLink appears to do nothing. On a h:commandLink, the value of the "target" attribute is incorporated into the javascript generated for the onclick method. AFAICT, this does not happen for the a4j:commandLink, so I'm not sure what this attribute actually does on a4j:commandLink.

                Using the h:commandLink gets us closer to a working solution, though it also has problems.

                Cheers,
                Rob Hills

                • 5. Re: a4j:commandLink opening in a new window
                  shadowcreeper Apprentice

                   


                  This causes a popup window to appear, however, it doesn't render any results, it simply displays the search fields and buttons. Furthermore, any changes to search parameters made in the original (parent) window are lost (ie replaced with default values). It looks exactly the same as the original search window when it's freshly opened.

                  Is your bean "request" scoped or "session"? Beans should be "session" scoped when their settings need to be remembered across multiple pages (which, if I am understanding correctly, is what you are requesting).

                  I don't think that a t:saveState will work across multiple pages (I could be wrong on this).


                  The "target" attribute on the a4j:commandLink appears to do nothing. On a h:commandLink, the value of the "target" attribute is incorporated into the javascript generated for the onclick method. AFAICT, this does not happen for the a4j:commandLink, so I'm not sure what this attribute actually does on a4j:commandLink.

                  I believe the target attribute is only helpful if you are opening in a frame on the same page, not into a whole new window or tab (I could be wrong on this one also).


                  • 6. Re: a4j:commandLink opening in a new window
                    Terry Apprentice

                    bean is default conversation scoped, which I believe is correct. Since Seam maintains instance until page is rendered.

                    problem is really to get a4j to send received contents to the new window, and be able to access that window for setting focus to it when it was already opened.

                    Usually this is done with javascript like:

                    var myWinHandle = window.open('','MyWindowName', 'location=no,resizable=yes,scrollbars=yes,toolbar=yes,status=no,width=500,height=500,top=50,left=50')
                    


                    then the myWinHandle can be used to focus on that window, myWinHandle.focus(); when that window was already opened and refreshed with new data.

                    The target attribute is a mystery so far.
                    Not sure why it is so hard incorporate it with a4j:commandLink.



                    • 7. Re: a4j:commandLink opening in a new window
                      shadowcreeper Apprentice

                      Sorry, I am not familiar with Seam.

                      But if your page comes up with defaults that is usually (when not dealing with Seam at least) because your bean's scope is too restrictive causing JBoss to create a new instance of the bean for the page (thus pulling in the defaults).

                      Per the docs for a4j:commandLink target attribute:

                      This attribute specifies the name of a frame where a document is to be opened. By assigning a name to a frame via the name attribute, authors can refer to it as the "target" of links defined by other elements


                      I read this to mean that if you are trying to send your response to another frame (or iframe perhaps) on the same page, you can use the target attribute. What you are attempting is something completely different, to send the response to a completely different window.

                      • 8. Re: a4j:commandLink opening in a new window
                        Terry Apprentice

                        Thanks for the clues, and sorry we actually confused the issue by providing some of the information not directly related to the issue on hand.

                        the seam bit and getting results back from backing backing bean is all good. the issue is to get a4j redirect the response to new window.

                        Im starting to think there is limitation in a4j in this regard; or there is some hidden trick to it which we can't find without some input from A4J developers; but not show how to get their attention here.



                        the h:commandLink target tag opens a new window; so I