我正在使用p:fileUpload
但是如果我使用mode="simple"
则不会调用fileUploadListener。有没有办法在简单模式下使用fileUploadListener。
<p:fileUpload id ="uploading"
fileUploadListener="#{workflowActionTemplate.handleFileUpload}"
mode="simple"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
multiple="true"/>
主要面孔:3.2
我已完成以下配置,如果我遗漏任何内容,请告诉我。
veb.hml:
<!-- 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.hml:
<!-- 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属性没有设置,因此它给出了以下错误。
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
,但这也没有用。
<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文件中。
<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。
<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>
在控制器类中,您需要像这样定义方法体
使用import org.primefaces.model.UploadedFile;
private UploadedFile uploadedfile;
如果要为上传的文件定义验证方法,可以这样写
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);
}
}