如何从< ui:repeat>中的jsf页面获取参数到BackingBean [英] How to get parametrs to BackingBean from jsf page in <ui:repeat>
问题描述
我的xhtml:
<ui:repeat value="#{c.voices}" var="v">
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{AddNote.handleFileUpload}"
converterMessage="converterMessage"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/">
</p:fileUpload>
<p:growl id="messages" showDetail="true"/>
</h:form>
</ui:repeat>
我的BackingBean:
My BackingBean:
public void handleFileUpload(FileUploadEvent event) {
//int v.id= here i need to know the v.id value from ui:repeater
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
c.voices 是对象列表.每个对象都有属性 id .我需要知道 handelFileUpload 中的 id ,该如何实现?
c.voices is list of objects. Every object have attribute id. I need to know the id in handelFileUpload how can I achieve that?
推荐答案
我只是将v.id
放在隐藏字段中:
I would simply put v.id
in an hidden field :
您的xhtml:
<ui:repeat value="#{c.voices}" var="v">
<h:form enctype="multipart/form-data">
<input type="hidden" name="vid" value="#{v.id}" />
<p:fileUpload fileUploadListener="#{AddNote.handleFileUpload}"
converterMessage="converterMessage"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/">
</p:fileUpload>
<p:growl id="messages" showDetail="true"/>
</h:form>
</ui:repeat>
然后使用FacesContext
将其重新放入Bean中:
And then get it back in the bean using FacesContext
:
您的BackingBean:
Your BackingBean:
public void handleFileUpload(FileUploadEvent event) {
HttpServletRequest request = (HttpServletRequest)
FacesContext.getCurrentInstance().getExternalContext().getRequest();
request.getParameter("vid"); // <= Here you are!
}
更新
如注释中所述,每个迭代将拥有自己的<h:form
和自己的<input type="hidden" name="vid"
.上传文件后,handleFileUpload
会被附带表格的数据触发,因此vid
参数将以正确的#{v.id}
As stated in the comments each iteration will have it's own <h:form
with it's own <input type="hidden" name="vid"
. When a file is uploaded the handleFileUpload
will be fired with the data of the enclosing form, thus the vid
parameter will be sent with the correct #{v.id}
更新2
正如BalusC所说,您最好以这种方式获取vid
参数:
As BalusC commented you should preferably get vid
parameter this way :
public void handleFileUpload(FileUploadEvent event) {
FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get("vid") // <= Here you are!
}
这篇关于如何从< ui:repeat>中的jsf页面获取参数到BackingBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!