【问题标题】:Primefaces datatable cell editing new and old valuesPrimefaces数据表单元格编辑新旧值
【发布时间】:2013-12-05 14:07:23
【问题描述】:

我有一个数据表,我无法获取旧值和新值。我只能得到新值 为什么?

处理 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) 行:1 - 1 - DTCC-DFA - q

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

14:58:58,342 INFO [stdout] (http-/0.0.0.0:8080-2) 行已更新:1 - 1 - REGIS-TR - q

14:58:58,342 INFO [stdout] (http-/0.0.0.0:8080-2) 行已更新:1 - 1 - REGIS-TR - q

14:58:58,343 INFO [stdout] (http-/0.0.0.0:8080-2) 密钥: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

14:58:58,386 INFO [stdout] (http-/0.0.0.0:8080-2) doFilter

问题是为什么变量

  mappingDataUpdate 

不要持有价值!!!并重置为新值???

row updated: 1 - 1 - REGIS-TR - q

row updated: 1 - 1 - REGIS-TR - q

在代码1中参考

 mappingDataUpdate.getCode()

另一个参考

 ((MappingData) event.getObject()).getCode()

为什么是一样的?

【问题讨论】:

  • 你能告诉我我是否正确吗?代码有效,但我不明白为什么有错误。

标签: jsf-2 primefaces


【解决方案1】:

我认为问题是因为 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());
        }

}

它有效。

【讨论】:

  • 你能告诉我我是否正确吗?代码有效,但我不明白为什么有错误。
猜你喜欢
  • 1970-01-01
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2013-05-16
  • 2017-10-23
  • 1970-01-01
  • 2017-07-02
相关资源
最近更新 更多