Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between converter="entityConverter" and converter="#{entityConverter}"

Tags:

jsf

converters

el

Is there any difference between converter="entityConverter" and converter="#{entityConverter}" ?

Initially, my application works as a charm with such code:

<p:selectOneMenu id="civilityId"
                 value="#{customerController.selected.civilityId}"
                 converter="civilityConverter"> ...

but when I change converter="civilityConverter" to converter="#{civilityConverter}",

I get this error :

Grave:   Error Rendering View[/customer/index.xhtml]
javax.el.PropertyNotFoundException: /customer/Edit.xhtml @30,186
value="#{customerController.selected.civilityId}":
Target Unreachable, 'null' returned null

Thanks for any help.

like image 778
devware33 Avatar asked Dec 02 '25 14:12

devware33


1 Answers

Although this is not specific to the <p:selectOneMenu>, it would be worthwhile to just check its tag documentation if it gives any hints. It says the following:

Name: converter

Type: javax.el.ValueExpression (must evaluate to java.faces.convert.Converter)

Description: An el expression or a literal text that defines a converter for the component. When it's an EL expression, it's resolved to a converter instance. In case it's a static text, it must refer to a converter id.

So, in case of

converter="entityConverter"

it will be resolved as a converter ID which is declared via either the @FacesConverter annotation on the converter class

@FacesConverter("entityConverter")
public class EntityConverter implements Converter {}

or the <converter-id> entry of <converter> registration in faces-config.xml:

<converter>
    <converter-id>entityConverter</converter-id>
    <converter-class>com.example.EntityConverter</converter-class>
</converter>

And, in case of

converter="#{entityConverter}"

it will be resolved as a concrete Converter instance in the EL context. The normal way to put such an instance in the EL context is to declare it as a managed bean. E.g. via JSF

@ManagedBean
@RequestScoped
public class EntityConverter implements Converter {}

or via CDI

@Named
@RequestScoped
public class EntityConverter implements Converter {}

This is often done so in order to be able to inject an @EJB into it, so that any necessary business/DB logic could be performed. Until the upcoming JSF 2.3, the @FacesConverter (and @FacesValidator) namely doesn't support dependency injection (which you could however work around by using JSF utility library OmniFaces).

See also:

  • How to inject @EJB, @PersistenceContext, @Inject, @Autowired, etc in @FacesConverter?
  • /Registration.xhtml @19,81 validator="#{UsernameValidator}": Identity 'UsernameValidator' was null and was unable to invoke
like image 196
BalusC Avatar answered Dec 05 '25 07:12

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!