2011-11-09 63 views
0

我面临PrimeFaces的<p:fileUpload>问题。我创建了一个facelet页上传的Excel如下文件:primefaces JSF P:fileUpload无法获得响应

<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" /> 

而且如下豆java代码:

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

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

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

回答

0

两个问题:

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

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

从它无法找到您的方法的事实来判断,我猜测bean是非托管的或者您的环境不同步(clean build)。

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

@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页面

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

. . . 

如果您的过滤器设置,你应该看到一系列针对影片上传的每个文件显示的消息。如果没有,你应该得到一个有用的错误信息。此外,请注意,您需要在路径上使用相当数量的基本Apache库(CommonsFileUpload),并且可能会导致您的问题。

+0

我使用primefaces RC2.2.jar,我复制给我的样本,不能得到我想要的结果。 – smilefatter

+0

我使用primefaces RC2.2.jar,我复制给我的样本,无法得到我想要的结果,我清理并重建项目。这个bean实际上没有被管理,我不知道原因。 – smilefatter

+0

问题已经得到了解决,该错误是在我的web.xml – smilefatter

0

不要忘了在你的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> 
+0

是的,我已经在我的web.xml中配置了它,谢谢 – smilefatter

+0

确保servlet名称对应,因为这可能是原因。 –

+0

是的,谢谢^ =^ – smilefatter