Search in sources :

Example 1 with ActionSource2

use of jakarta.faces.component.ActionSource2 in project faces by jakartaee.

the class BaseActionSource2TestServlet method actionSource2GetSetActionExpressionTest.

// init
// ------------------------------------------------------------ Test Methods
public void actionSource2GetSetActionExpressionTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    request.setAttribute("bean", new SimpleBean());
    ExpressionFactory factory = JspFactory.getDefaultFactory().getJspApplicationContext(servletContext).getExpressionFactory();
    MethodExpression expression = factory.createMethodExpression(getFacesContext().getELContext(), "#{bean.action}", java.lang.String.class, new Class[] {});
    ActionSource2 source2 = (ActionSource2) createComponent();
    source2.setActionExpression(expression);
    if (source2.getActionExpression() != expression) {
        out.println(JSFTestUtil.FAIL + " Unexpected return value from" + " getActionExpression() after having just called" + " setActionExpression().");
        out.println("Expected: " + expression);
        out.println("Received: " + source2.getActionExpression());
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : ExpressionFactory(jakarta.el.ExpressionFactory) ActionSource2(jakarta.faces.component.ActionSource2) MethodExpression(jakarta.el.MethodExpression) PrintWriter(java.io.PrintWriter)

Example 2 with ActionSource2

use of jakarta.faces.component.ActionSource2 in project faces by jakartaee.

the class BaseActionSourceTestServlet method actionSourceRemoveActListNPETest.

// ActionSource.removeActionListener() throws NullPointerException if
// ActionListener is null.
public void actionSourceRemoveActListNPETest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    ActionSource2 source = (ActionSource2) createComponent();
    JSFTestUtil.checkForNPE(source.getClass(), "removeActionListener", new Class<?>[] { ActionListener.class }, new Object[] { null }, out);
}
Also used : ActionSource2(jakarta.faces.component.ActionSource2) PrintWriter(java.io.PrintWriter)

Example 3 with ActionSource2

use of jakarta.faces.component.ActionSource2 in project faces by jakartaee.

the class BaseActionSourceTestServlet method actionSourceGetSetActListTest.

