在 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);
}
}