我在将数据表中的单个值传递给辅助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);
}
答案 0 :(得分:1)
您需要做的第一个也是最重要的变更是:RequestScoped->ViewScoped
,如Luiggi Mendoza所示。您可能需要使用process
的{{1}}将p:commandButton
属性添加到id
。您也可以将p:dataTable
对象直接传递到lab
方法而不使用action
作为:f:setPropertyActionListener
,并将其视为:action="#{coltsysHome.action(lab)}"
。为什么不public String action(Lab lab)
!