// ActionSource.{set,get}ActionListener()
public void actionSourceGetSetActListTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    request.setAttribute("actionListener", new ActionListener() {

        public void processAction(ActionEvent event) throws AbortProcessingException {
        // no-op
        }
    });
    FacesContext context = getFacesContext();
    ELContext elcontext = context.getELContext();
    MethodExpression binding = getApplication().getExpressionFactory().createMethodExpression(elcontext, "#{requestScope.actionListener.processAction}", null, new Class[] { ActionEvent.class });
    ActionSource2 source = (ActionSource2) createComponent();
    MethodExpressionActionListener listener = new MethodExpressionActionListener(binding);
    source.addActionListener(listener);
    ActionListener[] result = source.getActionListeners();
    if (result[0] != listener) {
        out.println(JSFTestUtil.FAIL + " getActionListener() failed to return " + "the value set via setActionListener().");
        out.println("Expected: " + binding);
        out.println("Received: " + result);
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ELContext(jakarta.el.ELContext) ActionListener(jakarta.faces.event.ActionListener) MethodExpressionActionListener(jakarta.faces.event.MethodExpressionActionListener) ActionEvent(jakarta.faces.event.ActionEvent) AbortProcessingException(jakarta.faces.event.AbortProcessingException) ActionSource2(jakarta.faces.component.ActionSource2) MethodExpression(jakarta.el.MethodExpression) PrintWriter(java.io.PrintWriter) MethodExpressionActionListener(jakarta.faces.event.MethodExpressionActionListener)

Example 4 with ActionSource2

use of jakarta.faces.component.ActionSource2 in project faces by jakartaee.

the class BaseActionSourceTestServlet method actionSourceSetIsImmediateTest.

// ActionSource.{set,is}Immediate
public void actionSourceSetIsImmediateTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    ActionSource2 source = (ActionSource2) createComponent();
    if (source.isImmediate()) {
        out.println(JSFTestUtil.FAIL + " Expected isImmediate() to return" + " false as the default value");
        return;
    }
    source.setImmediate(true);
    if (!source.isImmediate()) {
        out.println(JSFTestUtil.FAIL + " Expected isImmediate() to return" + " true after having explicitly set it as such via" + " setImmediate()");
        return;
    }
    source.setImmediate(false);
    if (source.isImmediate()) {
        out.println(JSFTestUtil.FAIL + " Expected isImmediate() to return" + " false after having explicitly set it as such via" + " setImmediate()");
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : ActionSource2(jakarta.faces.component.ActionSource2) PrintWriter(java.io.PrintWriter)

Example 5 with ActionSource2

use of jakarta.faces.component.ActionSource2 in project myfaces by apache.

the class ActionListenerImpl method processAction.

@Override
public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
    FacesContext facesContext = actionEvent.getFacesContext();
    Application application = facesContext.getApplication();
    UIComponent component = actionEvent.getComponent();
    MethodExpression methodExpression = null;
    String fromAction = null;
    String outcome = null;
    if (component instanceof ActionSource2) {
        // Must be an instance of ActionSource2, so don't look on action if the actionExpression is set
        methodExpression = ((ActionSource2) component).getActionExpression();
    }
    if (methodExpression != null) {
        fromAction = methodExpression.getExpressionString();
        Object objOutcome = methodExpression.invoke(facesContext.getELContext(), null);
        if (objOutcome != null) {
            outcome = objOutcome.toString();
        }
    }
    UIViewRoot root = facesContext.getViewRoot();
    ViewPoolProcessor processor = ViewPoolProcessor.getInstance(facesContext);
    ViewPool pool = (processor != null) ? processor.getViewPool(facesContext, root) : null;
    if (pool != null && pool.isDeferredNavigationEnabled() && processor.isViewPoolStrategyAllowedForThisView(facesContext, root) && (PhaseId.INVOKE_APPLICATION.equals(facesContext.getCurrentPhaseId()) || PhaseId.APPLY_REQUEST_VALUES.equals(facesContext.getCurrentPhaseId()))) {
        NavigationHandler navigationHandler = application.getNavigationHandler();
        if (navigationHandler instanceof ConfigurableNavigationHandler) {
            NavigationCase navigationCase = ((ConfigurableNavigationHandler) navigationHandler).getNavigationCase(facesContext, fromAction, outcome);
            if (navigationCase != null) {
                // Deferred invoke navigation. The first one wins
                if (!facesContext.getAttributes().containsKey(ViewPoolProcessor.INVOKE_DEFERRED_NAVIGATION)) {
                    String toFlowDocumentId = (component != null) ? (String) component.getAttributes().get(ActionListener.TO_FLOW_DOCUMENT_ID_ATTR_NAME) : null;
                    if (toFlowDocumentId != null) {
                        facesContext.getAttributes().put(ViewPoolProcessor.INVOKE_DEFERRED_NAVIGATION, new Object[] { fromAction, outcome, toFlowDocumentId });
                    } else {
                        facesContext.getAttributes().put(ViewPoolProcessor.INVOKE_DEFERRED_NAVIGATION, new Object[] { fromAction, outcome });
                    }
                }
            }
        }
    } else {
        NavigationHandler navigationHandler = application.getNavigationHandler();
        String toFlowDocumentId = (component != null) ? (String) component.getAttributes().get(ActionListener.TO_FLOW_DOCUMENT_ID_ATTR_NAME) : null;
        if (toFlowDocumentId != null) {
            navigationHandler.handleNavigation(facesContext, fromAction, outcome, toFlowDocumentId);
        } else {
            navigationHandler.handleNavigation(facesContext, fromAction, outcome);
        }
        // Render Response if needed
        facesContext.renderResponse();
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) UIComponent(jakarta.faces.component.UIComponent) ViewPoolProcessor(org.apache.myfaces.view.facelets.ViewPoolProcessor) ConfigurableNavigationHandler(jakarta.faces.application.ConfigurableNavigationHandler) NavigationHandler(jakarta.faces.application.NavigationHandler) MethodExpression(jakarta.el.MethodExpression) ConfigurableNavigationHandler(jakarta.faces.application.ConfigurableNavigationHandler) NavigationCase(jakarta.faces.application.NavigationCase) ActionSource2(jakarta.faces.component.ActionSource2) Application(jakarta.faces.application.Application) UIViewRoot(jakarta.faces.component.UIViewRoot) ViewPool(org.apache.myfaces.view.facelets.pool.ViewPool)

Aggregations

ActionSource2 (jakarta.faces.component.ActionSource2)14 MethodExpression (jakarta.el.MethodExpression)8 PrintWriter (java.io.PrintWriter)7 FacesContext (jakarta.faces.context.FacesContext)4 MethodExpressionActionListener (jakarta.faces.event.MethodExpressionActionListener)4 ELContext (jakarta.el.ELContext)3 UIComponent (jakarta.faces.component.UIComponent)3 ActionListener (jakarta.faces.event.ActionListener)3 ExpressionFactory (jakarta.el.ExpressionFactory)2 ValueExpression (jakarta.el.ValueExpression)2 ActionSource (jakarta.faces.component.ActionSource)2 EditableValueHolder (jakarta.faces.component.EditableValueHolder)2 UIViewRoot (jakarta.faces.component.UIViewRoot)2 FacesException (jakarta.faces.FacesException)1 Application (jakarta.faces.application.Application)1 ConfigurableNavigationHandler (jakarta.faces.application.ConfigurableNavigationHandler)1 NavigationCase (jakarta.faces.application.NavigationCase)1 NavigationHandler (jakarta.faces.application.NavigationHandler)1 ValueHolder (jakarta.faces.component.ValueHolder)1 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)1