View Javadoc

1   /*
2    * $Id: StrutsVariableResolver.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.struts2.jsf;
22  
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.EvaluationException;
25  import javax.faces.el.VariableResolver;
26  
27  import com.opensymphony.xwork2.util.OgnlValueStack;
28  import com.opensymphony.xwork2.ActionContext;
29  
30  /***
31   * Will return a reference to the current action if the action name matches the
32   * requested variable name. Otherwise it will attempt to resolve the name from
33   * the value stack. Otherwise it will delegate to the original jsf resolver.
34   */
35  public class StrutsVariableResolver extends VariableResolver {
36  
37      /*** The original <code>VariableResolver</code> passed to our constructor. */
38      private VariableResolver original = null;
39  
40      /*** The variable name of our Struts action */
41      private static final String STRUTS_VARIABLE_NAME = "action";
42  
43      /***
44       * Constructor
45       *
46       * @param original
47       *            Original resolver to delegate to.
48       */
49      public StrutsVariableResolver(VariableResolver original) {
50  
51          this.original = original;
52  
53      }
54  
55      /***
56       * <p>
57       * Will return a reference to the current action if the action name matches
58       * the requested variable name. Otherwise it will attempt to resolve the
59       * name from the value stack. Otherwise it will delegate to the original jsf
60       * resolver.
61       * </p>
62       *
63       * @param name
64       *            Variable name to be resolved
65       */
66      public Object resolveVariable(FacesContext context, String name)
67              throws EvaluationException {
68  
69          if (STRUTS_VARIABLE_NAME.equals(name)) {
70              return ActionContext.getContext().getActionInvocation().getAction();
71          }
72  
73          Object obj = ActionContext.getContext().getValueStack().findValue(name);
74          if (obj != null) {
75              return obj;
76          } else {
77              return original.resolveVariable(context, name);
78          }
79  
80      }
81  
82  }