In JSF, the idea is that you don't worry about how the select items are stored. You use a converter to change it from your database object to any arbitrary string representation (you pick), and then use same converter algorithm to change it back from the string representation to a database object. Some people use the primary key as the string representation, but that often leaks information and causes security issues.
I typically use the list index value. It does mean that you have to preserve the ordered list between the initial display and the parsing of the response, but that's not been an issue for me. On Mon, May 4, 2009 at 12:02 PM, Mike Quentel <[email protected]> wrote: > Many thanks to Omar for his advice…now a related question: > > > > Using javax.faces.model.SelectItem objects stored in an ArrayList is what > seems to work in terms of appearance, but I was hoping that each resulting > option tag would hold the same text for the value as well as the label. How > do I do this? > > > > My JSF looks like: > > > > <tr:selectOneListbox required="false" label="Users" > id="userRoleSelectOneListbox"> > > <f:selectItems value="#{userRoleBean.options}"/> > > </tr:selectOneListbox> > > > > My bean has: > > > > protected ArrayList<SelectItem> options = null; > > … > > public ArrayList<SelectItem> getOptions() { > > if (this.options == null) { > > this.options = new ArrayList<SelectItem>(); > > for (int i = 0; i < userRoles.size(); i++) { > > SelectItem si = new SelectItem(); > > String userName = userRoles.get(i).getUserName(); > > si.setLabel(new String(userName)); > > si.setValue(new String(userName)); > > this.options.add(si); > > } > > } > > return options; > > } > > > > The resulting output in HTML looks like: > > > > <option value="0">bob</option> > > > > <option value="1">tom</option> > > > > <option value="2">mary</option> > > > > <option value="3">paul</option> > > > > <option value="4">danielle</option> > > > > I was hoping to have the value attributes match the labels, so that it looks > like: > > > > <option value="bob">bob</option> > > > > <option value="tom">tom</option> > > > > <option value="mary">mary</option> > > > > <option value="paul">paul</option> > > > > <option value="danielle">danielle</option> > > > > Otherwise, how am I supposed to get the right value to be submitted in the > form? > > > > Sorry if this seems rather elementary as in “RTFM”…if you happen to know a > tutorial that shows dynamically populating a selection list, then submitting > the values to update a database table, I’d be very interested. Seems like > every example is about having a simple statically created selection list. > > > > Many thanks, > > > > Mike Quentel > > > > From: Omar E > Sent: 28 April 2009 19:46 > To: MyFaces Discussion > Subject: Re: dynamically populating a drop-down selection list with values > from a database > > > > > > In order to populate a selectOneListBox or selectOneChoice from a > database use: > > > > <tr:selectOneListbox value="#{bean.aValue}" required="yes"> > > <f:selectItems value="#{myBacking.selectList}" > > </tr:selectOneListbox> > > > > selectList should be a collection of type SelectItem. > > > > then your backing bean selectList should have a getter and a setter. > > > > private List<SelectItem> selectList; > > { > > //TODO: instanciate the list and populate the list > > } > > > > public List<SelectItem> getSelectList() { > > return selectList; > > } > > > > public void setSelectList(List<SelectItem> selectList) { > > this.selectList = selectList; > > } > > > > the difference between selectOneListBox and selectOneChoice that > selectOneChoice is a drop down list and selectOneListBox is a list box. take > a look at the components guide demo. > > > > http://www.irian.at/trinidad-demo/faces/components/selectOneChoice.jspx > > > > http://www.irian.at/trinidad-demo/faces/components/selectOneListbox.jspx > > > > --Omar > > > > > > On Apr 28, 2009, at 7:01 PM, Mike Quentel wrote: > > Please, would someone direct me to an example of populating a drop-down > selection list using values from a database table query? Most examples show > hard-coded values. I'm hoping to find an example (could be another library > than Trinidad; like Tomahawk or ADF) that includes the JSP and bean. > > I'm using Trinidad. Not clear what the difference is between a > selectOneListbox and a selectOneChoice. > > http://myfaces.apache.org/trinidad/trinidad-api/tagdoc/tr_selectOneListbox.h > tml > > http://myfaces.apache.org/trinidad/trinidad-api/tagdoc/tr_selectOneChoice.ht > ml > > They both seem the same. > > If I use either one of the above, does my bean need to use > CoreSelectOneListBox in order to populate the drop-down list? > > http://myfaces.apache.org/trinidad/trinidad-api/apidocs/org/apache/myfaces/t > rinidad/component/core/input/CoreSelectOneListbox.html > > Where I'm most confused is the proper syntax in the JSP...just need a good > example. > > Many thanks, > > Mike Quentel > > >

