0
<p:column headerText="Cancel" width="60">    
  <p:commandLink actionListener="#{userLeaveBean.cancelForLeave}" title="Cancel Request" process="@this" update="leaveDataTable" disabled="false">
    <h:graphicImage url="resources/images/cancel.gif"/>
        <f:attribute name="userId" value="#{employee.name}"/>
        <f:attribute name="leaveId" value="#{leaveDetails.strLeaveId}"/>
</p:commandLink></p:column>

您好我已经编写了这段代码来修改数据表的行值。调用名为'cancelForLeave'的方法修改了数据表的行值明细。方法如下:

public void cancelForLeave(ActionEvent actionEvent){

    String userId = (String)actionEvent.getComponent().getAttributes().get("userId");
    String leaveId = (String)actionEvent.getComponent().getAttributes().get("leaveId");
    int currentYear = Calendar.getInstance().get(Calendar.YEAR);
    ConnectLdap connectLdap = new ConnectLdap();
    UserTotalLeaveInfoBean userTotalLeaveInfoBean = connectLdap.getUserLeaveBean(""+currentYear, userId);

    if(userTotalLeaveInfoBean != null){

        UserGeneralLeaveDetailsBean[] userGeneralLeaveDetailsBean = userTotalLeaveInfoBean.getArrUserGeneralLeaveDetailsBean();
        if(userGeneralLeaveDetailsBean != null){
            for(int i=0;i<userGeneralLeaveDetailsBean.length;i++){

                String beanLeaveId = userGeneralLeaveDetailsBean[i].getStrLeaveId();
                if(leaveId.trim().equalsIgnoreCase(beanLeaveId)){

                    if(userGeneralLeaveDetailsBean[i].getStrLeaveStatus().trim().equalsIgnoreCase(LeaveStatus.LVST_APPLIED)){
                        userGeneralLeaveDetailsBean[i].setStrLeaveStatus(LeaveStatus.LVST_CANCELED);
                    }if(userGeneralLeaveDetailsBean[i].getStrLeaveStatus().trim().equalsIgnoreCase(LeaveStatus.LVST_APPROVED)){
                        userGeneralLeaveDetailsBean[i].setStrLeaveStatus(LeaveStatus.LVST_CANCELREQ);
                    }
                    userTotalLeaveInfoBean.setArrUserGeneralLeaveDetailsBean(userGeneralLeaveDetailsBean);
                    connectLdap.addAppliedLeaveInLdap(userTotalLeaveInfoBean, ""+currentYear, userId);
                    leaveDetails.add(userGeneralLeaveDetailsBean[i]);
                }
            }
        }
    }

}

private List<UserGeneralLeaveDetailsBean> leaveDetails;
//Getter and Setter

现在的问题是,当我单击图像按钮时,它会修改该行的值,但会在数据表中添加一个带有修改值的额外行。但是当我注销然后再次登录时,它显示没有额外的行,并且我尝试修改的行很好地显示了修改后的值。

在尝试更改数据表行的值时,应该怎么做才能防止在运行时添加额外的行?请帮助!

我得到以下日志详细信息:如果我在页面中放置日志:

Monday, June 03, 2013 3:59:55 PM : Initiating ajax request. Monday, June 03, 2013 3:59:55 PM : Form to post applyLeaveForm. Monday, June 03, 2013 3:59:55 PM : URL to post /DemoApplication/applyLeave.xhtml. Monday, June 03, 2013 3:59:55 PM : Post Data:javax.faces.partial.ajax=true&javax.faces.source=applyLeaveForm%3AleaveDataTable%3A0%3Aj_idt90&javax.faces.partial.execute=applyLeaveForm%3AleaveDataTable%3A0%3Aj_idt90&javax.faces.partial.render=applyLeaveForm%3AleaveDataTable&applyLeaveForm%3AleaveDataTable%3A0%3Aj_idt90=applyLeaveForm%3AleaveDataTable%3A0%3Aj_idt90&applyLeaveForm=applyLeaveForm&applyLeaveForm%3AfromDate_input=&applyLeaveForm%3AtoDate_input=&applyLeaveForm%3Aj_idt59=morning&applyLeaveForm%3Aj_idt60=endofday&applyLeaveForm%3AleaveDataTable%3A0%3AeditFrom_input=03-Jun-2013&applyLeaveForm%3AleaveDataTable%3A0%3Aj_idt68=morning&applyLeaveForm%3AleaveDataTable%3A0%3AeditTo_input=04-Jun-2013&applyLeaveForm%3AleaveDataTable%3A0%3Aj_idt72=endofday&javax.faces.ViewState=-3803632482959899224%3A-5634910090194656061 Monday, June 03, 2013 3:59:55 PM : Response received succesfully. Monday, June 03, 2013 3:59:56 PM : DOM is updated. Monday, June 03, 2013 3:59:56 PM : Response completed.

4

1 回答 1

1

您无需将修改后的项目添加回您的列表:

leaveDetails.add(userGeneralLeaveDetailsBean[i]);

您必须删除此行,它应该可以解决您的问题。您正在更新您的源代码之前的行,并且添加仅适用于当前视图,这就是刷新获取正确数据的原因。

此外,您的 bean 必须至少在@ViewScopedor中@SessionScoped

于 2013-06-03T08:10:38.080 回答