我正在尝试根据演示中给出的示例来上传Primefaces 3.0的组件。我还在我的应用程序中添加了commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar以及primefaces-3.1.jar文件,该文件正在JBoss 6.1上部署。
问题是handleFileUpload()没有被调用。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
/context-param>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-value>server</param-value>
</context-param>
<!--Humanity theme for PrimeFaces -->
<context-param>
<param-name>primefaces.THEME</param-name>
</context-param>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>
org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</web-app>
<h:body>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="500000"
description="select template"
allowTypes="/(\.|\/)(doc|docx)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
/h:body>
</html>
FileUploadController.java
import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent; import org.primefaces.model.UploadedFile;
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event) {
System.out.println("**** Inside handleFileUpload() ***** ");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
答案 0 :(得分:3)
PrimeFaces文件上传过滤器需要映射到FacesServlet
。
您已声明FacesServlet
的servlet名称如下:
<servlet-name>FacesServlet</servlet-name>
但是,PrimeFaces文件上传过滤器映射在错误的servlet名称上(注意空格):
<servlet-name>Faces Servlet</servlet-name>
相应修复。