5 Replies Latest reply on Jun 5, 2008 9:20 PM by jeffchiu

    Exporting to CSV in SEAM

    felixk2

      I'm trying to export some data to CSV in SEAM. It's pretty easy to do. I just have a view with:


      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                      xmlns:s="http://jboss.com/products/seam/taglib"
                      xmlns:ui="http://java.sun.com/jsf/facelets"
                      xmlns:f="http://java.sun.com/jsf/core"
                      xmlns:h="http://java.sun.com/jsf/html"
                      xmlns:rich="http://richfaces.org/rich"
                      xmlns:a="http://richfaces.org/a4j"> 
           <f:view contentType="text/csv">
                csv stuff goes here
           </f:view>
      </ui:composition>



      My problem is that when I view the .seam page in Firefox, it doesn't recognize that it's a CSV. It sees the SEAM extension and uses my OS default for opening up .seam files.


      Anyone have any ideas how I can solve this problem?


      Thanks,
      Felix

        • 1. Re: Exporting to CSV in SEAM
          jimk1723

          Can you add a servlet mapping in your web.xml?


             <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>yourfile.csv</url-pattern>
             </servlet-mapping>
          



          Where your JSF view is yourfile.xhtml.


          If you need to change the mime type for that extension, look at server/default/deploy/jboss-web.deployer/conf/web.xml; all of th default mime-types are in there.


          • 2. Re: Exporting to CSV in SEAM
            srini.ragu

            You may need to set the 'Content-Disposition'  header field with value "inline;filename=hello.csv"


            Or have a separate view with url mapped as mentioned above.

            • 3. Re: Exporting to CSV in SEAM
              felixk2

              Thank you. Your suggestions worked perfectly. One more thing:


              Any idea how I can stick a newline into my view? When I try

              \n

              it puts the literal text into my output.


              Thanks,
              Felix

              • 4. Re: Exporting to CSV in SEAM
                alteskind

                Hi! I have the following view:


                
                <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                
                                xmlns:s="http://jboss.com/products/seam/taglib"
                
                                xmlns:ui="http://java.sun.com/jsf/facelets"
                
                                xmlns:f="http://java.sun.com/jsf/core"
                
                                xmlns:h="http://java.sun.com/jsf/html"
                
                                xmlns:rich="http://richfaces.org/rich"
                
                                xmlns:a="http://richfaces.org/a4j"> 
                
                     <f:view contentType="text/csv">
                
                          12, 323, 43, 76, 32, 62, 73, 38
                
                     </f:view>
                
                </ui:composition>
                
                



                Where do I have to set the Content-Disposition header field?


                Thanks a lot!

                • 5. Re: Exporting to CSV in SEAM
                  jeffchiu

                  I know this issue is old but in case anyone stumbles across this page, here's a way to send the HTTP header from JSF. First, create a method in the action to set the header on the response:


                  public void httpHeadersForCSV() {
                    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
                    response.setHeader("Content-disposition", "attachment;filename=data.csv");
                  }
                  


                  Then, to call it from JSF pages, I added the following entry in pages.xml. The action gets called before the page is rendered, according to the seam docs.


                  <page view-id="/generate_csv.xhtml" action="#{action.httpHeadersForCSV}"/>
                  



                  Not sure if this is the best way to do it, but it is fairly clean and works.