我的p:fileUpload有问题。在我的Bean中,它在我加载文件时检索空值。我使用PrimeFaces3.5
这是我的.xhtml
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<p:messages showDetail="true"/>
<p:panelGrid columns="2">
<h:outputLabel for="image" value="Select Picture" />
<p:fileUpload value="#{uploadImage.file}" mode="advance" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:commandButton value="Submit" ajax="false"
action="#{uploadImage.test1()}"/>
</p:panelGrid>
</h:form>
</h:body>
</html>
这是UploadImage.java
package AS_2;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
/**
*
* @author Mac
*/
@ManagedBean
@SessionScoped
public class UploadImage implements Serializable{
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
/**
* Creates a new instance of UploadImage
*/
public void test1() {
try {
if (file != null) {
FacesMessage msg = new FacesMessage("Succesful is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} else {
FacesMessage msg = new FacesMessage("failure to upload");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public UploadImage() {
}
}
web.xml就像是:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<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>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/mainPage.xhtml</welcome-file>
</welcome-file-list>
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
<filter-name>Extensions Filter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Extensions Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Extensions Filter</filter-name>
<url-pattern>/faces/myFacesExtensionResource/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Extensions Filter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Extensions Filter</filter-name>
<url-pattern>*.xhtml</url-pattern>
</filter-mapping>
</web-app>
这是我的faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.FileUploadRenderer</renderer-type>
<renderer-class>AS_2.MyFileUploadRenderer</renderer-class>
</renderer>
</render-kit>
</faces-config>
请告诉我这件事。
答案 0 :(得分:0)
<p:fileUpload value="#{uploadImage.file}"
mode="advance" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
在您的代码中,您遗漏了fileUploadListener
。将上面的代码添加到:
<p:fileUpload value="#{uploadImage.file}"
fileUploadListener="#{uploadImage.handleFileUpload}"
mode="advance" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
然后在UploadImageBean
,
public void handleFileUpload(FileUploadEvent event) {
this.file=event.getFile();
}
您不能只在value
中拥有p:fileUpload
,还需要fileUploadListener
。我希望有所帮助。
答案 1 :(得分: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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
此外,如果您使用高级模式,请不要使用value属性,因为您在侦听器中管理上传的文件。