You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Cedric Hurst <ce...@gmail.com> on 2008/03/31 19:06:37 UTC
Dependency Injection in Validators
Hello all... does anyone know if it's possible to use dependency injection
inside JSF validators? My code is as follows:
package com.ibm.wasce.customerLocator.web.jsf;
import javax.ejb.EJB;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.component.UIComponent;
import com.ibm.wasce.customerLocator.ejb.interfaces.CustomerBeanLocal;
public class CustomerNumberValidator implements Validator {
@EJB(name="CustomerBeanLocal")
CustomerBeanLocal customerSessionBean;
public void validate(FacesContext context, UIComponent component, Object
value) throws ValidatorException {
System.out.println(customerSessionBean);
if(customerSessionBean.findCustomerByCustomerNumber((String)value) !=
null) {
FacesMessage message =
MessageGenerator.getCustomerNumberAlreadyExistsMessage();
throw new ValidatorException(message);
}
}
}
The problem is that customerSessionBean is coming up as null. Of course,
this behavior makes sense because the validator is not a managed bean. But
is there a way to treat it as if it was?
--
View this message in context: http://www.nabble.com/Dependency-Injection-in-Validators-tp16397217p16397217.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.
Re: Dependency Injection in Validators
Posted by Cedric Hurst <ce...@gmail.com>.
Thanks, Madhav. The issue actually turned out not to be directly related to
MyFaces. It was due to a potential bug in my app server which wouldn't
allow the same resource to be injected twice in the same package. I
developed a workaround and was able to get the validation working the way I
originally intended. But thanks for the suggestions. I've been meaning to
check out Seam for some time now.
Madhav Bhargava wrote:
>
> Seam supports this kind of injection via @in and @out annotations
> http://www.javabeat.net/jboss-seam/2007/06/jboss-seam-introduction/
>
> Another way would be to integrate Spring + JSF.
> Yet another way would be to manage business layer with Spring and then you
> an instance of a bean which implements ApplicationContextAware to get you
> any spring bean reference to any other bean not managed by Spring.
>
> ~madhav
>
> On Mon, Mar 31, 2008 at 1:42 PM, Cedric Hurst <ce...@gmail.com>
> wrote:
>
>>
>> Hello all... does anyone know if it's possible to use dependency
>> injection
>> inside JSF validators? My code is as follows:
>>
>> package com.ibm.wasce.customerLocator.web.jsf;
>>
>> import javax.ejb.EJB;
>> import javax.faces.validator.Validator;
>> import javax.faces.validator.ValidatorException;
>> import javax.faces.application.FacesMessage;
>> import javax.faces.context.FacesContext;
>> import javax.faces.component.UIComponent;
>>
>> import com.ibm.wasce.customerLocator.ejb.interfaces.CustomerBeanLocal;
>>
>> public class CustomerNumberValidator implements Validator {
>> @EJB(name="CustomerBeanLocal")
>> CustomerBeanLocal customerSessionBean;
>>
>> public void validate(FacesContext context, UIComponent component,
>> Object
>> value) throws ValidatorException {
>> System.out.println(customerSessionBean);
>>
>> if(customerSessionBean.findCustomerByCustomerNumber((String)value)
>> !=
>> null) {
>> FacesMessage message =
>> MessageGenerator.getCustomerNumberAlreadyExistsMessage();
>> throw new ValidatorException(message);
>> }
>> }
>> }
>>
>>
>> The problem is that customerSessionBean is coming up as null. Of course,
>> this behavior makes sense because the validator is not a managed bean.
>> But
>> is there a way to treat it as if it was?
>> --
>> View this message in context:
>> http://www.nabble.com/Dependency-Injection-in-Validators-tp16397217p16397217.html
>> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>>
>>
>
>
> --
> When I tell the truth, it is not for the sake of convincing those who do
> not
> know it, but for the sake of defending those that do
>
>
--
View this message in context: http://www.nabble.com/Dependency-Injection-in-Validators-tp16397217p16407993.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.
Re: Dependency Injection in Validators
Posted by Madhav Bhargava <un...@gmail.com>.
Seam supports this kind of injection via @in and @out annotations
http://www.javabeat.net/jboss-seam/2007/06/jboss-seam-introduction/
Another way would be to integrate Spring + JSF.
Yet another way would be to manage business layer with Spring and then you
an instance of a bean which implements ApplicationContextAware to get you
any spring bean reference to any other bean not managed by Spring.
~madhav
On Mon, Mar 31, 2008 at 1:42 PM, Cedric Hurst <ce...@gmail.com>
wrote:
>
> Hello all... does anyone know if it's possible to use dependency injection
> inside JSF validators? My code is as follows:
>
> package com.ibm.wasce.customerLocator.web.jsf;
>
> import javax.ejb.EJB;
> import javax.faces.validator.Validator;
> import javax.faces.validator.ValidatorException;
> import javax.faces.application.FacesMessage;
> import javax.faces.context.FacesContext;
> import javax.faces.component.UIComponent;
>
> import com.ibm.wasce.customerLocator.ejb.interfaces.CustomerBeanLocal;
>
> public class CustomerNumberValidator implements Validator {
> @EJB(name="CustomerBeanLocal")
> CustomerBeanLocal customerSessionBean;
>
> public void validate(FacesContext context, UIComponent component,
> Object
> value) throws ValidatorException {
> System.out.println(customerSessionBean);
> if(customerSessionBean.findCustomerByCustomerNumber((String)value)
> !=
> null) {
> FacesMessage message =
> MessageGenerator.getCustomerNumberAlreadyExistsMessage();
> throw new ValidatorException(message);
> }
> }
> }
>
>
> The problem is that customerSessionBean is coming up as null. Of course,
> this behavior makes sense because the validator is not a managed bean.
> But
> is there a way to treat it as if it was?
> --
> View this message in context:
> http://www.nabble.com/Dependency-Injection-in-Validators-tp16397217p16397217.html
> Sent from the MyFaces - Users mailing list archive at Nabble.com.
>
>
--
When I tell the truth, it is not for the sake of convincing those who do not
know it, but for the sake of defending those that do