0

I have a web project that has FacesValidator, this validator needs to access an EJB service to verify if a record exists. Unfortunately, I cannot inject my enterprise beans since the validator is not a managed-bean, so I'm trying to access it via InitialContext. I've tried different combination from http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html but failed.

What works is this format:

java:global/myProject-ear-1.0.0/myProject/MyService!com.czetsuya.myProject.service.membership.MyService,

My question is can it be simplify? Seems too long.

Thanks,
czetsuya

czetsuya
  • 4,773
  • 13
  • 53
  • 99

1 Answers1

4

Look at the server logs. A bit decent EJB container (at least, Glassfish 3 and JBoss 6/7 do), logs all available JNDI names of the EJB during EJB deployment step. Provided that the validator is properly been put in the WAR and the EJB has a @Local interface, then the shortest JNDI name would be the java:app one which should in your case have been java:app/myProject/MyService.

A completely different alternative is to just make the validator a JSF or CDI managed bean instead, so that you can just use the @EJB annotation.

@ManagedBean // Or @Named.
@ApplicationScoped // Provided that the instance doesn't have any state.
public class MyValidator implements Validator {

    @EJB
    private MyService myService;

    // ...
}

and reference it by binding instead of validatorId:

<f:validator binding="#{myValidator}" />

Note that from JSF 2.2 on, you should be able to inject @EJB in a @FacesValidator (and @FacesConverter).

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555