"converter class getasobject pass parameter to service class which accepts object" Code Answer

4

use a following converter, that simply converts number (which is a string, according to your code) to employee object:

@named("employeeconverter")
public class employeeconverter implements converter {

    @inject
    private employeeservice employeeservice;

    public object getasobject(facescontext context, uicomponent component, string value) {
        if(value == null || value.equals("")) {
            return null;
        }
        employee employee = employeeservice.getemployeebynumber(value);//or if it is not a string, replace value with integer.parseint(value)
        if(employee == null) {
            throw new converterexception(new facesmessage("employee with number: " + value + " not found."));
        }
        return employee;
    }

    public string getasstring(facescontext context, uicomponent component, object value) {
        if (!(value instanceof employee) || (value == null)) {
            return null;
        }
        return ((employee)value).getemployeenumber();
    }

}

and use it in your views by converter="#{employeeconverter}".

By Jesse Kernaghan on August 7 2022

Answers related to “converter class getasobject pass parameter to service class which accepts object”

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