5 Replies Latest reply on Mar 13, 2008 8:39 PM by Shimon Lifshitz

    Code that works in JSF is not working with Richfaces - pleas

    E Aish Novice

      Hi,

      I have the following code that works fine in JSF. I replaced the <h:commandLink> with <a4j:commandLink> and the code does not work as expected. Can you please guide me why the richfaces code is not working.

      Here are the related codes:
      =================

      1. index.jsp - in the format of jsf that works fine
      ----------------------------------------------------------

      <h:commandLink
      id="downloadPdfInline"
      value="Download PDF"
      action="#{myBean.downloadPdfInline}"
      target="_blank"
      />

      2. index.jsp - using richfaces commandLink and does not work
      -------------------------------------------------------------------------

      <a4j:commandLink
      id="downloadPdfInline"
      value="Download PDF"
      action="#{myBean.downloadPdfInline}"
      target="_blank"/>

      3. 'myBean' Beans and its methods
      --------------------------------------------
      public class MyBean
      {
      private String errorString;

      /* to open and display an pdf file - hardcoded filename*/
      public void downloadPdfInline()
      {

      // Prepare.
      File pdfFile = new File(getPdfFilePath(), getPdfFileName());
      // File pdfFile = new File(getPdfFileName());

      FacesContext context = FacesContext.getCurrentInstance();
      HttpServletResponse response =
      (HttpServletResponse) context.getExternalContext().getResponse();


      // Download file.
      try
      {
      HttpServletUtil.downloadFile(response, pdfFile, false);
      }
      catch (IOException e)
      {
      // Do your error handling thing.
      setErrorMessage("Download file failed: " + e.getMessage());
      e.printStackTrace();
      return;
      }

      // Prevent other JSF lifecycle phases eventually being invoked.
      // Otherwise you can get the following exception:
      // java.lang.IllegalStateException: Cannot forward after response has been committed.
      context.responseComplete();
      }

      private String getPdfFileName()
      {
      // TODO Auto-generated method stub
      //return "SummaryReleaseCalendar.xls";
      return "defect-01022008.xls";
      }


      private String getPdfFilePath()
      {
      // TODO Auto-generated method stub
      //return "C:\\JavaProjects\\Run\\WebContent";
      return "C:\\FileLoadTest";
      }

      private void setErrorMessage(String string)
      {
      errorString=string;

      }


      This 'downloadPdfInline' method in myBean call another method (HttpServletUtil.downloadFile) which I have not copied here due to space. If you would like to look at it, please let me know and I will copy it here.

      1. The way the program works is as we click the "Download PDF" link, it opens a pdf file. This works if the code is in JSF and opens the pdf file when the link is selected. In Richfaces, when the link is selected, it displays some unreadable characters. Can you please let me know what I am missing.

      Thanks
      Aish

        • 1. Re: Code that works in JSF is not working with Richfaces - p
          Sergey Smirnov Master

          a4j:commandLink in contrast to h:commandLink is an Ajax button. Ajax means partial replacement of the context of the same page. target="_blank" means that you going to get the result in new window. a4j:commandLink is not an appropriate component for this. It should not work in such situation by definition.

          Use just the classical h:commandLink

          • 2. Re: Code that works in JSF is not working with Richfaces - p
            E Aish Novice

            Sergey,

            Thanks for getting back to me on this. I have been spending some time on this - trying to figure out why it is not working. Thanks a lot.

            I have a quick question. This is not richfaces but related to JSF. I do not know where to find an answer. If you could help me with that, I would be very grateful to you as I have encountered this problem many times.

            The jsf programs that have h:commandLink in it works fine as an individual component. In reality, we develop application with a left hand side for menu and the right hand side for content display. We have a index.jsp (main) page with all the and <h:form> tags. Based on the menu selected, the content page will be displayed. The content page will have only the tags for content display and not the and <h:form> tags.

            If the content page has a h:commandLink, it expects a <h:form> tag around it. If we include <h:form> tag in the content page, then it becomes a nested form as the index.jsp has already a <h:form> tag and the content page does not work as expected.

            I am unaware of how to handle this situation if we have a h:commandLink/h:commandButton tag in the content page .. and the index.jsp has already 'form' tag in it. Can you please let me know..

            Thanks a lot
            Aish


            • 3. Re: Code that works in JSF is not working with Richfaces - p
              Shimon Lifshitz Newbie

              Why do you need another form, if you are already in a form, there is no need to create a form for the content?

              • 4. Re: Code that works in JSF is not working with Richfaces - p
                E Aish Novice

                Hi,

                Thanks for responding. Please look at the below content page:

                ====

                <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
                <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
                <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
                <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>

                <h:dataTable value="#{TableDetail.dirlist}" var="listfiles">
                <h:column>

                <f:facet name="header">
                <h:outputText value="File No"/>
                </f:facet>
                <h:outputText value="#{listfiles.fileno}">
                </h:outputText>
                </h:column>

                <h:column>
                <f:facet name="header">
                <h:outputText value="Files"/>
                </f:facet>

                <h:commandLink id="downloadExcelInline"
                value="#{listfiles.filename}"
                action="#{myBean.downloadExcelInline}"
                target="_blank">

                <f:param name="filename" value="#{listfiles.filename}" />
                </h:commandLink>

                </h:column>



                </h:dataTable>

                ====

                As soon as I enter the above program in IBM RAD(our development environment), I get the error message next to h:commandLink stating "This tag must be nested within tag". Even though the main page has the form tag, this content page with the h:commandLink expects it. Not sure how to handle it.. Please suggest

                Thanks
                Aish

                • 5. Re: Code that works in JSF is not working with Richfaces - p
                  Shimon Lifshitz Newbie

                  Simple one, just ignore your ide remark, the ide editor doen't know that your page is nested in another page. You know better :=)