Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject managed-bean property into custom converter

How can I inject a property of a managed-bean into a custom converter?

For instance, a generic List so that I can extract my object from the list inside the converter?

like image 523
djmj Avatar asked Dec 30 '11 03:12

djmj


1 Answers

In several case, when you need to inject a bean such as ManagedBean, EJB, etc. into a Converter or a Validator, you can try annotating your Converter or Validator as a ManagedBean. For example, you can try this:

@ManagedBean
@RequestScoped
public class MyConverter implements Converter {
    @EJB
    private MrEJBBean mrEJBBean;
    @ManagedProperty(value="#{mrsManagedBean}")        
    private MrsManagedBean mrsManagedBean;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {        
        // Convert to object
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        // Convert to string
    }

}

You can take a look at this for an example on Validator.

like image 89
Mr.J4mes Avatar answered Oct 18 '22 21:10

Mr.J4mes