【问题标题】:p:fileUpload method not firedp:fileUpload 方法未触发
【发布时间】:2012-02-05 20:11:16
【问题描述】:

我有一个带有 p:fileUpload 的表单,当我提交表单时,所有方法都不会触发

这是我的 xhtml:

<h:form enctype="multipart/form-data">
          <p:messages id="messages" showDetail="true"/>
          <p:fileUpload value="#{uploadBean.file}" mode="simple" id="fileUploadId"/>
          <p:commandButton value="Envoyer ce fichier" process="@form" update="messages fileUploadId" actionListener="#{uploadBean.upload}"/>
</h:form>

我的豆子:

public void setFile(final UploadedFile file)
  {
    System.out.println("Dans le setFile");
    this.file = file;
  }

  public void upload()
  {
    System.out.println("Dans le upload");
    System.out.println("Fichier : " + file.getFileName());
    FacesContext.getCurrentInstance().addMessage(null, msg);
  }

我的 web.xml:

<filter>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <filter-class>
    org.primefaces.webapp.filter.FileUploadFilter
  </filter-class>
</filter>
<filter-mapping>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

在跟踪中,我刚刚:

Infos: Dans le upload
Grave: Réception de «java.lang.NullPointerException» lors de l’invocation du listener d’action «#{uploadBean.upload}» du composant «j_idt11»
Grave: java.lang.NullPointerException

setFile()方法没有调用...

谢谢

编辑: 我的 bean 的所有代码:

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class UploadBean implements Serializable
{
  /**
   * 
   */
  private static final long serialVersionUID = 556636819990963651L;

  private UploadedFile      file;

  public UploadedFile getFile()
  {
    System.out.println("Dans le getFile");
    return file;
  }

  public void setFile(final UploadedFile file)
  {
    System.out.println("Dans le setFile");
    this.file = file;
  }

  public void upload()
  {
    System.out.println("Dans le upload");
    // System.out.println("Fichier : " + file.getFileName());

    FacesMessage msg;
    if (file == null)
    {
      msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Raté ! ", "Le fichier vaut null.");
      System.out.println("la variable file : null");
    }
    else
    {
      msg = new FacesMessage("Ouép ! ", file.getFileName() + " is uploaded.");
      System.out.println("Le nom du fichier uploader est : " + file.getFileName());
    }
    FacesContext.getCurrentInstance().addMessage(null, msg);
  }
}

【问题讨论】:

  • 我可以从你的堆栈跟踪中找到的关键字是java.lang.NullPointerException。我建议你从那里开始,寻找到底是什么null
  • @Mr.J4mes :这是我的变量“文件”。
  • 你能从你的setFile方法中删除final这个词吗?
  • hmmm... 我能想到的最后一个问题是您为 Scope 导入了错误的包。你能提供你uploadBean的所有代码吗?
  • 我已经用我的 bean 的所有代码编辑了我以前的帖子

标签: jsf file-upload primefaces


【解决方案1】:

从您提供的内容来看,我认为您到目前为止做得正确。但是,您仍然需要注意两件事:

  1. 您需要下载common-io & common-fileupload 并将.jar 文件导入您的Library 文件夹中。
  2. 您还需要确保web.xml 或任何使用@WebFilter 注释的类中没有其他过滤器,它们可能在PrimeFaces 的过滤器之前读取HttpServletRequest#getInputStream(),因为它只能读取一次。

【讨论】:

  • 1) 我已经下载了这两个文件(并导入到我的库中)。 2)我的 web.xml 中没有其他过滤器,也没有阅读 HttpServler#getInputStream() 的类...我不明白为什么它不起作用...
  • 使用 PrimeFaces 的代码(未修改)我可以在 Firebug 中看到网络上的文件,但它不调用我的方法。如果我添加 ajax,方法会被调用,但我在 Firebug 的网络选项卡上看不到文件...
  • 我试过测试你的代码,它对我有用。我还没有弄清楚它对您不起作用的任何其他原因。
  • 好的,谢谢。我不知道为什么它对我不起作用^^'
  • 在你这边一切正常......你有所有(两个)方法称为(setFile 和 Upload)?使用相同的代码,它在您这边运行良好,而不是在我这边。真是奇怪……
【解决方案2】:

你也在使用漂亮的面孔吗?那就试试吧(设置dispatcher):

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

【讨论】:

    【解决方案3】:

    我有一个类似的问题,表单没有提交,setters 方法也没有被调用,添加

    <f:ajax event="change"/> 
    

    在你想要提交的组件内部。还有

    "immediate=true"
    

    在组件本身上。想法可能对其他人有帮助..

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 2019-04-08
      • 2012-08-25
      相关资源
      最近更新 更多