View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.application;
20  
21  import javax.el.MethodExpression;
22  import javax.faces.application.Application;
23  import javax.faces.application.NavigationHandler;
24  import javax.faces.component.ActionSource;
25  import javax.faces.component.ActionSource2;
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.el.MethodBinding;
29  import javax.faces.event.AbortProcessingException;
30  import javax.faces.event.ActionEvent;
31  import javax.faces.event.ActionListener;
32  
33  
34  /**
35   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
36   * @author Anton Koinov
37   * @version $Revision: 1341540 $ $Date: 2012-05-22 10:47:08 -0500 (Tue, 22 May 2012) $
38   */
39  public class ActionListenerImpl implements ActionListener
40  {
41      public void processAction(ActionEvent actionEvent) throws AbortProcessingException
42      {
43          FacesContext facesContext = FacesContext.getCurrentInstance();
44          Application application = facesContext.getApplication();
45          UIComponent component = actionEvent.getComponent();
46          
47          MethodExpression methodExpression = null;
48          MethodBinding methodBinding = null;
49          
50          String fromAction = null;
51          String outcome = null;
52          
53          if (component instanceof ActionSource2)
54          {
55              // Must be an instance of ActionSource2, so don't look on action if the actionExpression is set 
56              methodExpression = ((ActionSource2) component).getActionExpression();            
57          }
58          if (methodExpression == null && component instanceof ActionSource)
59          {
60              // Backwards compatibility for pre-1.2.
61              methodBinding = ((ActionSource) component).getAction();
62          }
63          
64          if (methodExpression != null)
65          {
66              fromAction = methodExpression.getExpressionString();
67  
68              Object objOutcome = methodExpression.invoke(facesContext.getELContext(), null);
69              if (objOutcome != null)
70              {
71                  outcome = objOutcome.toString();
72              }
73              
74          }
75          
76          else if (methodBinding != null)
77          {
78              fromAction = methodBinding.getExpressionString();
79              Object objOutcome = methodBinding.invoke(facesContext, null);
80  
81              if (objOutcome != null)
82              {
83                  outcome = objOutcome.toString();
84              }
85  
86          }
87          
88          NavigationHandler navigationHandler = application.getNavigationHandler();
89          navigationHandler.handleNavigation(facesContext, fromAction, outcome);
90          //Render Response if needed
91          facesContext.renderResponse();
92  
93      }
94  }