"how create a custom converter in jsf 2?" Code Answer


you're trying to pass a complex object around as http request parameter which can be only be a string. jsf/el has builtin converters for primitives and its wrappers (e.g. int, integer) and even enums. but for all other types you really need to write a custom converter. in this case you need to write a converter which converts between string and operation. the string is then used as option value (open page in browser, rightclick and view source and notice the <option value>). the operation is then used as model value. the string should uniquely identify the operation object. you could use operation id for this.

but in this particular case, with such a hardcoded map and a relatively simple model, i think it's easier to use an enum instead.

public enum operation {

    donation("donation"), exchange("exchange");

    private string label;

    private operation(string label) {
        this.label = label;

    public string getlabel() {
        return label;



private operation operation; // +getter +setter

public operation[] getoperations() {
    return operation.values();


<h:selectonemenu value="#{bean.operation}">
    <f:selectitems value="#{bean.operations}" var="operation" itemvalue="#{operation}" itemlabel="#{operation.label}" />

but if those values have actually to be retrieved from the db and its size is undefinied, then you still really need a custom converter. you could in getasstring() return the id and in getasobject() use the operation dao/ejb to get an operation by the id.

public class operationconverter implements converter {

    private operationservice operationservice;

    public string getasstring(facescontext context, uicomponent component, object value) {
        // convert here operation object to string value for use in html.
        if (!(value instanceof operation) || ((operation) value).getid() == null) {
            return null;

        return string.valueof(((operation) value).getid());

    public object getasobject(facescontext context, uicomponent component, string value) {
        // convert here string submitted value to operation object.
        if (value == null || !value.matches("\d+")) {
            return null;

        operation operation = operationservice.find(long.valueof(value));

        if (operation == null) {
            throw new converterexception(new facesmessage("unknown operation id: " + value));

        return operation;


use it as follows:

<h:selectonemenu ... converter="#{operationconverter}">

as to why it's a @managedbean instead of @facesconverter, read this: converting and validating get request parameters.

update as to the validation error: value not valid error, this means that the equals() method of the operation class is broken or missing. during validation, jsf compares the submitted value with the list of available values by object#equals(). if no one of the list matches with the submitted value, then you'll see this error. so, ensure that equals() is properly implemented. here's a basic example which compares by the db technical identity.

public boolean equals(object other) {
    return (other instanceof operation) && (id != null) 
         ? id.equals(((operation) other).id) 
         : (other == this);

don't forget to implement hashcode() as well:

public int hashcode() {
    return (id != null) 
         ? (getclass().hashcode() + id.hashcode())
         : super.hashcode();
By tjohnson on April 26 2022

Answers related to “how create a custom converter in jsf 2?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.