2

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.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
devware33
  • 71
  • 1
  • 3

3 Answers3

2

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

converter="civilityConverter" is for FacesConverters
converter=#{civilityConverter} is for Named managed beans.

acpuma
  • 419
  • 4
  • 14
0

Just to add some useful information, I can add following thing.

I use FacesConverter and I have tested following JSF elements

<h:selectOneMenu value="#{controller.panelTpl.txtUser.value}">
    <f:converter converterId="userConverter"/>
    <f:selectItems 
        value="#{controller.panelTpl.txtOriginator.userList}"
        var="user" 
        itemLabel="#{user.humanName}"
        itemValue="#{user}"
        />
</h:selectOneMenu>

and

<h:selectOneMenu value="#{controller.panelTpl.txtOriginator.value}">
    <f:converter converterId="#{userConverter}"/>
    <f:selectItems 
        value="#{controller.panelTpl.txtOriginator.userList}"
        var="user" 
        itemLabel="#{user.humanName}"
        itemValue="#{user}"
        />
</h:selectOneMenu>

The first work correctly.

The second return following error

Severe:   JSF1006: Cannot instantiate converter of type 
Severe:   Expression Error: Named Object: ' not found.
javax.faces.FacesException: Expression Error: Named Object: ' not found.
    at com.sun.faces.application.ApplicationImpl.createConverter(ApplicationImpl.java:1339)
    at javax.faces.application.ApplicationWrapper.createConverter(ApplicationWrapper.java:393)
    at com.sun.faces.facelets.tag.jsf.ConverterTagHandlerDelegateImpl.createConverter(ConverterTagHandlerDelegateImpl.java:158)

As Balus said, this is not only linked to Primefaces.

I use JSF 2.2 because I deploy my application on Glassfish 4.1.

schlebe
  • 2,315
  • 2
  • 27
  • 39