PrimeFaces单击更新按钮使文件消失

时间:2016-05-15 18:53:33

标签: jsf-2 primefaces updates

我正在使用JSF 2.0开发一个应用程序,并使用Primefaces组件库。但是,我遇到了Primefaces Library的 p:fileUpload 组件的问题。我基本上试图实现以下目标。

  
      
  1. 用户在点击上传按钮后上传.xls文件   该文件应保存到磁盘中的临时位置。

  2.   
  3. 接下来,当单击提交按钮时,将读取保存的文件   使用Apache POI逐行并保存到数据库中。

  4.   

但是,单击上传按钮按钮后文件会消失。作为JSF / Primefaces的新手,我无法弄清楚我要走向何方?

最糟糕的部分是没有收到错误消息,我可以看到JSF生命周期的所有阶段都在执行。 非常感谢任何帮助!!! 以下是我的代码

的index.html

<h:body>     
    <h:form prependId="false" enctype="multipart/form-data" id="welcomeForm" style="width:800px">
            <h4 style="color:red; font-family:Arial"><h:outputLabel value="Upload your file:" /></h4>           
            <p:growl id="growl" showDetail="true" />             
            <p:fileUpload  fileUploadListener="#{fileUploadBean.handleFileUpload}"
                            mode="advanced" 
                            dragDropSupport="false"
                            sizeLimit="100000" fileLimit="3" 
                            allowTypes="/(\.|\/)(xls|xlsx|docx|doc)$/"
                            update="growl"                          
                            />                                      
            <p:separator/>


            <p:commandButton value="Submit" actionListener="#{fileUploadBean.growlMessage}" update="growl" />           
            <h:message for="welcomeForm" />                 
    </h:form>

FileUploadBean.java

public class FileUploadBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String message;    
    private UploadedFile uploadedFile;
    private String myFileName;


    public FileUploadBean(){

    }      

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }


    public void growlMessage(){
        FacesContext context = FacesContext.getCurrentInstance();     
        context.addMessage(null, new FacesMessage("Your " + myFileName +"was uploaded successfully") ); }



    public void handleFileUpload(FileUploadEvent event) throws IOException {

            System.out.println("Reached here:::::");
            String name = uploadedFile.getFileName();
            System.out.println("File name: " + name);

            String type = uploadedFile.getContentType();
            System.out.println("File type: " + type);

            long size = uploadedFile.getSize();
            System.out.println("File size: " + size); 

            Path folder = Paths.get("C:/Windows/Temp");
            String filename = FilenameUtils.getBaseName(uploadedFile.getFileName()); 
            String extension = FilenameUtils.getExtension(uploadedFile.getFileName());
            Path file = Files.createTempFile(folder, filename + "-", "." + extension);

            try (InputStream input = uploadedFile.getInputstream()) {
                Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
            }

            System.out.println("Uploaded file successfully saved in " + file);

            FacesMessage msg = new FacesMessage("Succesful", event.getFile()
                    .getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            System.out.println("last line of file upload....");

    }

    public String save() throws IOException {       

            System.out.println("Uploaded file value ::::"+uploadedFile);

            if(uploadedFile != null) {
                FacesMessage message = new FacesMessage("Succesful", uploadedFile.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }

            else{

                /*String filename = FilenameUtils.getName(e.getFile().getFileName());  
                System.out.println("Filename is :::: " +filename);*/
                System.out.println("Uploaded File Name Is :: "+uploadedFile.getFileName()+" :: Uploaded File Size :: "+uploadedFile.getSize());

                System.out.println("FAILED !!!!! FAILED !!!!!  FAILED !!!!!");
            }

            return "true";           
    }

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>file_uploading</display-name>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>

  <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
  </context-param>
  <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>auto|native|commons</param-value>
   </context-param>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
            <init-param>
                <param-name>thresholdSize</param-name>
                <param-value>51200</param-value>
            </init-param>
            <init-param>
                <param-name>uploadDirectory</param-name>
                <param-value>C:\Windows\Temp</param-value>
            </init-param>
       </filter> 

    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>    

  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

0 个答案:

没有答案