Я нашел эту часть работ кода, в которых я могу программно создавать richfaces выпадающее меню. Но часть кода удерживается от использования. Кто-либо может сказать мне, что вставить вместо вызовов устаревших?
Спасибо
public HtmlDropDownMenu getMyMenu()
{
HtmlDropDownMenu menu = new HtmlDropDownMenu();
menu.setValue( "Node Select" );
HtmlMenuItem menuItem = new HtmlMenuItem();
// TODO programmatically pass from getNodes into a String[] rather than an ArrayList of SelectItems
String subOption = "myBox";
menuItem.setValue( subOption );
Application app = FacesContext.getCurrentInstance().getApplication();
javax.faces.el.MethodBinding mb = app.createMethodBinding( "#{PrismBacking.onItemClick}", new Class[] { ActionEvent.class } );
menuItem.setActionListener( mb );
menu.getChildren().add( menuItem );
return( menu );
}
public void onItemClick( ActionEvent event )
{
Object obj = event.getSource();
if( obj instanceof HtmlMenuItem )
{
HtmlMenuItem item = (HtmlMenuItem)obj;
if( item != null )
{
lastItem = item.getValue().toString();
}
}
}
строки кода устаревшие:
javax.faces.el.MethodBinding mb = app.createMethodBinding( "#{PrismBacking.onItemClick}", new Class[] { ActionEvent.class } );
menuItem.setActionListener( mb );
В javadocs четко указано:
Application.createMethodBinding
Deprecated. Это было заменено вызовом getExpressionFactory() затем ExpressionFactory.createMethodExpression(javax.el.ELContext, java.lang.String, java.lang.Class, java.lang.Class[]).
Вот как его использовать:
MethodExpression methodExpression =
application.getExpressionFactory().createMethodExpression(
FacesContext.getCurrentInstance().getELContext(),
"#{PrismBacking.onItemClick}",
null,
new Class[] { ActionEvent.class });
menuItem.setActionExpression(methodExpression);
Как обычно, все амортизацию действительно просто описано в документах API, включая подробную информацию о замене.
Чтобы иметь четкий обзор, вот как предыдущий JSF 1.2, так и Post-JSF 1.2 способами создания действия и диаграммы ActionListener динамически:
Создание привязки действия в JSF 1.0 / 1.1:
MethodBinding action = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.action}", new Class[0]);
uiCommandComponent.setAction(action);
Создание привязки действия JSF 1.0 / 1.1:
MethodBinding actionListener = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.actionListener}", new Class[] {ActionEvent.class});
uiCommandComponent.setActionListener(actionListener);
Создать экспрессию действий в JSF 1.2 или новее:
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression action = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.action}", String.class, new Class[0]);
uiCommandComponent.setActionExpression(action);
Создать экспрессию ActionListener в JSF 1.2 или новее:
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression actionListener = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.actionListener}", null, new Class[] {ActionEvent.class});
uiCommandComponent.addActionListener(new MethodExpressionActionListener(actionListener));
Чтобы избежать большого количества кода Bovertlate, вы можете просто обернуть его в методах помощника (если Необходимо в помощниках / утилителе), например:
public static MethodExpression createAction(String actionExpression, Class<?> returnType) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), actionExpression, returnType, new Class[0]);
}
public static MethodExpressionActionListener createActionListener(String actionListenerExpression) {
FacesContext context = FacesContext.getCurrentInstance();
return new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), actionListenerExpression, null, new Class[] {ActionEvent.class}));
}
Так что вы можете просто использовать его следующим образом:
uiCommandComponent.setActionExpression(createAction("#{bean.action}", String.class);
uiCommandComponent.addActionListener(createActionListener("#{bean.actionListener}");
Механизмы замены подробно описаны в API je5 (из которых является частью JSF):