我很困惑为什么组件p:fileUpload
在p:ajax
显示组件后不调用fileUploadListener
。如果我把它放在panelGrid
之外,甚至去掉它们,效果很好。
什么不起作用:
<div class="pure-control-group">
<label for="mostraNoIndex">Slideshow</label>
<p:selectBooleanCheckbox value="#{destaqueCadastrarBean.d.apareceNoSlide}">
<p:ajax event="change" update="upload"></p:ajax>
</p:selectBooleanCheckbox>
</div>
<h:panelGrid id="upload">
<h:panelGrid rendered="#{destaqueCadastrarBean.d.apareceNoSlide}">
<div class="pure-control-group">
<label for="mostraNoIndex">Imagem</label>
<p:fileUpload fileUploadListener="#{destaqueCadastrarBean.handleFileUpload}" mode="advanced" dragDropSupport="true" sizeLimit="100000000000" fileLimit="1" allowTypes="/(.|/)(gif|jpe?g|png)$/" />
</div>
</h:panelGrid>
</h:panelGrid>
工作原理:
<div class="pure-control-group">
<label for="mostraNoIndex">Slideshow</label>
<p:selectBooleanCheckbox value="#{destaqueCadastrarBean.d.apareceNoSlide}">
</p:selectBooleanCheckbox>
</div>
<div class="pure-control-group">
<label for="mostraNoIndex">Imagem</label>
<h:fileUpload fileUploadListener="#{destaqueCadastrarBean.handleFileUpload}" mode="advanced" dragDropSupport="true" sizeLimit="100000000000" fileLimit="1" allowTypes="/(.|/)(gif|jpe?g|png)$/" />
</div>
刚刚找到了一个解决方案。被调用的bean是RequestScoped
,改为ViewScoped
。我想,由于bean是在加载表单时请求的,并且p:fileUpload只能在创建bean后出现,所以组件无法访问bean。有了视图范围,bean就可以用于后面的组件。如果有人有更专业、更清晰的解释,我希望能更好地理解。