當我將selectOneMenu放入dataTable中時,它在selectOneMenu中不顯示正確的默認值。數據表綁定到POJO列表。 POJO實體徽章引用一個POJO實體,我們將調用Facility。該工具應該是該行中selectOneMenu的選定值(該行是每個徽章)。selectOneMenu在dataTable中,默認值沒有正確設置
以下是我的一個表的簡單例子:
<h:dataTable id="examp" value="#{managedBean.badges}" var="badge">
<h:column rowHeader="rowie">
<h:selectOneMenu value="#{badge.facility}" id="col1">
<f:converter converterId="facilityConverter" />
<f:selectItems value="#{managedBean.facilities}"
/>
</h:selectOneMenu>
</h:column>
</h:dataTable>
的selectItems的是在PostConstruct創建的SelectItem對象的名單。這些位於ViewScope中的managedbean中。
public class ListBadges extends BaseBean {
private List<Badge> badges = new ArrayList<Badge>();
private List<SelectItem> facilities = new ArrayList<SelectItem>();
public ListBadges() {
getBadgesFromDatabase(true);
}
@PostConstruct
public void init() {
if (facilities.size() <= 0) {
try {
List<Facility> facilityBeans = FacilityHelper.getFacilities();
for (Facility fac : facilityBeans) {
facilities.add(new SelectItem(fac, fac.getFacilityName()));
}
} catch (tException e) {
log.error("ListBadges.init(): " + e.getMessage());
e.printStackTrace();
}
}
}
public void getBadgesFromDatabase(boolean forceRefresh) {
if (forceRefresh || badges == null || badges.isEmpty())
badges = BadgeHelper.getBadgeList();
}
///
/// Bean Properties
///
public List<Badge> getBadges() {
return badges;
}
public void setBadges(List<Badge> badges) {
this.badges = badges;
}
public List<SelectItem> getFacilities() {
return facilities;
}
public void setFacilities(List<SelectItem> facilities) {
this.facilities = facilities;
}
單步執行代碼我確認所有的數據都是正確的。在我的轉換器中,我驗證了傳遞給getAsString的參數是正確的,所以它應該已經識別出正確的項目。
@FacesConverter("facilityConverter")
public class FacilityConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String from) {
try {
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
ListBadges neededBean =
(ListBadges) context.getApplication().getELResolver().getValue(elContext, null, "managedBean");
long id = Long.parseLong(from);
for (SelectItem sItem : neededBean.getFacilities()) {
Facility facility = (Facility)sItem.getValue();
if (facility.getFacilityId() == id)
return facility;
}
} catch (Exception e) {
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
try {
Facility facility = (Facility)value;
return facility.getFacilityId() + "";
} catch (Exception e) {
}
return null;
}
}
這裏是Facility
類具有equals和hashCode實現:
public class Facility implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long facilityId;
private String facilityName;
private String address1;
private String address2;
private String city;
private String state;
private String postalCode;
private String url;
private String phone;
private String siteManager;
public Facility() {
}
public Facility(String facilityName) {
this.facilityName = facilityName;
}
public Facility(String facilityName,
String address1, String address2, String city, String state,
String postalCode, String url, String phone, String siteManager) {
this.facilityName = facilityName;
this.address1 = address1;
this.address2 = address2;
this.city = city;
this.state = state;
this.postalCode = postalCode;
this.url = url;
this.phone = phone;
this.siteManager = siteManager;
}
public long getFacilityId() {
return this.facilityId;
}
public void setFacilityId(long facilityId) {
this.facilityId = facilityId;
}
public String getFacilityName() {
return this.facilityName;
}
public void setFacilityName(String facilityName) {
this.facilityName = facilityName;
}
public String getAddress1() {
return this.address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
public String getAddress2() {
return this.address2;
}
public void setAddress2(String address2) {
this.address2 = address2;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getPostalCode() {
return this.postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getUrl() {
return this.url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSiteManager() {
return siteManager;
}
public void setSiteManager(String siteManager) {
this.siteManager = siteManager;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Facility) || (o == null))
return false;
if (o == this)
return true;
Facility obj = (Facility)o;
return obj.getFacilityId() == this.getFacilityId();
}
@Override
public int hashCode() {
return (new Long(this.getFacilityId()).hashCode())^
((this.getAddress1() == null) ? 0 : this.getAddress1().hashCode())^
((this.getAddress2() == null) ? 0 : this.getAddress2().hashCode())^
((this.getCity() == null) ? 0 : this.getCity().hashCode())^
((this.getFacilityName() == null) ? 0 : this.getFacilityName().hashCode())^
((this.getPhone() == null) ? 0 : this.getPhone().hashCode())^
((this.getPostalCode() == null) ? 0 : this.getPostalCode().hashCode())^
((this.getSiteManager() == null) ? 0 : this.getSiteManager().hashCode())^
((this.getUrl() == null) ? 0 : this.getUrl().hashCode());
}
}
我將不勝感激任何反饋。
未來,請特別注意您對論壇框架/ API提出的爭議態度:)這會嚴重激發其他人發佈答案。我已經掃過了那一個。不過,如果您不同意,請隨時回滾。 – BalusC 2011-03-29 13:40:03
我道歉BalusC。你是正確的從我的帖子中清除不必要的部分。我對我的個人情況感到非常沮喪,並且處於緊張的期限之內。我不是故意批評莫哈拉。 – 2011-03-29 14:23:11