7 Replies Latest reply on May 29, 2009 3:36 PM by avaldez314

    How to avoid page reload on a4j:htmlCommandLink ?

      Hello, do anyone know if there is some way to avoid a page reload when using <a4j:htmlCommandLink>?

      I've large page with a htmlCommandLink at the bottom of the page and when I click on the link the page is reloaded and the browser don't scroll down to the previous position.

      I thought that using the htmlCommandLink inside an a4j:form would do an ajax request, avoiding the page reload.

      Any clue will be appreciated.



      Here is an example of how I'm using it:

      <a4j:form id="form">
       <rich:separator height="1000"/>
       <a4j:htmlCommandLink id="startDownload" actionListener="#{testBean.downloadFile}">
       <f:param name="fileName" value="filename.ext" />
       <f:param name="errorMessageId" value="form:startDownload" />
       <h:outputText value="filename.ext" />
       </a4j:htmlCommandLink>
       <rich:message for="form:startDownload" errorClass="errorMessage"/>
      </a4j:form>
      
      


        • 1. Re: How to avoid page reload on a4j:htmlCommandLink ?
          nbelaevski

          Hi,

          Use a4j:commandLink.

          • 2. Re: How to avoid page reload on a4j:htmlCommandLink ?

             

            "nbelaevski" wrote:
            Hi,

            Use a4j:commandLink.


            Hello, using a4j:commandLink was my very first choice but this link starts a file download and I could not make it work with an a4j:commandLink.

            To start the file download I'm using the method described at:

            http://ahoehma.wordpress.com/2008/03/31/jsf-file-download/


            If the a4j:htmlCommandLink can't be used without a page reload, do anyone know other way to start a file download from a managed bean (not a servlet) without doing a page reload? (I can't use a simple html link to the file because it's retrieved from a database).




            • 3. Re: How to avoid page reload on a4j:htmlCommandLink ?
              nbelaevski

              It shouldn't replace page contents. What is file type?

              • 4. Re: How to avoid page reload on a4j:htmlCommandLink ?

                 

                "nbelaevski" wrote:
                It shouldn't replace page contents. What is file type?


                It happens with all extensions I have tried (i.e. pdf, xls).

                I just tried again, I replaced the a4h:htmlCommandLink for a4j:commandLink, then an AJAX request is made to call the actionListener, the file is retrieved and sent to the browser but the file content is rendered (raw) in the browser screen.

                I checked that the contentType attribute in the HttpServletResponse object is set to the correct type, but anyway when using a4j:commandLink the file content is sent directly to the browser screen.

                This is a snippet of how I'm setting the HttpServletResponse response object:

                HttpServletResponse response = (HttpServletResponse) context
                 .getExternalContext().getResponse();
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setContentType(contentType);
                response.setHeader("Content-Disposition", "attachment; filename=\""+ filename + "\"");
                
                // feed the file into the response outputStream in response.getOutputStream()
                // ...
                context.responseComplete();
                



                Any suggestion?


                • 5. Re: How to avoid page reload on a4j:htmlCommandLink ?
                  nbelaevski

                  Sorry, I was not precise, a4j:commandLink doesn't allow files downloading. Use a4j:htmlCommandLink for that. Try not setting content-type at all.

                  • 6. Re: How to avoid page reload on a4j:htmlCommandLink ?

                     

                    "nbelaevski" wrote:
                    Sorry, I was not precise, a4j:commandLink doesn't allow files downloading. Use a4j:htmlCommandLink for that. Try not setting content-type at all.


                    Using a4j:htmlCommandLink the download works correctly but the page is reloaded and the browser shows the top of the page, not preserving the scroll position where it was before start the download.

                    This is the reason why I was asking for a way to avoid the page reload in the a4j:htmlCommandLink.

                    If avoiding the page reload isn't possible, do you know any workaround to this situation?



                    • 7. Re: How to avoid page reload on a4j:htmlCommandLink ?

                      I'm sorry, I forgot something important:

                      When doing the download with the a4j:htmlCommandLink the download starts correctly and the page is not reloaded.

                      But in case that of a database failure a message should be displayed to the user to inform about the error, I'm showing that error with a <rich:message> inside the <a4j:form>.

                      When there is a message to show, then the page is reloaded and the scroll position is not preserved. I though that <rich:message> would be updated in an AJAX request without a page reload.

                      Please, take a look at my first post, there I show how I'm trying to do it.

                      I appreciate a lot your help, really :-)