我有一个数据表,我无法获得旧的和新的价值。我只能得到新的价值 为什么呢?
适用于ajax事件的代码
<p:ajax event="rowEdit" listener="#{productMapBean.onEdit}"/>
支持bean:
public void onEdit(RowEditEvent event) throws SQLException {
System.out.println("BEGIN:: onEdit");
if (ds == null)
throw new SQLException("Can't get data source");
// get database connection
Connection con = ds.getConnection();
PreparedStatement ps = null;
try {
String myPreparedStatement = preparedStatements.getUpdateStmt();
ps = con.prepareStatement(myPreparedStatement);
ps.setString(1, ((MappingData) event.getObject()).getCode());
ps.setString(2, ((MappingData) event.getObject()).getSubcode());
ps.setString(3, ((MappingData) event.getObject()).getDestination());
ps.setString(4, ((MappingData) event.getObject()).getMapped());
ps.setString(5, mappingDataUpdate.getCode());
ps.setString(6, mappingDataUpdate.getSubcode());
ps.setString(7, mappingDataUpdate.getDestination());
ps.setString(8, mappingDataUpdate.getMapped());
int executeUpdate = ps.executeUpdate();
System.out.println("row updated: " + executeUpdate + " - " + ((MappingData) event.getObject()).getCode() + " - " + ((MappingData) event.getObject()).getSubcode()
+ " - " + ((MappingData) event.getObject()).getDestination() + " - " + ((MappingData) event.getObject()).getMapped());
System.out.println("row updated: " + mappingDataUpdate.getCode() + " - " + mappingDataUpdate.getSubcode() + " - " + mappingDataUpdate.getDestination() + " - "
+ mappingDataUpdate.getMapped());
System.out.println("key : " + mappingDataUpdate.getRowkey());
System.out.println("key2 : " + ((MappingData) event.getObject()).getRowkey());
} catch (Exception e) {
System.out.println(e.getMessage());
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), e.getLocalizedMessage()));
} finally {
ps.close();
con.close();
}
dataList = null;
}
应该获得旧值的代码:
<p:ajax event="rowSelect" listener="#{productMapBean.onRowSelect}"/>
支持bean:
public void onRowSelect(SelectEvent event) {
System.out.println("BEGIN:: onRowSelect");
if(mappingDataUpdate==null){
if (((MappingData) event.getObject()) != null) {
mappingDataUpdate = ((MappingData) event.getObject());
System.out.println("row: " + mappingDataUpdate.getCode() + " - " + mappingDataUpdate.getSubcode() + " - " + mappingDataUpdate.getDestination() + " - "
+ mappingDataUpdate.getMapped());
}
}
}
日志说:
14:58:55,170 INFO [stdout](http- / 0.0.0.0:8080-2)BEGIN :: onRowSelect
14:58:55,170 INFO [stdout](http- / 0.0.0.0:8080-2)row:1 - 1 - DTCC-DFA - q&lt; - 当我选择行
14:58:58,323 INFO [stdout](http- / 0.0.0.0:8080-2)doFilter
14:58:58,340 INFO [stdout](http- / 0.0.0.0:8080-2)BEGIN :: onEdit&lt; - 当我按下单元格编辑器上的复选标记时
14:58:58,342 INFO [stdout](http- / 0.0.0.0:8080-2)行更新:1 - 1 - REGIS-TR - q&lt; - 是同样的原因?
14:58:58,342 INFO [stdout](http- / 0.0.0.0:8080-2)行更新:1 - 1 - REGIS-TR - q&lt; - 是同样的原因?
14:58:58,343 INFO [stdout](http- / 0.0.0.0:8080-2)key:11REGIS-TRq
14:58:58,343 INFO [stdout](http- / 0.0.0.0:8080-2)key2:11REGIS-TRq
14:58:58,359 INFO [stdout](http- / 0.0.0.0:8080-2)doFilter
14:58:58,373 INFO [stdout](http- / 0.0.0.0:8080-2)BEGIN :: onRowSelect&lt; - 在调用onEdit支持bean后,jsf单独调用它
14:58:58,386 INFO [stdout](http- / 0.0.0.0:8080-2)doFilter
问题是为什么变量
mappingDataUpdate
不守值!它被重置为新值???
行更新:1 - 1 - REGIS-TR - q&lt; - 是同一个原因?
行更新:1 - 1 - REGIS-TR - q&lt; - 是同一个原因?
代码1中的指的是
mappingDataUpdate.getCode()
和另一个参考
((MappingData) event.getObject()).getCode()
为什么一样?
答案 0 :(得分:0)
我认为问题是因为mappingDataUpdate是可序列化的对象
因为做:
public void onRowSelect(SelectEvent event) {
if (((MappingData) event.getObject()) != null) {
mappingDataUpdate = ((MappingData) event.getObject());
this.destination = mappingDataUpdate.getDestination();
this.mappedValue = mappingDataUpdate.getMapped();
this.subProduct = mappingDataUpdate.getSubcode();
this.product = mappingDataUpdate.getCode();
System.out.println("row: " + mappingDataUpdate.getCode() + " - " + mappingDataUpdate.getSubcode() + " - " + mappingDataUpdate.getDestination() + " - "
+ mappingDataUpdate.getMapped());
}
}
它有效。