在JSF中下载文件后,在同一页面上显示消息

时间:2014-07-09 23:49:21

标签: ajax jsf

必须下载csv文件,一旦完成,我需要在同一页面上显示一条消息。使用setInterval进行ajax轮询但获取非法状态异常:在写入数据后无法更改缓冲区大小。以下是代码:

Java Bean

package com.one;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;

import javax.annotation.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpServletResponse;

@ManagedBean

public class HelloBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
    private static int count=0;

    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        HelloBean.count = count;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
    public void getSayWelcome(){
       //check if null?
      /* if("".equals(name) || name ==null){
        return "";
       }else{
        return "Ajax message : Welcome " + name;
       }*/

       System.out.println("In Download");
        HttpServletResponse response=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
       response.setContentType("application/vnd.ms-excel"); // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
      // response.setContentLength(contentLength); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
      String fileName="MyStatements.csv";
       response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
       response.setContentLength(1000);  

      // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
   try{
       OutputStream output = response.getOutputStream();
       output.write(1234);
       output.flush();

   }

   catch(Exception e){

   System.out.println("Dekho"+e.toString());
   }

   finally{
    try {
            response.getOutputStream().close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Dekho2"+e.toString());

    }
   }
   }
    /*public void updates(AjaxBehaviorEvent event){
           System.out.println("Printing"+count++);
           this.name=name+"agdam";
          // return name;
    }*/
    public void updatess(AjaxBehaviorEvent event){
          System.out.println("Printing"+count++);
           this.name=name+"agdam";
          // return name;

    }
    public void download(){

        System.out.println("In Download");
        HttpServletResponse response=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
        response.setContentType("application/vnd.ms-excel"); // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
       // response.setContentLength(contentLength); // Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
       String fileName="MyStatements.csv";
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        response.setContentLength(1000);  

       // The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
    try{
        OutputStream output = response.getOutputStream();
        output.write(1234);
        output.flush();

    }

    catch(Exception e){

    System.out.println("Dekho"+e.toString());
    }

    finally{
        try {
            response.getOutputStream().close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Dekho2"+e.toString());
        }

    }
   // return;

        }
    /*public void actionListener(AjaxBehaviorEvent actionEvent) {
        // Add event code here...
        System.out.println("Made it!");
    }*/
}

JSF页面

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">

    <h:body>

                <h3>JSF 2.0 + Ajax Hello World Example</h3>

        <h:form id="form1">
           <h:inputText id="name" value="#{helloBean.name}"></h:inputText>
           <h:commandButton value="Welcome Me" action="#{helloBean.getSayWelcome}">

           </h:commandButton>

           <h2><h:outputText id="output" value="#{helloBean.name}" /></h2>


<h:commandButton id="changeme"  style="display:none" value="try">
   <f:ajax  listener="#{helloBean.updatess}" render="countsee"></f:ajax>
</h:commandButton>

<h2><h:outputText id="countsee" value="#{helloBean.count}" /></h2>
        </h:form>




    </h:body>
</html>

0 个答案:

没有答案