简体   繁体   English

是否可以在JSF中将视图范围的bean注入@FacesValidator中?

[英]Is it possible to inject a view scoped bean into a @FacesValidator in JSF?

As the title implies, I'm trying to inject a view scoped bean into a validator decorated by @FacesValidator as follows. 就像标题所暗示的那样,我正尝试将视图范围的bean注入到@FacesValidator装饰的验证器中,如下所示。

@FacesValidator(value = "priceRangeValidator")
public final class PriceRangeValidator implements Validator {
    @ManagedProperty(value="#{rangeSliderBean}")
    private RangeSliderBean rangeSliderBean; //Setter only.

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        // The bean instance is unavailable here. It is null.
    }
}

The target view scoped bean - RangeSliderBean is unavailable in the validate() method. 目标视图范围的bean- RangeSliderBeanvalidate()方法中不可用。


I'm temporarily following the following way to get an instance of that bean in the validate() method itself. 我暂时按照以下方法在validate()方法本身中获取该bean的实例。

RangeSliderBean rangeSliderBean = context.getApplication().evaluateExpressionGet(context, "#{rangeSliderBean}", RangeSliderBean.class);

Is it possible to inject a view scoped JSF managed bean into a validator using the @ManagedProperty annotation or something else? 是否可以使用@ManagedProperty批注或其他方式将视图范围内的JSF托管bean注入到验证器中?

I'm using JSF 2.2.6. 我正在使用JSF 2.2.6。


UPDATE: 更新:

This does not work on Mojarra 2.3.0-m01 . 这在Mojarra 2.3.0-m01不起作用 The bean instance still remains null as it did before. Bean实例仍然像以前一样保持为null This time long after this post, I took a corresponding view scoped CDI bean ( @ManagedProperty was replaced by @Inject ). 这段时间很久以后,我带了一个对应的视图作用域CDI bean( @ManagedProperty@Inject代替了)。

Try dynamic Injection by evaluating an expression using Application like this. 通过使用这样的Application评估表达式来尝试动态注入。

FacesContext facesContext = FacesContext.getCurrentInstance();
RangeSliderBean rangeSliderBean = facesContext.getApplication().evaluateExpressionGet(facesContext, "#{rangeSliderBean}", RangeSliderBean .class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM