Jsf p:fileUpload侦听器方法从未为mode=";调用;简单的;
我正在使用Jsf p:fileUpload侦听器方法从未为mode=";调用;简单的;,jsf,jsf-2,primefaces,Jsf,Jsf 2,Primefaces,我正在使用p:fileUpload,但是如果我使用mode=“simple”,则不会调用fileUploadListener。是否有任何方法可以在简单模式下使用fileUploadListener <p:fileUpload id ="uploading" fileUploadListener="#{workflowActionTemplate.handleFileUpload}" mode="simple"
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"/>
--
如果我使用value属性而不是fileUploadListener,并且如果我不上传文件,那么
fileUpload属性未设置,因此出现以下错误
javax.faces.component.UpdateModelException:javax.el.eleException:/search/workflowAction.xhtml@181104 value=“#{workflowActionTemplate.uploadedFile}”:无法将类“com.principal.nq.statements.search.WorkflowActionTemplate$$EnhancerByGlib$$6ebcb7eb”上类型为“org.primefaces.model.uploadedFile”的属性“uploadedFile”设置为值“”
更新
由于fileUploadListener不起作用,我还尝试以以下方式使用ajax调用来更新文件值。但是f:ajax
无法执行Primefacesp: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()}" />
如果你想为上传的文件定义验证方法,你可以这样写
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);
}
}
public void validateFile(FacesContext ctx,
UIComponent comp,
对象值){
List msgs=new ArrayList();
UploadedFile=(UploadedFile)值;
int fileByte=file.getContents().length;
如果(文件字节>15360){
添加(新的FacesMessage(“太大了,不能超过15KB”);
}
if(!(file.getContentType().startsWith(“image”)){
添加(新的FacesMessage(“不是图像文件”);
}
如果(!msgs.isEmpty()){
抛出新的验证器异常(msgs);
}
}
@ronnk:OP表示在使用mode=“advanced”
时,它工作正常@Arpit:你是在嵌套表单吗?@BalusC你是对的,它不是在简单模式下工作的。不,我没有嵌套表单。我还尝试使用ajax,以便实现相同的功能,但f:ajax和p:ajax都无法执行primeface p:fileUpload组件。ELException
表示setter方法缺失或错误。请注意,mode=“simple”
不支持ajax(而且与mode=“advanced”
相关的所有其他属性,如update
、sizeLimit
、allowTypes
和auto
都被完全忽略)。因此,如果您在使用mode=“simple”
时一直试图通过ajax上传,那么首先您就错了。另请参阅RongNK提到的链接。@BalusC谢谢,但我还不知道为什么fileUploadListener不能在简单模式下工作。我已经搜索了很多,也遵循了RongNK提供的链接中提到的步骤,但没有运气。感谢您的回答,但我不确定我是否能够测试出来,因为我现在不再在该领域工作了。无论如何,我感谢你的帮助。
<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>
<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()}" />
<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>
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);
}
}