gpt4 book ai didi

java - 使用确认对话框将值从数据表传递到 bean JSF

转载 作者:太空宇宙 更新时间:2023-11-04 07:45:37 24 4
gpt4 key购买 nike

我在将单个值从数据表传递到支持 bean 时遇到问题。当我尝试在确认对话框的方法中打印它时,我总是得到 0 的值,但是当我尝试在命令按钮的方法中打印它时,它显示我需要的值。我认为它会重置值或其他什么。

<p:dataTable id="labLists" var="lab" value="#{coltsysHome.laboratory}" >
.....
<p:column headerText=" ">
<p:commandButton value="DELETE" onclick="confDlg.show()" icon="ui-icon-closethick" action="#{coltsysHome.action}">
<f:setPropertyActionListener value="#{lab.lab_id}" target="#{coltsysHome.lab_id_del}" />
</p:commandButton>
</p:column>

对于确认对话框:

<p:confirmDialog widgetVar="confDlg" header="DELETE LABORATORY" message="Are you sure you want to delete this lab?">
<h:form id="delDlgForm">
<p:commandButton id="confirm" value="Yes Sure" oncomplete="confDlg.hide()" actionListener="#{coltsysHome.deleteLab(event)}"/>
<p:commandButton id="decline" value="Not Yet" onclick="confDlg.hide()" type="button" />

</h:form>

Bean(RequestScoped):

...getter and setter (lab_id_del)

public void deleteLab(ActionEvent event) {

FacesContext context = FacesContext.getCurrentInstance();
String cpath = context.getExternalContext().getRequestContextPath();

try (Connection conn = dataSource.getConnection()) {
ColtsysDAO coltsysDAO = new ColtsysDAO(conn);
coltsysDAO.deleteLab(lab_id_del, coltsysDAO.getUserID(getUser_name()));

} catch (Exception e) {
e.printStackTrace(System.err);
}
}
public void action() {
System.out.println("lab_id_del: " + lab_id_del);
}

最佳答案

您需要做的第一个也是最重要的更改是:RequestScoped->ViewScoped as Luiggi Mendoza已建议。您可能需要使用 p:dataTableidprocess 属性添加到 p:commandButton。您还可以将 lab 对象直接传递到 action 方法中,而无需使用 f:setPropertyActionListener,如:action="#{coltsysHome.action(lab)}" 并将其视为:public String action(Lab lab)。为什么不actionListener!

关于java - 使用确认对话框将值从数据表传递到 bean JSF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15329510/

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