Hi Volker,

thanks for the quick response. I changed my converter to return the id
as you suggested (and I guess I had this state before but got over it
...). This way it works for the tx:selectOneChoice.

But if the object containing the person as a field is displayed in a
table now the person's id is displayed. I can avoid this by adding
special properties to the <tc:out /> tag. Or do you know a better
solution for this?

Regards,

Stefan.

Volker Weber schrieb:
> Hi Stefan,
>
> change the converter to convert from and to id
> and don't use the converter for the label.
>
> e.g.:
>
>
>   List<SelectItem> items = new ArrayList<SelectItem>();
>   for (Person person : list) {
>     StringBuilder builder = new StringBuilder();
>     if (person.getSalutation() != null) {
>       builder.append(person.getSalutation().getName()).append(" ");
>     }
>     if (!StringUtils.isEmpty(person.getFirstname())) {
>       builder.append(person.getFirstname()).append(" ");
>     }
>      String label =  builder.append(person.getLastname())
>         .append("(").append(person.getId()).append(")").toString();
>     items.add(new SelectItem(person, label));
>   }
>
>
> and
>
> public Object getAsObject(FacesContext context, UIComponent component,
> String value) throws ConverterException {
>   if (StringUtils.isEmpty(value)) {
>     return null;
>   }
>   try {
>       int personId = Integer.parseInt(value);
>       return
> ProjectManagementHelper.getProjectManagement().getPerson(personId);
>     }
>   } catch (NumberFormatException e) {
>     LOG.error("", e);
>   }
>   return null;
> }
>
> public String getAsString(FacesContext context, UIComponent component,
> Object obj) throws ConverterException {
>   if (obj == null) {
>     LOG.info("###+++ received null value! +++###");
>     return null;
>   }
>   if (obj instanceof Person) {
>     LOG.info("###+++ received Person instance! +++###");
>     return ((Person) obj).getId();
>   }
>   throw new ConverterException("Expected " + Person.class.getName() +
> ", received " + obj.getClass().getName());
> }
>
>
> Regards,
>  Volker
>
> 2007/1/29, Stefan Hedtfeld <[EMAIL PROTECTED]>:
>> Hi,
>>
>> I'm trying to display some complex values within a tx:selectOneChoice
>> element. What I've managed so far is the following:
>>
>> 1. The tag:
>>
>>           <tx:selectOneChoice
>> value="#{editorController.currentEntry.member}"
>>                  label="#{mainBundle.member}" >
>>             <f:selectItems value="#{editorController.memberItems}" />
>>           </tx:selectOneChoice>
>>
>> 2. The item list (memberItem):
>>
>>     ...
>>     // list is declared as List<Person> list above
>>     ...
>>     List<SelectItem> items = new ArrayList<SelectItem>();
>>     for (Person person : list) {
>>       items.add(new SelectItem(person, converter.getAsString(person)));
>>     }
>>     return items;
>>
>> 3. The converter registered for the Person class (an instance of
>> Converter with an additional convenience method getAsString(Person p) -
>> this method is accessible from a caller through the second interface):
>>
>> public class PersonConverter implements Converter,
>> AsStringConverter<Person> {
>>
>>   private static final Log LOG =
>> LogFactory.getLog(PersonConverter.class);
>>
>>   public Object getAsObject(FacesContext context, UIComponent component,
>> String value) throws ConverterException {
>>     if (StringUtils.isEmpty(value)) {
>>       return null;
>>     }
>>     try {
>>       int posOpen = value.lastIndexOf("(");
>>       int posClose = value.lastIndexOf(")");
>>       if (posOpen >= 0 && posClose > posOpen) {
>>         String substring = value.substring(posOpen + 1, posClose);
>>         LOG.info("#+#+# Substring = \"" + substring + "\"");
>>         int personId = Integer.parseInt(substring);
>>         return
>> ProjectManagementHelper.getProjectManagement().getPerson(personId);
>>       }
>>     } catch (NumberFormatException e) {
>>       LOG.error("", e);
>>     }
>>     return null;
>>   }
>>
>>   public String getAsString(FacesContext context, UIComponent component,
>> Object obj) throws ConverterException {
>>     if (obj == null) {
>>       LOG.info("###+++ received null value! +++###");
>>       return null;
>>     }
>>     if (obj instanceof Person) {
>>       LOG.info("###+++ received Person instance! +++###");
>>       return getAsString((Person) obj);
>>     }
>>     throw new ConverterException("Expected " + Person.class.getName() +
>> ", received " + obj.getClass().getName());
>>   }
>>
>>   public String getAsString(Person person) {
>>     StringBuilder builder = new StringBuilder();
>>     if (person.getSalutation() != null) {
>>       builder.append(person.getSalutation().getName()).append(" ");
>>     }
>>     if (!StringUtils.isEmpty(person.getFirstname())) {
>>       builder.append(person.getFirstname()).append(" ");
>>     }
>>     return builder.append(person.getLastname()).append("
>> (").append(person.getId()).append(")").toString();
>>   }
>>
>> }
>>
>> This at least works, but it is kind of ugly UI as I had to code the
>> object's internal id into the String displayed in the list. So my own
>> entry could be "Mr. Stefan Hedtfeld (2)" - which is not the intended
>> way. I'd like to simply display "Mr. Stefan Hedtfeld".
>>
>> Is there a better way to handle this?
>>
>> Regards,
>>
>> Stefan.
>>
>

Reply via email to