gpt4 book ai didi

jsf - 为什么 UploadedFile 为空?

转载 作者:行者123 更新时间:2023-12-03 23:57:33 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable

(11 个回答)


4年前关闭。




我正在使用 JSF 2.2 开发 Java Web 应用程序和 Primefaces .我希望用户可以上传单个文件;为此,我使用了 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>

我的bean代码是
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.UploadedFile;

@ManagedBean
@RequestScoped
public class ZipBean{

UploadedFile arch;

public ZipBean() {
}

public void subir() {
try {
FacesContext context = FacesContext.getCurrentInstance();
if (this.arch != null) {
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "PERFECT!","PERFECT!"));
} else {
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "FILE NULL","FILE NULL"));
}
} catch (Exception e) {
System.out.println(e.toString());
}
}

public UploadedFile getArch() {
return arch;
}

public void setArch(UploadedFile arch) {
this.arch = arch;
}

}

我的 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>

当我上传文件时, p:grow显示消息 FILE IS NULL .

我有下一个 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 中声明咆哮
    有关更多信息,请参阅此 How to upload file in primefaces

    文件上传 View .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);
    }
    }
    }

    基本.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>

    关于jsf - 为什么 UploadedFile 为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41641469/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com