p:fileUpload Listener方法永远不会为mode ="simple"调用. [英] p:fileUpload Listener method is never invoked for mode="simple"

查看:108
本文介绍了p:fileUpload Listener方法永远不会为mode ="simple"调用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用p:fileUpload,但是如果我使用mode="simple",则不会调用fileUploadListener.有什么方法可以在简单模式下使用fileUploadListener.

I am using p:fileUpload but the fileUploadListener is not getting invoked if I use mode="simple" . Is there any way to use fileUploadListener in simple mode.

<p:fileUpload id ="uploading"
              fileUploadListener="#{workflowActionTemplate.handleFileUpload}"
              mode="simple" 
              update="messages"
              sizeLimit="100000" 
              allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
              multiple="true"/>

素颜:3.2

我已经完成了以下配置,如果我遗漏了任何东西,请告诉我.

I have done following configuration, please let me know if I am missing anything.

web.xml:

<!-- File Upload filter -->
<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>

pom.xml:

<!-- Dependancy for file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.2</version>
</dependency>

收听者方法:

public void handleFileUpload(FileUploadEvent event) {
  FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); System.out.println("msg : "+ msg);
  uploadedFile = event.getFile();
}

-

如果我使用value属性而不是fileUploadListener,并且不上传文件,则 fileUpload属性未设置,因此出现以下错误.

If I use value attribute instead of fileUploadListener and if I don't upload the file, then the fileUpload attribute not getting set and so it gives following error.

javax.faces.component.UpdateModelException: javax.el.ELException: /search/workflowAction.xhtml @181,104 value="#{workflowActionTemplate.uploadedFile}": Can't set property 'uploadedFile' of type 'org.primefaces.model.UploadedFile' on class 'com.principal.nq.statements.search.WorkflowActionTemplate$$EnhancerByCGLIB$$6ebcb7eb' to value ''

更新

由于fileUploadListener无法正常工作,我还试图以以下方式使用ajax调用来更新文件值.但是f:ajax无法执行Primefaces p:fileUpload组件.我也尝试过使用p:ajax,但是那也不起作用.

As fileUploadListener is not working I was also trying to use ajax call in the following way to update the file value. But f:ajax is not able to execute Primefaces p:fileUpload component. I also tried with p:ajax but that is also not working.

<p:fileUpload id="uploading"
              value="#{workflowActionTemplate.uploadedFile}"
              mode="simple"
              update="messages"
              sizeLimit="100000" 
              allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
              auto="true"/>
<p:growl id="messages" showDetail="true"/>
<h:commandButton id="uploadDocument" styleClass="continuebutton" value="#{msg.upload}" action="#{workflowActionTemplate.uploadParticipantCustomDoc}">
  <f:ajax execute="uploading" render="uploadDocumentDlg" onevent="onAjaxUploadCustomDoc"/>
</h:commandButton>

推荐答案

请按照以下步骤操作,以使您的代码完美无缺. 在XHTML文件中.

Please follow these steps to get your code perfectly fine. in the XHTML file.

    <p:fileUpload id="choose" validator="#{controllerClass.validateFile}" multiple="false" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"  value="#{controllerclass.uploadedfile}" required="true" mode="simple"/>

<p:commandButton ajax="false" id="saveBtn" update="errBrand,pnl" value="Save Brand" action="#{controllerClass.uploadFile()}" />

在web.xml中

定义以下过滤器和servlet.

in the web.xml define following filter and servlet.

  <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>
  </filter-mapping>

在控制器类中,您需要像这样定义方法主体

in the controller class you need to define method body like this

使用import org.primefaces.model.UploadedFile;

private UploadedFile uploadedfile;

如果要为上传的文件定义验证方法,可以这样编写

if you want to define validation method for uploaded file you can write it like this

public void validateFile(FacesContext ctx,
            UIComponent comp,
            Object value) {
        List<FacesMessage> msgs = new ArrayList<FacesMessage>();
        UploadedFile file = (UploadedFile)value;
        int fileByte = file.getContents().length;
        if(fileByte > 15360){
            msgs.add(new FacesMessage("Too big must be at most 15KB"));
        }
        if (!(file.getContentType().startsWith("image"))) {
            msgs.add(new FacesMessage("not an Image file"));
        }
        if (!msgs.isEmpty()) {
            throw new ValidatorException(msgs);
        }
    }

这篇关于p:fileUpload Listener方法永远不会为mode ="simple"调用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