gpt4 book ai didi

java - 弃用 richfaces javax.faces.el.MethodBinding 替换使用

转载 作者:搜寻专家 更新时间:2023-10-31 08:09:32 24 4
gpt4 key购买 nike

我发现这段代码有效,因为我可以通过编程方式创建一个 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 );

最佳答案

像往常一样,所有弃用确实只是在 API 文档中描述,包括有关替换的详细信息。

为了有一个清晰的概述,这里是 JSF 1.2 之前和 JSF 1.2 之后动态创建 Action 和 ActionListener 的方法:

在 JSF 1.0/1.1 中创建 Action 绑定(bind):

MethodBinding action = FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.action}", new Class[0]);
uiCommandComponent.setAction(action);

在 JSF 1.0/1.1 中创建 ActionListener 绑定(bind):

MethodBinding actionListener =  FacesContext.getCurrentInstance().getApplication()
.createMethodBinding("#{bean.actionListener}", new Class[] {ActionEvent.class});
uiCommandComponent.setActionListener(actionListener);

在 JSF 1.2 或更新版本中创建 Action 表达式:

FacesContext context = FacesContext.getCurrentInstance();
MethodExpression action = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.action}", String.class, new Class[0]);
uiCommandComponent.setActionExpression(action);

在 JSF 1.2 或更新版本中创建 ActionListener 表达式:

FacesContext context = FacesContext.getCurrentInstance();
MethodExpression actionListener = context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(), "#{bean.actionListener}", null, new Class[] {ActionEvent.class});
uiCommandComponent.addActionListener(new MethodExpressionActionListener(actionListener));

为了避免大量样板代码,您可以将它很好地包装在辅助方法中(如果需要,在辅助/实用程序类中),例如:

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}");

关于java - 弃用 richfaces javax.faces.el.MethodBinding 替换使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2158009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com