2 Replies Latest reply on Jan 8, 2008 10:49 AM by Mikael Andersson

    Text File download with RichFaces

    Richard Hardy Newbie

      Hi,
      I am using JBOSS Seam v2.0.0 and RichFaces 3.1.2
      I am trying to make the following happen:

      I have a page containing a commandLink (or commandButton) and when a user clicks on this, I want a file to be downloaded. This file will be generated by a backing bean method.

      I have set my code up like this:

       <h:commandLink immediate="true"
       action="#{searchManager.generateVCalendar}">
       <h:graphicImage value="/images/vcalendar.bmp"/>
       </h:commandLink>
      


      and


      public String generateVCalendar()
       {
       String fileContents = "text file";
      
       log.debug("generateVCalendar");
       if (!facesContext.getResponseComplete())
       {
       String fileName = "export.wri";
      
      
       try
       {
       ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
       HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
       String encoding = response.getCharacterEncoding();
       byte[] bytes = fileContents.getBytes(encoding);
       response.setContentType("text/plain");
       StringBuffer disposition = new StringBuffer("attachment; filename=\"");
       disposition.append("Plato-Termin");
       disposition.append(".txt");
       disposition.append("\"");
       response.setHeader("Content-Disposition", disposition.toString());
       response.setContentLength(bytes.length);
       ServletOutputStream out = null;
      
       out = response.getOutputStream();
       out.write(bytes);
       out.flush();
       out.close();
       facesContext.getApplication().getStateManager().saveSerializedView(facesContext);
       facesContext.responseComplete();
       }
       catch (Exception e)
       {
       log.error("generateVCalendar Exception: " + e.getMessage());
       }
       }
       return null;
       }


      However, when I click on the link, I get:

      java.lang.NullPointerException
       java.util.regex.Matcher.getTextLength(Matcher.java:1127)
       java.util.regex.Matcher.reset(Matcher.java:284)
       java.util.regex.Matcher.<init>(Matcher.java:205)
       java.util.regex.Pattern.matcher(Pattern.java:879)
       org.ajax4jsf.webapp.ParserConfig.getParser(ParserConfig.java:28)
       org.ajax4jsf.webapp.ConfigurableXMLFilter.getParser(ConfigurableXMLFilter.java:109)
       org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:259)
       org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:281)
       org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:60)
       org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
       org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
       org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)



      Can anyone help me as to why this is not working?
      Or, is there a better way to do a file download. e.g. <a4j:mediaOutput>? I have seen examples for this but these always seem to be for images embedded into a page.

      Thanks for your help,
      Richard.