简体   繁体   English

FileDownload和FileUpload JSF Primefaces无法正常工作

[英]FileDownload and FileUpload JSF Primefaces not working

I'm using PrimeFaces 3.1.2, NetBeans 7.2, JSF 2.1 and GlassFish 3.1.2. 我正在使用PrimeFaces 3.1.2,NetBeans 7.2,JSF 2.1和GlassFish 3.1.2。

I'm using the actual code I got from http://www.primefaces.org/showcase/ui/fileUploadAuto.jsf and http://www.primefaces.org/showcase/ui/fileDownload.jsf . 我正在使用我从http://www.primefaces.org/showcase/ui/fileUploadAuto.jsfhttp://www.primefaces.org/showcase/ui/fileDownload.jsf获得的实际代码。

When I run the file upload code, it doesn't work at all. 当我运行文件上传代码时,它根本不起作用。 The file doesn't get uploaded and no success message is shown. 文件未上传,未显示成功消息。 But if the file size exceeds the size mentioned, it is showing a message that the file size is too large. 但是,如果文件大小超过上述大小,则会显示文件大小过大的消息。

Here is my view: 这是我的观点:

<?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://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <h:form enctype="multipart/form-data">
            <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                mode="advanced"
                update="messages"
                auto="true"
                sizeLimit="100000" 
                allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
            <p:growl id="messages" showDetail="true"/>
        </h:form>
    </h:body>
</html>

Here is my backing bean: 这是我的支持bean:

package com;

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;

@ManagedBean
@SessionScoped
public class FileUploadController {
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful",   event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

Its basically the same code in the PrimeFaces showcase page. 它与PrimeFaces展示页面中的代码基本相同。

Similarly with file download code; 与文件下载代码类似; when I click on download nothing happens. 当我点击下载时没有任何反应。 A pop up opens and closes before I could even notice it. 弹出窗口打开和关闭,然后才能注意到它。 I have the image file in place as mentioned in the get resource stream (for the download part), but I don't know what's the problem. 我有get资源流中提到的图像文件(对于下载部分),但我不知道是什么问题。 The code is also basically the same as in the PrimeFaces showcase page. 代码也与PrimeFaces展示页面中的代码基本相同。

I don't see any logs or errors under Glassfish in Netbeans. 我没有在Netbeans的Glassfish下看到任何日志或错误。 I also don't know how to enable logging if necessary. 如果有必要,我也不知道如何启用日志记录。

The first thing you need is add some libraries to your application. 您需要的第一件事是向您的应用程序添加一些库。 As a matter of fact, PrimeFaces file upload relies on Apache commons-file-upload and commons-io libraries. 事实上,PrimeFaces文件上传依赖于Apache commons-file-upload和commons-io库。 So dowload them and add them to your WEB-INF/lib path: 因此下载它们并将它们添加到WEB-INF / lib路径:

you can download it from following link. 你可以从以下链接下载它。

http://commons.apache.org/io/ http://commons.apache.org/io/

http://commons.apache.org/fileupload/ http://commons.apache.org/fileupload/

in addition you have to configure it into web.xml 此外,您必须将其配置为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>51200</param-value>
  </init-param>
  <init-param>
    <param-name>uploadDirectory</param-name>
    <param-value>C:\etc</param-value>
   </init-param>
</filter>
<filter-mapping>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Also If you want to set programmatically change the destination of your uploaded files have a look: 此外,如果要以编程方式设置更改上载文件的目标,请查看:

PrimeFaces FileUpload File Saving Destination PrimeFaces FileUpload文件保存目标

To be able use the bean from the xhtml, you need to annotate your controller as a @ManagedBean and set some scope. 为了能够使用xhtml中的bean,您需要将控制器注释为@ManagedBean并设置一些范围。 Preferably @ViewScoped or @RequestScoped in this case. 在这种情况下,最好是@ViewScoped或@RequestScoped。

example: 例:

@ManagedBean
@ViewScoped
public class FileUploadController {

    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful",     event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

Learn more about how jsf managed beans works here: http://www.mkyong.com/jsf2/configure-managed-beans-in-jsf-2-0/ 了解有关jsf托管bean如何在这里工作的更多信息: http//www.mkyong.com/jsf2/configure-managed-beans-in-jsf-2-0/

One thing that I have noticed is that if you don't put inside the tag "allowTypes" a regular expression correctly, the "fileupload" element does not trigger the action, giving the impression that the action is unreachable. 我注意到的一件事是,如果你没有正确地将标记“allowTypes”放入正则表达式,那么“fileupload”元素不会触发操作,给人的印象是操作无法访问。

I know that this is not your problem right now, but I think it is important to share this information. 我知道现在这不是你的问题,但我认为分享这些信息很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM