1

在 PrimeFaces 3.5 中上传文件之前,是否可以进行一些服务器端处理?假设表单包含一些p:inputText元素,需要在文件上传之前填写,那么我该如何检查呢?有什么活动吗?

视图看起来像 -

<h:form enctype="multipart/form-data">
    <p:inputText value="#{fileUploadController.name}" id="name" label="Name" />
    <p:inputText value="#{fileUploadController.about}" id="about" label="About" />
    <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
            mode="advanced" 
            update="messages"
            sizeLimit="100000" 
            allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

    <p:growl id="messages" showDetail="true"/>

</h:form>

ManagedBean 如下 -

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

public class FileUploadController {

    private String name;
    private String about;
    //Getters & Setters

    public void beforeUpload(){
    if(getName().isEmpty()||getAbout().isEmpty())
        FacesMessage msg = new FacesMessage("Error", "Name or About cannot be empty");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

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

2 回答 2

3

<p:fileUpload/>有一个onStart属性,您可以方便地与<p:remoteCommand/>组件连接以在上传操作开始之前触发支持 bean 操作

      <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
        mode="advanced" 
        onstart="startSomething();"
        update="messages"
        sizeLimit="100000" 
        allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

      <p:remoteCommand name="startSomething" action="#{bean.startSomething}"/>  
于 2013-04-19T06:58:42.180 回答
1

我在文本字段和文件上传方面遇到了类似的情况。除了检查handleFileUpload 中的名称值外,我找不到任何其他方法来执行此操作。

public void handleFileUpload(FileUploadEvent event){
  HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();`
  name =req .getRequestParams("formId:name")
  if(name==null){
   FacesMessage msg = new FacesMessage("Error", "Name or About cannot be empty")
   return;
  }
}
于 2013-04-18T19:37:45.167 回答