1

I want to pick a custom object from select one menu. It neither shows an error nor values. What should I do?

My xhtml document:

<h:panelGrid columns="2">
    <p:outputLabel value="" />
        <p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currency}" >
            <f:selectItem itemLabel="-- Select Currency--" itemValue="#{null}"/>
        <f:selectItems value="#{CurrencyMB.currencyList}" var="currency"  itemValue="#{currency.currencyId}" itemLabel="#{currency.currencyName}" > 
            </f:selectItems>
        <p:ajax update="currencyOut" />
        </p:selectOneMenu>
        <p:outputLabel value="Currency Id : #{CurrencyMB.currency.currencyId}" id="currencyOut" />
</h:panelGrid>

My managedBean class:

@ManagedBean(name = "CurrencyMB")
@RequestScoped
public class CurrencyManagedBean {

private Currency currency;
private List<Currency> currencyList;


public Currency getCurrency() {
        return currency;
    }

public void setCurrency(Currency currency) {
        this.currency = currency;
    }
public List<Currency> getCurrencyList() {
        currencyList = new ArrayList<Currency>();
        currencyList.addAll(getiCurrencyService().getCurrencies());
        return currencyList;
    }

public void setCurrencyList(List<Currency> currencyList) {
        this.currencyList = currencyList;
    }

}
Umair
  • 860
  • 2
  • 13
  • 30

2 Answers2

8

You are trying to map a Java object of class Currency to a string that comes as a HTTP request parameter. A converter is intended to be used in a situation when you need to create an object from a its string representation, and vice versa, like in the situation you faced.

Basically there are two approaches.

1. Utilize converter.

With this approach you define item value as a Currency object and use a converter to create string representation from an object and recreate an object back from a string. For the converter part, just follow the tutorial Luiggi pointed at. Basically you need to create a class that implements Converter, annotate it with @FacesConverter("currencyConverter") to be able to refer to the converter by id, like in converter="currencyConverter" attribute of a JSF tag:

<p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currency}" converter="currencyConverter">
    <f:selectItems value="#{CurrencyMB.currencyList}" var="currency"  itemValue="#{currency}" itemLabel="#{currency.currencyName}" /> 
    <p:ajax update="currencyOut" />
</p:selectOneMenu>

2. Utilize plain Strings (or java primitive wrappers).

With this approach you bind item values, as well as user selection to a bean property of String type, and not to an actual object. Using it this way you won't need any converter, and string values will be set for you:

<p:selectOneMenu id="CurrencyMenu" value="#{CurrencyMB.currencyName}">
    <f:selectItems value="#{CurrencyMB.currencyList}" var="currency"  itemValue="#{currency.currencyName}" itemLabel="#{currency.currencyName}" /> 
    <p:ajax update="currencyOut" />
</p:selectOneMenu>

Finally, it is worth reading the question to the answer Why selectOneMenu Send ItemLabel to the converter?.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • i have made converter. now i'm seeing null pointer exception. :( – Umair Mar 31 '13 at 20:21
  • i have made a converter. now i'm seeing null pointer exception. [link](http://stackoverflow.com/questions/15733862/pick-custom-object-from-select-one-menu-jsf-converter-exception) – Umair Mar 31 '13 at 20:22
  • @Umair from your duplicate content, looks like the managed bean injection is failing. Are you sure your `ICurrencyService` is a managed bean or is it an EJB? – Luiggi Mendoza Mar 31 '13 at 21:09
  • yes i'm using spring DI(dependency injection). and it working fine with service layers and DAOs. – Umair Apr 01 '13 at 08:32
0

You can create Converter for your Custom Object Currency.

Step 1: Create a Converter class and Implement javax.faces.convert.Converter Interface,Override getAsObject and getAsString methods and write your logic for String to Object Conversion and Object to String Conversion.

Step 2: Simply declare something like @FacesConverter("currencyConverter") in your converter class or If you want use Spring Inject or Autowired Annotation in Converter class declare your Converter Class with @Component("currencyConverter") Annotation and don't use @FacesConverter.And your converter class should in component scan package.

Step 3: Declare your converter in Selectonemenu Using converter property.

If you still have any problem please refer this link

http://www.techpages.org/jsf/jsf-custom-object-converter-from-selectonemenu/2428/

Mohan
  • 1
  • 1