如何向 ADF 中的命令按钮添加基于条件的操作?

How to add condition based action to command button in ADF?

如何根据命令按钮的 action 属性内部调用的方法的 return 值导航到下一页。

<af:button id="tt_b2"
           rendered="#{attrs.nextRendered}"
           partialSubmit="true"                        
           action="#{attrs.backingBean.nextAction}"
           text="Next"
           disabled="#{attrs.nextDisabled}"/>

private static final String NEXT_NAVIGATION_ACTION = "controllerContext.currentViewPort.taskFlowContext.trainModel.getNext";

public String nextAction() {
    if (validate()) {
        updateModel();
        return NEXT_NAVIGATION_ACTION;
    }
    return null;
}

用例是针对train model做的,是基于这篇博客实现的:http://javacollectibles.blogspot.co.uk/2014/10/adf-train-template.html

我们需要在模板中定义一个通用的下一个动作,但应该根据是否已通过所有验证检查来有条件地调用该动作。

不需要硬编码任何步骤,您可以查询TaskFlowTrainModel

/**
* Navigates to the next stop in a train
* @return outcome string
*/
public String navigateNextStop() {
  String nextStopAction = null;
  ControllerContext controllerContext = ControllerContext.getInstance();
  ViewPortContext currentViewPortCtx = controllerContext.getCurrentViewPort();
  TaskFlowContext taskFlowCtx = currentViewPortCtx.getTaskFlowContext();
  TaskFlowTrainModel taskFlowTrainModel = taskFlowCtx.getTaskFlowTrainModel();
  TaskFlowTrainStopModel currentStop = taskFlowTrainModel.getCurrentStop();
  TaskFlowTrainStopModel nextStop = taskFlowTrainModel.getNextStop(currentStop);
  //is either null or has the value of outcome
  return nextStopAction;
}

Full code of the sample can be found on the ADF Code Corner.

要按任务流结果导航,您只需提供准确的结果字符串作为方法的 return:

private static final String NEXT_NAVIGATION_ACTION = "next";
public String nextAction() {
  if (validate()) {
      updateModel();
      return NEXT_NAVIGATION_ACTION;
  }
  return null;
}

尝试使用 ADFUtils.invokeEl

public String nextAction() {
    if (validate()) {
        updateModel();
        return (String)ADFUtils.invokeEL(NEXT_NAVIGATION_ACTION);
    }
    return null;
}

你能验证一下吗,你可以通过phase listener来做。 验证您在阶段侦听器中的条件并允许它继续前进,如果它验证否则停止线程执行。

下面是示例阶段侦听器代码。

public class MyPhaseListener implements PagePhaseListener{
public MyPhaseListener() {
    super();
}

@Override
public void afterPhase(PagePhaseEvent pagePhaseEvent) {
        if (pagePhaseEvent.getPhaseId() == Lifecycle.PREPARE_RENDER_ID ) {            
     // DO your logic here

  }
}

@Override
public void beforePhase(PagePhaseEvent pagePhaseEvent) {
}

}