Note that there are some explanatory texts on larger screens.

plurals
  1. POselectOneMenu in dataTable, default value not getting set properly
    text
    copied!<p>When I place a selectOneMenu within a dataTable, it does not display the correct default value in the selectOneMenu. The datatable is bound to a list of POJO's. The POJO entity Badge references a POJO entity we will call Facility. This Facility should be the selected value of the selectOneMenu in the row (the row being each Badge).</p> <p>The following is my simple example of a table:</p> <pre><code>&lt;h:dataTable id="examp" value="#{managedBean.badges}" var="badge"&gt; &lt;h:column rowHeader="rowie"&gt; &lt;h:selectOneMenu value="#{badge.facility}" id="col1"&gt; &lt;f:converter converterId="facilityConverter" /&gt; &lt;f:selectItems value="#{managedBean.facilities}" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:column&gt; &lt;/h:dataTable&gt; </code></pre> <p>The selectItems are a List of SelectItem objects that are created at PostConstruct. These are within my managedbean that is in ViewScope.</p> <pre><code>public class ListBadges extends BaseBean { private List&lt;Badge&gt; badges = new ArrayList&lt;Badge&gt;(); private List&lt;SelectItem&gt; facilities = new ArrayList&lt;SelectItem&gt;(); public ListBadges() { getBadgesFromDatabase(true); } @PostConstruct public void init() { if (facilities.size() &lt;= 0) { try { List&lt;Facility&gt; 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&lt;Badge&gt; getBadges() { return badges; } public void setBadges(List&lt;Badge&gt; badges) { this.badges = badges; } public List&lt;SelectItem&gt; getFacilities() { return facilities; } public void setFacilities(List&lt;SelectItem&gt; facilities) { this.facilities = facilities; } </code></pre> <p>Stepping through the code I confirm that all of the data is correct. In my converter, I verified that the arguments passed to getAsString is correct, so it should have identified the correct item.</p> <pre><code>@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; } } </code></pre> <p>Here is the <code>Facility</code> class which has equals and hashCode implemented:</p> <pre><code>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()); } } </code></pre> <p>I would greatly appreciate any feedback.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload