primefaces JSF P:fileUpload 无法获取响应

发布于 2024-12-15 02:12:47 字数 780 浏览 3 评论 0原文

我遇到了 PrimeFaces 的 问题。我创建了一个 Facelet 页面来上传 Excel 文件,如下所示:

<p:fileUpload fileUploadListener="#{blackListImportBean.xlsFileUpload}"
    multiple="true" allowTypes="*.xls;*.xlsx" description="*.xls;*.xlsx" 
    sizeLimit="100000"/>
<h:commandButton actionListener="#{blackListImportBean.test}" 
    value="#{msg.SAVE}" action="test-page.xhtml" />

和 bean java 代码如下:

public void xlsFileUpload(FileUploadEvent event){
    // ...
}

public void test() {
    // ...
}

当我单击按钮时,调用方法 test() 并调用方法 xlsFileUpload()< /code>没有被调用,错误提示找不到方法xlsFileUpload(),因为该方法需要参数。当我删除参数时,页面找不到该方法。另一个让我困惑的问题是我无法获取上传文件。我按照文档做了,但我不知道该怎么办。

I am facing a problem with <p:fileUpload> of PrimeFaces. I created a Facelet page to upload the Excel file as below:

<p:fileUpload fileUploadListener="#{blackListImportBean.xlsFileUpload}"
    multiple="true" allowTypes="*.xls;*.xlsx" description="*.xls;*.xlsx" 
    sizeLimit="100000"/>
<h:commandButton actionListener="#{blackListImportBean.test}" 
    value="#{msg.SAVE}" action="test-page.xhtml" />

And bean java code as below:

public void xlsFileUpload(FileUploadEvent event){
    // ...
}

public void test() {
    // ...
}

When I click the button, the method test() is called and the method xlsFileUpload() is not invoked and an error prompts that it cannot find the method xlsFileUpload(), because the method need the parameter. When I remove the parameter, the page cannot find the method. Another issue which confused me is that I cannot get the upload file. I did it as per the documentation and I do not know what should I do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温馨耳语 2024-12-22 02:12:47

两个问题:

1)您使用的是 Primefaces 2.X 还是 3.X?
2)堆栈跟踪上有什么?它可能包含有关原因的信息。

文件上传组件按照自己的事件序列上传文件,以便在用户触发文件上传时触发。这可以通过属性 auto="true" 自动实现。或者,它显示一个导致上传的“上传”按钮。因此,它与第二个操作(即您的测试方法)分开。

从它找不到你的方法的事实来看,我猜测要么 bean 不受管理,要么你的环境不同步(干净的构建)。

另外,尝试一个简单的测试:

@ViewScope
public class TestBean
{
  public void handleFileUpload(FileUploadEvent evt)
  {
     System.out.println("Handling Upload: " + evt.getFile());
     UploadedFile upload = evt.getFile();
FacesContext.getCurrentInstance()
                .addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File Uploaded", "This file is " + upload));
     . . . //do whatever here....
  }
}

//JSF Page

. . .
     <h:form>
        <p:messages id="messages" />
        <p:fileUpload 
          fileUploadListener="#{testBean.handleFileUpload}"   
          multiple="true" 
          allowTypes="*.*;" 
          update="messages"
        />
      </h:form>

. . .

如果设置了过滤器,您应该会看到针对每个上传的文件显示的一系列消息。如果没有,您应该会收到一条有用的错误消息。另外,请注意,您的路径上需要大量的基本 Apache 库 (CommonsFileUpload),这很可能会导致您的问题。

Two questions:

1) Are you using Primefaces 2.X or 3.X?
2) What is on the stack trace? It probably contains the information as to why.

The file-upload component uploads the file on its own event sequence so that will get triggered when the user triggers the file upload. This can be automatic via the property auto="true". Alternatively it displays an "upload" button that causes the upload. As such, it is separated from the second action which is your test method.

Judging from the fact it can't find your method I would guess that either bean is unmanaged or that your environments are out of sync (clean build).

Also, try a simple test:

@ViewScope
public class TestBean
{
  public void handleFileUpload(FileUploadEvent evt)
  {
     System.out.println("Handling Upload: " + evt.getFile());
     UploadedFile upload = evt.getFile();
FacesContext.getCurrentInstance()
                .addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File Uploaded", "This file is " + upload));
     . . . //do whatever here....
  }
}

//JSF Page

. . .
     <h:form>
        <p:messages id="messages" />
        <p:fileUpload 
          fileUploadListener="#{testBean.handleFileUpload}"   
          multiple="true" 
          allowTypes="*.*;" 
          update="messages"
        />
      </h:form>

. . .

If your filter is set you should see a series of messages displayed for each file that's uploaded. If not, you should get a useful error message. Also, be aware you need a fair amount of basic Apache libraries (CommonsFileUpload) on the path and odds are that this is causing your problem.

权谋诡计 2024-12-22 02:12:47

不要忘记将其添加到您的 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>

Don't forget to add this in your 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文