[英]Create a converter for selectOneMenu at runtime
因此,我需要創建一個動態屏幕,並提供有關具有未知數量參數的對象的信息,這些參數有各種類型(可能是文本,可能是日期,而導致問題的原因,可能是從SelectOneMenu,其綁定(如“#{myBean.list}”)和轉換器(如“#{somethingConverter})是該參數的字段。
因此,要創建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);
}
但是我必須將Converter設置為菜單,並且我不能使用相同的方法,因為無法實例化Converter,因此我不能僅將其設置為表達式。
我嘗試使用反射,它幾乎可以工作,但是在我的Converters中,使用
@Inject
private SomeManager mManager;
而且,如果我使用反射創建轉換器,則不會創建這些管理器,使用它們時最終會收到NullPointerException。
那么,有沒有一種方法可以使用在XHtml頁面中“ converter =“之后的表達式來動態創建轉換器? 或者,使用反射來強制我的轉換器類進行“注入”實例化?
謝謝您幫忙 !
我嘗試使用反射,它幾乎可以工作,但是在我的Converters中,使用
@Inject private SomeManager mManager;
而且,如果我使用反射創建轉換器,則不會創建這些管理器,使用它們時最終會收到NullPointerException。
換句話說,您的轉換器是托管bean(例如@Named
或@ManagedBean
),而不是轉換器(例如@FacesConverter
或<converter>
)嗎? 在這種情況下,您需要通過以編程方式評估引用它的EL表達式來創建它們
Converter converter = context.getApplication().evaluateExpressionGet(context, "#{someConverter}", Converter.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.