3 Replies Latest reply on Nov 25, 2006 2:32 PM by gavin.king

    link action passes null instead of object

    modoc

      I'm working off of some example code pasted in this forum for downloading pdfs, but I'm having issues. I have a datatable showing a list of attachments from an e-mail. I want the link to then download the file. Using the pdf download example in this forum. What is happening is the method on the seam component is getting called, but the attachment parameter is being passed in as null. No errors, it's just null.

      Any ideas?


      The download component code (POJO, not EJB):

      /**
       *
       */
      package com.digitalsanctuary.seam;
      
      import java.io.ByteArrayInputStream;
      import java.io.IOException;
      
      import javax.ejb.Remove;
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      import javax.servlet.ServletOutputStream;
      import javax.servlet.http.HttpServletResponse;
      
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.log.Log;
      
      import com.digitalsanctuary.mail.message.Attachment;
      
      /**
       * @author modoc
       *
       */
      @Name("downloadAttachment")
      @Scope(ScopeType.SESSION)
      public class DownloadAttachment {
       @In(value = "#{facesContext.externalContext}")
       private ExternalContext mExternalContext;
      
       @In(value = "#{facesContext}")
       private FacesContext mFacesContext;
      
       @Logger
       private Log mLog;
      
       public String download(Attachment pAttachment) {
       if (pAttachment == null) {
       return null;
       }
       int read = 0;
       byte[] bytes = new byte[1000];
      
       HttpServletResponse response = (HttpServletResponse) mExternalContext.getResponse();
       response.setContentType("application/octet");
       response.addHeader("Content-disposition", "attachment; filename=\"" + pAttachment.getFileName() + "\"");
      
       try {
       ServletOutputStream responseOutputStream = response.getOutputStream();
       ByteArrayInputStream attachmentInputStream = new ByteArrayInputStream(pAttachment.getContent());
       while ((read = attachmentInputStream.read(bytes)) != -1) {
       responseOutputStream.write(bytes, 0, read);
       }
       responseOutputStream.flush();
       responseOutputStream.close();
       this.mFacesContext.responseComplete();
       } catch (IOException ioe) {
       mLog.error("IOException trying to send out attachment file", ioe);
       }
       return null;
       }
      
       @Destroy
       @Remove
       public void destroy() {
       }
      
      }
      



      The relevant section of the xhtml:

       <h:outputText rendered="#{mailQueue.currentMessage.attachmentCount > 0}">
       <div id="toplinedblock">
       <strong>Attachments:</strong><br />
       <h:dataTable value="#{mailQueue.currentMessage.attachments}" var="attachment"
       rendered="#{mailQueue.currentMessage.attachmentCount>0}">
       <h:column>
       <f:facet name="header">Name</f:facet>
       <s:link value="#{attachment.fileName}" action="#{downloadAttachment.download(attachment)}" />
       </h:column>
       <h:column>
       <f:facet name="header">Size</f:facet>
       #{attachment.contentSizeInK} KB
       </h:column>
       </h:dataTable>
       </div>
       </h:outputText>
      




      The attachment is definitely working correctly in the datatable, as all it's values appear correctly. When the link is clicked, the method gets called, but the pAttachment comes in as null into the java.

      Thoughts?

      Thanks!