Jsf prime faces中的onRowEdit(RowEditEvent事件)未获取更新值

Jsf prime faces中的onRowEdit(RowEditEvent事件)未获取更新值,jsf,primefaces,datatable,roweditor,Jsf,Primefaces,Datatable,Roweditor,我试图使用jsf和primefaces来更新行,这里是我对每个文件的代码,但是当我调试时,我没有得到更新的值,它每次都会给我一个旧值,并将其保持为旧值 用户类型.xhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1

我试图使用jsf和primefaces来更新行,这里是我对每个文件的代码,但是当我调试时,我没有得到更新的值,它每次都会给我一个旧值,并将其保持为旧值

用户类型.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body style="align:center;">

    <div style="width: 100%">
        <h:form id="form">
            <div style="float: left; width: 54%;">
                <p:growl id="msgs" showDetail="true" />
                <p:dataTable id="userTypesTbl" var="varUserType"
                    value="#{userTypeTabMenuBean.userTypeMasters}" editable="true"
                    style="margin-bottom:20px" rowIndexVar="rowIndex">
                    <f:facet name="header">User Type Managging</f:facet>

                    <p:ajax event="rowEdit" listener="#{userTypeTabMenuBean.onRowEdit}"
                        update=":form:msgs" />
                    <p:ajax event="rowEditCancel"
                        listener="#{userTypeTabMenuBean.onRowCancel}" update=":form:msgs" />

                    <p:column headerText="Sr.">
                        #{rowIndex+1}.
                    </p:column>

                    <p:column headerText="Type">
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{varUserType.type}" />
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText value="#{varUserType.type}" style="width:100%"
                                    label="type" />
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column>
                        <f:facet name="header">View</f:facet>
                        <p:commandButton update=":form" oncomplete="userTypeDialog.show()"
                            icon="ui-icon-search" title="View">
                            <f:setPropertyActionListener value="#{userType}"
                                var="#{selectedUserType}"
                                target="#{userTypeTabMenuBean.selectedUserType}" />
                        </p:commandButton>
                    </p:column>

                    <p:column style="width:32px;">
                        <f:facet name="header">Edit</f:facet>
                        <p:rowEditor />
                    </p:column>

                    <p:column style="width:32px;align-items: center;">
                        <f:facet name="header">Delete</f:facet>
                        <h:commandLink
                            action="#{userTypeTabMenuBean.deleteAction(varUserType)}">
                            <h:graphicImage library="images" name="delete.png" height="45%"
                                width="50%" />
                        </h:commandLink>
                    </p:column>
                </p:dataTable>
                <ui:debug hotkey="x" />

                <p:dialog header="User type detail" widgetVar="userTypeDialog"
                    resizable="false" width="330" showEffect="explode"
                    hideEffect="explode">
                    <h:panelGrid id="display" columns="2" cellpadding="4">
                        <h:outputText value="ID : " />
                        <h:outputText value="#{userTypeTabMenuBean.selectedUserType.id}" />
                        <h:outputText value="TYPE: " />
                        <h:outputText value="#{userTypeTabMenuBean.selectedUserType.type}" />
                    </h:panelGrid>
                </p:dialog>
            </div>
            <div style="float: right; width: 44%;">
                <p:panel menuTitle="Add User Type." header="Add User Type.">
                    <h:outputText value="User type *:" />
                    <p:inputText value="#{userTypeTabMenuBean.userType}"
                        title="Enter User Type.." id="myUserType"
                        style="margin:0px 10px 0px 10px;"></p:inputText>
                    <p:commandButton value="Add Type"
                        action="#{userTypeTabMenuBean.saveAction}" ajax="false">
                    </p:commandButton>
                </p:panel>
            </div>
        </h:form>
    </div>
</h:body>
</html>
UserTypeMasterDAO.java

package com.convoy.gpack.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.RowEditEvent;

import com.convoy.gpack.dao.UserTypeMasterDAO;
import com.convoy.gpack.pack.models.UserTypeMaster;

