gpt4 book ai didi

jsf-2 - 如何从中的jsf页面获取Backing Bean的参数

转载 作者:行者123 更新时间:2023-12-04 05:03:29 24 4
gpt4 key购买 nike

我的 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>

我的后台 bean :
 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 .我需要知道 id 处理文件上传我怎样才能做到这一点?

最佳答案

我只想把 v.id在隐藏字段中:

你的 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 中:

您的 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} 一起发送

更新 2

正如 BalusC 评论的,你最好得到 vid参数这样:
public void handleFileUpload(FileUploadEvent event) {
FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get("vid") // <= Here you are!
}

关于jsf-2 - 如何从<ui :repeat>中的jsf页面获取Backing Bean的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com