我在将数据表中的单个值传递给backingbean时遇到了问题。当我试图在确认对话框中的方法中打印它时,我总是得到值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);
}
您需要做的第一个也是最重要的更改是:如Luiggi Mendoza所建议的RequestScoped->ViewScoped
。您可能需要使用p:dataTable
的id
将process
属性添加到p:commandButton
。此外,您可以将lab
对象直接传递到action
方法中,而无需将f:setPropertyActionListener
用作:action="#{coltsysHome.action(lab)}"
,并将其作为:public String action(Lab lab)
。为什么不actionListener
!