@ManagedBean(name = "userTypeTabMenuBean")
@SessionScoped
public class UserTypeTabMenuBean implements Serializable {
private static final long serialVersionUID = 1467465633405172689L;
private static final Logger logger = Logger
        .getLogger(UserTypeTabMenuBean.class);
private List<UserTypeMaster> userTypeMasters;
private UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
private UserTypeMaster selectedUserType;
private String userType;

public List<UserTypeMaster> getUserTypeMasters() {
    userTypeMasters = userTypeMasterDAO.getAllUserTypes();
    logger.info("getUserTypeMasters=" + userTypeMasters);
    return userTypeMasters;
}

public String deleteAction(UserTypeMaster userTypeMaster) {
    logger.info("Deleting the object with id = " + userTypeMaster.getId());
    UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    int result = userTypeMasterDAO.deleteUserType(userTypeMaster);
    userTypeMasters.remove(userTypeMaster);
    if (result == 1) {
        FacesMessage msg = new FacesMessage(
                "User type deleted successfuly.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } else {
        FacesMessage msg = new FacesMessage("Failed to delete user types.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    return null;
}

public void onRowEdit(RowEditEvent event) {
    UserTypeMaster userTypeMaster = (UserTypeMaster) event.getObject();
    logger.info("UPDATING THE USER TYPE MASTER : "
            + userTypeMaster.getType());
    UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    userTypeMasterDAO.saveOrUpdateUserTypeMaster(userTypeMaster);
    logger.info("UPDATING THE USER TYPE MASTER : "
            + userTypeMaster.getType());

    FacesMessage msg = new FacesMessage("Edited type ",
            ((UserTypeMaster) event.getObject()).getType() + "");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public void onRowCancel(RowEditEvent event) {
    FacesMessage msg = new FacesMessage("Editing Cancelled for ",
            ((UserTypeMaster) event.getObject()).getType() + "");
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

public UserTypeMaster getSelectedUserType() {
    return selectedUserType;
}

public void setSelectedUserType(UserTypeMaster selectedUserType) {
    this.selectedUserType = selectedUserType;
}

public String getUserType() {
    return userType;
}

public void setUserType(String userType) {
    this.userType = userType;
}

public void saveAction() {
    logger.info("Saving the object to database...." + userType);
    if (userType.trim().length() < 1) {
        FacesMessage msg = new FacesMessage("Can not save empty user type.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    UserTypeMaster userTypeMaster = new UserTypeMaster();
    userTypeMaster.setType(userType);
    int result = userTypeMasterDAO
            .saveOrUpdateUserTypeMaster(userTypeMaster);
    if (result == 1) {
        FacesMessage msg = new FacesMessage("User type saved.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    } else {
        FacesMessage msg = new FacesMessage("Failed to save user type.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}
}
package com.convoy.gpack.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.convoy.gpack.hibernate.util.HibernateUtil;
import com.convoy.gpack.pack.models.UserTypeMaster;

public class UserTypeMasterDAO implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -4005898944892506760L;

public UserTypeMaster getUserTypeById(long long1) {

    return null;
}

public List<String> getAllUserTypesInString() {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(UserTypeMaster.class);
        @SuppressWarnings("unchecked")
        List<UserTypeMaster> userTypeMasters = criteria.list();
        List<String> userTypeMastersString = new ArrayList<String>();
        for (UserTypeMaster userTypeMaster : userTypeMasters) {
            userTypeMastersString.add(userTypeMaster.getType());
        }
        transaction.commit();
        session.close();
        return userTypeMastersString;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public List<UserTypeMaster> getAllUserTypes() {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(UserTypeMaster.class);
        @SuppressWarnings("unchecked")
        List<UserTypeMaster> userTypeMasters = criteria.list();
        transaction.commit();
        session.close();
        return userTypeMasters;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public int saveOrUpdateUserTypeMaster(UserTypeMaster userTypeMaster) {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        userTypeMaster.setType(userTypeMaster.getType().toUpperCase());
        session.saveOrUpdate(userTypeMaster);
        transaction.commit();
        session.close();
        return 1;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }

}

public int deleteUserType(UserTypeMaster userTypeMaster) {
    try {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(userTypeMaster);
        transaction.commit();
        session.close();
        return 1;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}
}
package com.corfay.gpack.dao;
导入java.io.Serializable;
导入java.util.ArrayList;
导入java.util.List;
导入org.hibernate.Criteria;
导入org.hibernate.Session;
导入org.hibernate.Transaction;
导入com.corfay.gpack.hibernate.util.HibernateUtil;
导入com.corfay.gpack.pack.models.UserTypeMaster;
公共类UserTypeMasterDAO实现可序列化{
/**
* 
*/
私有静态最终长serialVersionUID=-4005898944892506760L;
公共UserTypeMaster getUserTypeById(长1){
返回null;
}
公共列表getAllUserTypesInString(){
试一试{
Session Session=HibernateUtil.getSessionFactory().openSession();
事务=会话。beginTransaction();
条件=session.createCriteria(UserTypeMaster.class);
@抑制警告(“未选中”)
List userTypeMasters=criteria.List();
List userTypeMastersString=new ArrayList();
用于(UserTypeMaster UserTypeMaster:userTypeMasters){
userTypeMastersString.add(userTypeMaster.getType());
}
commit();
session.close();
返回userTypeMastersString;
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
公共列表getAllUserTypes(){
试一试{
Session Session=HibernateUtil.getSessionFactory().openSession();
事务=会话。beginTransaction();
条件=session.createCriteria(UserTypeMaster.class);
@抑制警告(“未选中”)
List userTypeMasters=criteria.List();
commit();
session.close();
返回用户打字机;
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
public int saveOrUpdateUserTypeMaster(UserTypeMaster UserTypeMaster){
试一试{
Session Session=HibernateUtil.getSessionFactory().openSession();
事务=会话。beginTransaction();
setType(userTypeMaster.getType().toUpperCase());
session.saveOrUpdate(userTypeMaster);
commit();
session.close();
返回1;
}捕获(例外e){
e、 printStackTrace();
返回-1;
}
}
public int deleteUserType(UserTypeMaster UserTypeMaster){
试一试{
Session Session=HibernateUtil.getSessionFactory().openSession();
事务=会话。beginTransaction();
删除(userTypeMaster);
commit();
session.close();
返回1;
}捕获(例外e){
e、 printStackTrace();
返回-1;
}
}
}

提前感谢~:)

我想可能是因为数据库值列表的缘故

#{userTypeTabMenuBean.userTypeMasters}
这总是从数据库中检索,这就是getObject()返回旧值(实际上是数据库中的值)的原因

更新getUserTypeMasters()方法,如下所示

if (userTypeMasters == null){

        userTypeMasters = userTypeMasterDAO.getAllUserTypes();

}

return userTypeMasters ;

我想这可能是因为数据库值列表

#{userTypeTabMenuBean.userTypeMasters}
这总是从数据库中检索,这就是getObject()返回旧值(实际上是数据库中的值)的原因

更新getUserTypeMasters()方法,如下所示

if (userTypeMasters == null){

        userTypeMasters = userTypeMasterDAO.getAllUserTypes();

}

return userTypeMasters ;

Primefaces遵循严格的结构,我认为必须添加带有@PostConstruct注释的init()方法

@PostConstruct
public void init(){
    userTypeMasters = userTypeMasterDAO.getAllUserTypes();
}
这是解决办法

package com.convoy.gpack.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.RowEditEvent;

import com.convoy.gpack.dao.UserTypeMasterDAO;
import com.convoy.gpack.pack.models.UserTypeMaster;

@ManagedBean(name = "userTypeTabMenuBean")
@SessionScoped
public class UserTypeTabMenuBean implements Serializable {
    private static final long serialVersionUID = 1467465633405172689L;
    private static final Logger logger = Logger
            .getLogger(UserTypeTabMenuBean.class);
    private List<UserTypeMaster> userTypeMasters;
    private UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
    private UserTypeMaster selectedUserType;
    private String userType;

// This is the required method to get the datatable list.
    @PostConstruct
    public void init() {
        userTypeMasters = userTypeMasterDAO.getAllUserTypes();
        logger.info("getUserTypeMasters=" + userTypeMasters);
    }

    public List<UserTypeMaster> getUserTypeMasters() {
        return userTypeMasters;
    }

    public String deleteAction(UserTypeMaster userTypeMaster) {
        logger.info("Deleting the object with id = " + userTypeMaster.getId());
        UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
        int result = userTypeMasterDAO.deleteUserType(userTypeMaster);
        userTypeMasters.remove(userTypeMaster);
        if (result == 1) {
            FacesMessage msg = new FacesMessage(
                    "User type deleted successfuly.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            FacesMessage msg = new FacesMessage("Failed to delete user types.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        return null;
    }

    public void onRowEdit(RowEditEvent event) {
        UserTypeMaster userTypeMaster = (UserTypeMaster) event.getObject();
        logger.info("UPDATING THE USER TYPE MASTER : "
                + userTypeMaster.getType());
        UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
        userTypeMasterDAO.saveOrUpdateUserTypeMaster(userTypeMaster);
        logger.info("UPDATING THE USER TYPE MASTER : "
                + userTypeMaster.getType());

        FacesMessage msg = new FacesMessage("Edited type ",
                ((UserTypeMaster) event.getObject()).getType() + "");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Editing Cancelled for ",
                ((UserTypeMaster) event.getObject()).getType() + "");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public UserTypeMaster getSelectedUserType() {
        return selectedUserType;
    }

    public void setSelectedUserType(UserTypeMaster selectedUserType) {
        this.selectedUserType = selectedUserType;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public void saveAction() {
        logger.info("Saving the object to database...." + userType);
        if (userType.trim().length() < 1) {
            FacesMessage msg = new FacesMessage("Can not save empty user type.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
        UserTypeMasterDAO userTypeMasterDAO = new UserTypeMasterDAO();
        UserTypeMaster userTypeMaster = new UserTypeMaster();
        userTypeMaster.setType(userType);
        int result = userTypeMasterDAO
                .saveOrUpdateUserTypeMaster(userTypeMaster);
        if (result == 1) {
            FacesMessage msg = new FacesMessage("User type saved.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            init();
        } else if (result == -2) {
            FacesMessage msg = new FacesMessage("User type already exist..");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } else {
            FacesMessage msg = new FacesMessage("Failed to save user type.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}
package com.corfay.gpack.managedbean;
导入java.io.Serializable;
导入java.util.List;
导入javax.annotation.PostConstruct;
导入javax.faces.application.FacesMessage;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.SessionScoped;
导入javax.faces.context.FacesContext;
导入org.apache.log4j.Logger;
导入org.primefaces.event.RowEditEvent;
导入com.corfay.gpack.dao.UserTypeMasterDAO;
导入com.corfay.gpack.pack.models.UserTypeMaster;
@ManagedBean(name=“userTypeTabMenuBean”)
@会议范围
公共类UserTypeTabMenuBean实现可序列化{
私有静态最终长serialVersionUID=1467465633405172689L;
专用静态最终记录器=记录器
.getLogger(UserTypeTabMenuBean.class);
私人列表用户打字机;
私有UserTypeMasterDAO UserTypeMasterDAO=新的UserTypeMasterDAO();
private UserTypeMaster selectedUserType;
私有字符串用户类型;
//这是获取datatable列表所需的方法。
@施工后
公共void init(){
userTypeMasters=userTypeMasterDAO.getAllUserTypes();
logger.info(“getUserTypeMasters=“+userTypeMasters”);
}
公共列表getUserTypeMasters(){
返回用户打字机;
}
公共字符串删除操作(UserTypeMaster UserTypeMaster){
logger.info(“删除id=“+userTypeMaster.getId())的对象);
UserTypeMasterDAO UserTypeMasterDAO=新的UserTypeMasterDAO();
int result=userTypeMasterDAO.deleteUserType(userTypeMaster);
移除(userTypeMaster);
如果(结果==1){
FacesMessage msg=新的FacesMessage(
“用户类型已成功删除。”);
FacesContext.getCurrentInstance().addMessage(null,msg);
}否则{
FacesMessage msg=新的FacesMessage(“未能删除用户类型”);
FacesContext.getCurrentInstance().addMessage(null,msg);
}
返回null;
}
公共无效onRowEdit(RowEditEvent事件){
UserTypeMaster UserTypeMaster=(UserTypeMaster)事件。getObject();
logger.info(“更新用户类型主控码:”
+userTypeMaster.getType());
UserTypeMasterDAO UserTypeMasterDAO=new