[jboss-user] [JBoss Seam] - Re: Drop Down Lists using Facelets?
Jark
do-not-reply at jboss.com
Sun Apr 8 15:23:30 EDT 2007
Hi,
i have done something similair in a schoolproject of mine. In this project we had a user who was a member of a certain organisation. On the useredit page you had to be able to change the organisation of the user by using an dropdown list. The big problem was to let it see a name rather than an organisation reference it self, we used an converter for this as described by quilleashm.
First the dropdownlist in the edit page:
| <h:selectOneMenu value="#{userHome.instance.orgLevel}"
| converter="#{orgbean.converter}">
| <f:selectItems value="#{orgbean.orgLevels}" />
| </h:selectOneMenu>
Second the converter:
| import java.io.Serializable;
| import java.util.List;
| import java.util.Map;
| import java.util.TreeMap;
|
| import javax.ejb.Remove;
| import javax.ejb.Stateful;
| import javax.faces.component.UIComponent;
| import javax.faces.context.FacesContext;
| import javax.faces.convert.Converter;
| import javax.faces.convert.ConverterException;
| import javax.persistence.EntityManager;
| import javax.persistence.PersistenceContext;
| import org.jboss.seam.ScopeType;
| import org.jboss.seam.annotations.Create;
| import org.jboss.seam.annotations.Destroy;
| import org.jboss.seam.annotations.Name;
| import org.jboss.seam.annotations.Scope;
|
|
| @Stateful
| @Name("orgbean")
| @Scope(ScopeType.EVENT)
| public class OrgLevelsBean implements OrgLevels, Serializable
| {
| List<OrgLevel> orgLevels;
| Map<String,OrgLevel> orgLevelMap;
|
| @PersistenceContext
| EntityManager em;
|
| @Create
| public void loadData() {
| orgLevels = em.createQuery("select o from OrgLevel o")
| .setHint("org.hibernate.cacheable", true)
| .getResultList();
|
| Map<String,OrgLevel> results = new TreeMap<String,OrgLevel>();
|
| for (OrgLevel orgLevel: orgLevels) {
| results.put(orgLevel.getName(),orgLevel);
| }
|
| orgLevelMap = results;
|
| }
| public Map<String, OrgLevel> getOrgLevels() {
| return orgLevelMap;
| }
|
| public Converter getConverter() {
| return new OrgLevelConverter(orgLevels);
| }
|
| public OrgLevel getNullOrgLevel() {
|
| return new OrgLevel();
| }
|
| static public class OrgLevelConverter implements Converter,Serializable
| {
| List<OrgLevel> orgLevels;
|
| public OrgLevelConverter(List<OrgLevel> orgLevels) {
| this.orgLevels = orgLevels;
| }
|
| public String getAsString(FacesContext facesContext,
| UIComponent component,
| Object obj)
| {
| if (obj == null) return null;
| OrgLevel orgLevel = (OrgLevel) obj;
|
| String val = String.valueOf(orgLevel.getId());
| return val;
| }
|
| public Object getAsObject(FacesContext facesContext,
| UIComponent component,
| String str)
| throws ConverterException
| {
| if (str == null || str.length()==0) {
| return null;
| }
|
| int id = Integer.valueOf(str).intValue();
| for (OrgLevel orgLevel : orgLevels) {
| if (orgLevel.getId() == id) {
| return orgLevel;
| }
| }
|
| return null;
| }
| }
| @Remove @Destroy
| public void destroy() {}
|
|
| }
|
I think we largely took this from an example included in the Seam package but i'm not sure. Hope this helps :)
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4035613#4035613
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4035613
More information about the jboss-user
mailing list