简体   繁体   English

在运行时为selectOneMenu创建一个转换器

[英]Create a converter for selectOneMenu at runtime

So I need to create a screen that is dynamic and provides information about objects wich have an unknown number of parameters, of various types (could be texte, could be date, and, thing that causes problem, could be an item to select from a SelectOneMenu, whose binding (like "#{myBean.list}" and converter (like "#{somethingConverter}) are fields of the parameter. 因此,我需要创建一个动态屏幕,并提供有关具有未知数量参数的对象的信息,这些参数有各种类型(可能是文本,可能是日期,而导致问题的原因,可能是从SelectOneMenu,其绑定(如“#{myBean.list}”)和转换器(如“#{somethingConverter})是该参数的字段。

So, to create the SelectOneMenu, I've got something like this: 因此,要创建SelectOneMenu,我需要执行以下操作:

    SelectOneMenu menu = new SelectOneMenu();
    UISelectItems items = new UISelectItems();

    ValueExpression expr = getValueExpression(param.binding); //param.binding is a String containing the expression
    items.setValueExpression("value", expr);

    menu.getChildren().add(items);
    panel.getChildren().add(menu);
}


private ValueExpression getValueExpression(String expression) {
    ExpressionFactory expressionFactory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
    ELContext expressionContext = FacesContext.getCurrentInstance().getELContext();
    return expressionFactory.createValueExpression(expressionContext, expression, Object.class);
}

But I have to set a Converter to the menu to, and I can't use the same method because it's impossible to instantiate a Converter, so I can't juste set it an expression. 但是我必须将Converter设置为菜单,并且我不能使用相同的方法,因为无法实例化Converter,因此我不能仅将其设置为表达式。

I tried using reflection, and it almost works, but in my Converters , some managers are created using 我尝试使用反射,它几乎可以工作,但是在我的Converters中,使用

    @Inject
    private SomeManager mManager;

And if I create a converter using reflection, those managers aren't created, and I end up getting a NullPointerException when I use them. 而且,如果我使用反射创建转换器,则不会创建这些管理器,使用它们时最终会收到NullPointerException。

So is there a way I can dynamically create a converter using the expression I would put after "converter=" in my XHtml page ? 那么,有没有一种方法可以使用在XHtml页面中“ converter =“之后的表达式来动态创建转换器? Or, using reflection, to force my converter classes to do the "inject" instantiation ? 或者,使用反射来强制我的转换器类进行“注入”实例化?

Thank for your help ! 谢谢您帮忙 !

I tried using reflection, and it almost works, but in my Converters , some managers are created using 我尝试使用反射,它几乎可以工作,但是在我的Converters中,使用

 @Inject private SomeManager mManager; 

And if I create a converter using reflection, those managers aren't created, and I end up getting a NullPointerException when I use them. 而且,如果我使用反射创建转换器,则不会创建这些管理器,使用它们时最终会收到NullPointerException。

In other words, your converters are managed beans (eg @Named or @ManagedBean ), not faces converters (eg @FacesConverter or <converter> )? 换句话说,您的转换器是托管bean(例如@Named@ManagedBean ),而不是转换器(例如@FacesConverter<converter> )吗? In that case, you'd need to create them by programmatically evaluating the EL expression referencing it like so 在这种情况下,您需要通过以编程方式评估引用它的EL表达式来创建它们

Converter converter = context.getApplication().evaluateExpressionGet(context, "#{someConverter}", Converter.class);

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

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