我正在尝试通过单击命令链接对数据表进行 JSF/primefaces ajax 更新,我使用该命令链接删除记录。单击命令链接时,我的数据库会更新,但即使我使用更新数据表也不会刷新="@form" 属性。有关如何解决此问题的任何指导都会非常有帮助。
我的 JSF 代码片段是一个行可编辑数据表,其中包含几个列,如 state、region 如下所示
<h:form id="lpcForm">
<div id="content">
<p:commandLink id="cmdLinkDelete" value="Delete" ajax="true"
action="#{lpcBean.deleteRecords}" update="lpcDataTable" />
<p:dataTable var="lpcData" id="lpcDataTable" widgetVar="lpcTable"
value="#{lpcBean.lpcIdList}" selection="#{lpcBean.selectedRows}"
editable="true" scrollable="true" scrollWidth="1110"
scrollHeight="330" styleClass="datatable" resizableColumns="true">
<p:ajax event="rowEdit" listener="#{lpcBean.onEdit}" />
<p:ajax event="rowEditCancel" listener="#{lpcBean.onCancel}" />
<p:column selectionMode="multiple" style="width:15px" />
<p:column headerText="LPC ID" style="width:20px">
<h:outputText value="#{lpcData.LPCID}" />
</p:column>
<p:column headerText="Region" style="width:40px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.region}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{lpcData.region}">
<f:selectItems value="#{lpcBean.regions}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="State" style="width:15px">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{lpcData.homeState}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{lpcData.homeState}">
<f:selectItems value="#{lpcBean.mainStates}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
.
.
.
<p:column style="width:10px">
<p:rowEditor />
</p:column>
</p:dataTable>
</div>
</h:form>
变量 lpcIdList 是实现DataModel的类的类型。
下面是包含deleteRecords方法的托管 bean 片段。托管 bean属于RequestScoped
public String deleteRecords()
{
System.out.println("inside delete records");
System.out.println("Selected rows length:"+ selectedRows.length);
for(LPC itemSelect:selectedRows)
{
String lpcId=itemSelect.getLPCID();
JdbcConnection jdbcConnection=new JdbcConnection();
connection=jdbcConnection.getJdbcConnection();
try{
preparedStmt=connection.prepareStatement("delete from LPC where LPCID=?");
preparedStmt.setString(1, lpcId);
preparedStmt.execute();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return null;
}
变量selectedRows是数据表的选择属性
getLpcFields 从数据库中获取数据的方法
public void getLpcFields() {
try
{
JdbcConnection jdbcConnection =new JdbcConnection();
connection=jdbcConnection.getJdbcConnection();
System.out.println("connected to the database");
selectStmt1 = connection.createStatement();
ResultSet rs1 = selectStmt1.executeQuery("select * from LPC order by LPCID");
while(rs1.next()){
lpcItems.add(new LPC(rs1.getString("LPCID"),rs1.getString("Name_First"),rs1.getString("Name_Last"),rs1.getString("Email_Address"),rs1.getString("Region"),
rs1.getString("HomeState"),rs1.getString("ProfitCenter"),rs1.getString("Active_LPC"),rs1.getString("Name")));
}
selectStmt2=connection.createStatement();
ResultSet rs2=selectStmt2.executeQuery("Select * from Tbl_state");
while(rs2.next())
{
mainStates.add(rs2.getString("State"));
}
selectStmt3=connection.createStatement();
ResultSet rs3=selectStmt3.executeQuery("Select * from Tbl_Regional_Managers");
while(rs3.next())
{
regions.add(rs3.getString("Location"));
}
selectStmt4=connection.createStatement();
ResultSet rs4=selectStmt4.executeQuery("Select * from YesNo order by YesNo_Key desc");
while(rs4.next())
{
activeLpcs.add(rs4.getString("Description"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}