在我单击命令链接后,无法完全弄清楚为什么数据表中的所有现有项目都被重置为 new Item()。这个应用程序为用户提供了一个联系人列表以及一个编辑(commandlink)按钮,用户可以单击该按钮将所选项目更改为编辑模式。当我单击命令链接时,它会调用 bean 的 edit() 并且 getRowData() 正在获取预期的行。但是该行中的现有数据在某种程度上是空白的(确切地说是新的 MSAMContactJTO())。事实上,数据表中的所有现有行都显示如何被重置为空白。
下面是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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:c="http://java.sun.com/jstl/core">
<!-- saved from url=(0049)http://usmodev/my_send_a_message/address_book.htm -->
<h:head><title>My Send a Message</title>
</h:head>
<h:body>
<h:form id="addressBookForm" prependId="false">
<h:dataTable id="contactListTable" border="0" cellspacing="0" cellpadding="0"
value="#{MSAMAddressBook2ListBean.contactListTable}"
rendered="#{not empty MSAMAddressBook2ListBean.contactList}"
var="item">
<h:column>
<h:panelGrid id="contactNameColumn" >
<h:panelGroup style="text-align:left; white-space:nowrap;" rendered="#{!item.editMode and !item.newAdd}">
<h:outputText value="#{item.contactName}"/>
</h:panelGroup>
<h:panelGroup style="text-align:left; white-space:nowrap;" rendered="#{item.editMode || item.newAdd}">
<h:inputText id="contactName" value="#{item.contactName}" maxlength="30" size="30" />
</h:panelGroup>
</h:panelGrid>
</h:column>
<h:column>
<h:panelGrid id="actionColumn" cellpadding="0" cellspacing="0">
<h:commandLink id="editContact" action="#{MSAMAddressBook2ListBean.edit}" value="EDIT"/>
<!-- f:ajax event="action" execute="@form" render="contactListTable"/ -->
</h:panelGrid>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
下面是管理bean:
package com.arch.myaccount.jsf;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import com.arch.database.myaccount.MSAMAddressBookDatabase;
import com.arch.myaccount.MSAMContactDTO;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.annotation.PostConstruct;
@SuppressWarnings("serial")
@ManagedBean(name="MSAMAddressBook2ListBean")
@ViewScoped
public class MSAMAddressBook2ListBean extends JSFBean implements Serializable
{
// table list info
private transient DataModel<MSAMContactJTO> contactListTable;
private ArrayList<MSAMContactJTO> contactList = null;
@PostConstruct
public void init() {
getContactList();
}
public String edit() {
String result = "successful";
MSAMContactJTO item = getContactListTable().getRowData();
item.setEditMode(true);
return result;
}
public List<MSAMContactJTO> getContactList() {
if (contactList == null) {
try {
loadContactList();
}
catch (Exception ex) {}
}
return contactList;
}
public void loadContactList() {
try {
List<MSAMContactDTO> contacts = new MSAMAddressBookDatabase().findContactsByLoginName(getLoginID());
if (contactList == null) contactList = new ArrayList<MSAMContactJTO>();
else contactList.clear();
for (MSAMContactDTO contact :contacts) {
MSAMContactJTO abDTO = new MSAMContactJTO();
abDTO.copyFromMSAMContactDTO(contact);
contactList.add(abDTO);
}
}
catch (Exception ex) {
addFatalMessage("We are unable to fulfill your request. Please try again later");
ex.printStackTrace();
}
}
public DataModel<MSAMContactJTO> getContactListTable() {
if (contactListTable == null) {
contactListTable = new ListDataModel<MSAMContactJTO>(contactList);
}
return contactListTable;
}
public void setContactList(ArrayList<MSAMContactJTO> contactList) {
this.contactList = contactList;
}
}