我尝试了几种方法来实现和编写带有action或actionListener的commandButon。我尝试实现ajax行为,设置动作并设置actionListener但没有成功。在我的pom中,我添加了 el-api 依赖,因为我的项目没有得到MethodExpression,MethodExpressionActionListener和ExpressionFactory类,并且在某些示例代码中它们是必需的。我没有在glassfish日志中获得任何错误消息,也没有js log。
修改 如果我将bean范围更改为Session,则会调用action方法,但不会在此balus example上使用请求或视图
方法' BeanUtils类
public static MethodExpression createMethodExpression(String valueExpression,Class<?> expectedReturnType,Class<?>[] expectedParamTypes) {
MethodExpression methodExpression = null;
try {
FacesContext fc = FacesContext.getCurrentInstance();
ExpressionFactory factory = fc.getApplication().getExpressionFactory();
methodExpression = factory.createMethodExpression(fc.getELContext(), valueExpression, expectedReturnType, expectedParamTypes);
} catch (Exception e) {
throw new FacesException("Method expression '" + valueExpression + "' could not be created.");
}
return methodExpression;
}
public static MethodExpressionActionListener createMethodActionListener(String valueExpression,Class<?> expectedReturnType,Class<?>[] expectedParamTypes) {
MethodExpressionActionListener actionListener = null;
try {
actionListener = new MethodExpressionActionListener(createMethodExpression(
valueExpression, expectedReturnType, expectedParamTypes));
} catch (Exception e) {
throw new FacesException("Method expression for ActionListener '" + valueExpression
+ "' could not be created.");
}
return actionListener;
}
按钮代码 (每次尝试都相同)
CommandButton cancelBtn=(CommandButton)app.createComponent(CommandButton.COMPONENT_TYPE);
cancelBtn.setId("deleteText_btn_" + panel.getId());
cancelBtn.setAlt("Change");
cancelBtn.setTitle("Change");
cancelBtn.setValue("Change");
首先尝试 - Ajax行为
MethodExpression me = BeanUtils.createMethodExpression("#{eventProvider.deleteText()}",null,new Class<?>[]{BehaviorEvent.class});
AjaxBehavior ajaxBehavior = (AjaxBehavior) app.createBehavior(AjaxBehavior.BEHAVIOR_ID);
ajaxBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(me, me));
cancelBtn.addClientBehavior("submit",ajaxBehavior);
第二次尝试 - 设置actionListener
FacesContext context = FacesContextWrapper.getCurrentInstance();
MethodExpressionActionListener methodExpression = new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
.createMethodExpression(context.getELContext(),"#{eventProvider.deleteText}", null, new Class[] {ActionEvent.class}));
cancelBtn.addActionListener(methodExpression);
第三次尝试 - 设置操作
FacesContext context = FacesContext.getCurrentInstance();
MethodExpression methodExpression = context.getApplication().getExpressionFactory().createMethodExpression(
context.getELContext(), "#{eventProvider.deleteText}", null, new Class[] { ActionEvent.class });
cancelBtn.setActionExpression(methodExpression);
必须从action / actionListener
调用的方法 @Component("eventProvider")
@Scope("view")
public class EventProvider {
............
............
public void deleteText(ActionEvent event){
logger.entry("EventProvider.deleteText()");
System.out.print("debbuging");
}
}