我尝试使用p:fileUpload但是没有调用fileUploadListener方法,也没有生成任何异常。没有PrettyFaces,文件上传工作正常。
经过一番研究,我发现重写常见问题解答(http://ocpsoft.org/rewrite/docs/faq)回答了“为什么文件上传不能正常工作?”可能是解决方案,但仅在Tomcat上进行测试。我在GlassFish上试过但没有成功。
然后我在OcpSoft论坛上发现了一个解决我问题的帖子:http://ocpsoft.org/support/topic/pretty-primefaces-fileupload/page/2/#post-25781。只需将文件上传代码放在其他页面源代码中,并使用html iframe标记进行引用。
好的,问题解决了,但我对iframe解决方案不满意,而fileUploadListener方法应该在没有解决方法的情况下调用。那么,任何人都知道如何配置文件上传与PrettyFaces一起使用?
Maven依赖项:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.ocpsoft</groupId>
<artifactId>prettyfaces-jsf2</artifactId>
<version>3.3.3</version>
</dependency>
Xhtml源代码:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadMB.handleFileUpload}" auto="true"
update="fileName" />
<h:outputText id="fileName" value="#{fileUploadMB.uploadedFileName}" />
</h:form>
</h:body>
</html>
管理bean:
@ManagedBean
@ViewScoped
@URLMapping(pattern = "/index", viewId = "/index.xhtml")
public class FileUploadMB {
private UploadedFile uploadedFile;
private String uploadedFileName;
public void handleFileUpload(FileUploadEvent evt) {
this.uploadedFile = evt.getFile();
this.uploadedFileName = evt.getFile().getFileName();
}
public String getUploadedFileName() {
return uploadedFileName;
}
public void setUploadedFileName(String uploadedFileName) {
this.uploadedFileName = uploadedFileName;
}
}
和web.xml:
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>com.ocpsoft.pretty.BASE_PACKAGES</param-name>
<param-value>
<!-- Packages with @URLMapping annotation from PrettyFaces -->
br.nti.ufms
</param-value>
</context-param>
<context-param>
<param-name>com.ocpsoft.pretty.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<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>
<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>*.xhtml</url-pattern>
</servlet-mapping>
答案 0 :(得分:2)
这可能是一个过滤器链问题。尝试在注册过滤器中添加以下内容:
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
在prettyfaces faq question 2中查看更多内容。
答案 1 :(得分:0)
对于我来说,使用Primefaces 5.0&amp; Prettyfaces在Tomcat 7.0.41上重写2.0.12-final。
但是我从web.xml
<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>51200000</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/uploadedFilesDirectoryForGlassfish</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
并添加到context.xml
这个:
<Context allowCasualMultipartParsing="true">
p:fileUpload
也开始在重写的网址上正常工作。
This page可能有用。
另见:http://www.ocpsoft.org/rewrite/docs/faq&amp; this SO answer
但等等,
刚刚观察到它只适用于小文件上传,对于大文件上传,fileUpload侦听器仍然没有被触发。有解决方案的人吗?