Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 为什么UploadedFile为空?_Jsf_File Upload_Primefaces - Fatal编程技术网

Jsf 为什么UploadedFile为空?

Jsf 为什么UploadedFile为空?,jsf,file-upload,primefaces,Jsf,File Upload,Primefaces,我正在用jsf2.2和Primefaces开发一个javaweb应用程序。我想用户可以上传单个文件;为此,我使用了Primefaces 6.0,但它不起作用,我只找到了Primefaces 5.x的教程(和示例),它也不适用于Primefaces 5.0 我倾向于使用Primefaces 6.0,但如果您能帮助我使用Primefaces 5.x版本,那没关系 我的web.xml是: <?xml version="1.0" encoding="UTF-8"?> <web-app

我正在用
jsf2.2
Primefaces
开发一个javaweb应用程序。我想用户可以上传单个文件;为此,我使用了
Primefaces 6.0
,但它不起作用,我只找到了
Primefaces 5.x
的教程(和示例),它也不适用于
Primefaces 5.0

我倾向于使用
Primefaces 6.0
,但如果您能帮助我使用
Primefaces 5.x
版本,那没关系

我的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>primefaces.THEME</param-name>
        <param-value>bootstrap</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <filter>
        <filter-name>primeFacesFileUploadFilter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>primeFacesFileUploadFilter</filter-name>
        <servlet-name>facesServlet</servlet-name>
    </filter-mapping>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>
我的xhtml是:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <p:growl autoUpdate="true"/>
        <h:form enctype="multipart/form-data">
            <p:fileUpload mode="simple" value="#{zipBean.arch}" />
            <p:commandButton value="Upload" action="#{zipBean.subir}" ajax="false" />
        </h:form>
    </h:body>
</html>

Facelet标题
当我上传文件时,
p:grow
显示消息
文件为空

我有下一个
jar
库:

  • commons-fileupload-1.3.1.jar
  • commons-io-2.4.jar

谢谢大家!

下面是一个如何使用primefaces 5.0上传文件的示例;您不需要commons-fileupload-1.3.1.jar和commons-io-2.4.jar;而且您不需要更改web.xml,也不需要在h:form中声明growl 有关更多信息,请参见此

java

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;
@ManagedBean
public class FileUploadView {
    private UploadedFile file;
    public UploadedFile getFile() {
        return file;
    }
    public void setFile(UploadedFile file) {
        this.file = file;
    }
    public void upload() {
        if(file.getSize() > 0) {
            FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
else{
    FacesMessage message = new FacesMessage("Not Succesful", "file is not uploaded");
            FacesContext.getCurrentInstance().addMessage(null, message);
}
    }
}
basic.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
<h:form enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />

    <p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>

    <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" />
</h:form>
  </h:body>
</html>

Facelet标题

完美!非常感谢你!
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
<h:form enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />

    <p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>

    <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" />
</h:form>
  </h:body>
</html>