Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-1415

Updated the StrutsVariableResolver to add OGNL support for resolving variable names

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.0, 2.0.1
    • Fix Version/s: 2.0.0
    • Component/s: Expression Language, Other
    • Labels:
      None
    • Environment:

      All

    • Flags:
      Patch

      Description

      Changed StrutsVariableResolver to first return the current Action if the Action name matches the requested variable name. Otherwise try the value stack to resolve the variable. Otherwise delegate to the original jsf variable resolver.

      This allows you to use things like the following with your JSF EL expressions:

      <h:outputText value="#

      {billingAction.billingActions[editId].baId}

      " />

      (Where "billingAction" is the name of your action, "billingActions" is a List property of the action, "editId" is a Long property of the action, "baId" is a property of a model object with in the List.)

      This will also work:

      <h:outputText value="#

      {billingActions[editId].baId}

      " />

      (Same as above but starting with a property of the action rather than the action itself.)

      org.apache.struts2.jsf.StrutsVariableResolver.java

      /*

      • $Id: TokenInterceptor.java 394468 2006-04-16 12:16:03Z tmjee $
        *
      • Copyright 2006 The Apache Software Foundation.
        *
      • Licensed under the Apache License, Version 2.0 (the "License");
      • you may not use this file except in compliance with the License.
      • You may obtain a copy of the License at
        *
      • http://www.apache.org/licenses/LICENSE-2.0
        *
      • Unless required by applicable law or agreed to in writing, software
      • distributed under the License is distributed on an "AS IS" BASIS,
      • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      • See the License for the specific language governing permissions and
      • limitations under the License.
        */
        package org.apache.struts2.jsf;

      import javax.faces.context.FacesContext;
      import javax.faces.el.EvaluationException;
      import javax.faces.el.VariableResolver;

      import com.opensymphony.xwork2.ActionContext;
      import com.opensymphony.xwork2.util.OgnlValueStack;

      /**

      • Will return a reference to the current action if the action name matches
      • the requested variable name. Otherwise it will attempt to resolve the
      • name from the value stack. Otherwise it will delegate to the original jsf
      • resolver.
        */
        public class StrutsVariableResolver extends VariableResolver {

      /** The original <code>VariableResolver</code> passed to our constructor. */
      private VariableResolver original = null;

      /** The variable name of our Struts action */
      private static final String STRUTS_VARIABLE_NAME = "action";

      /**

      • Constructor
      • @param original
      • Original resolver to delegate to.
        */
        public StrutsVariableResolver(VariableResolver original) { this.original = original; }

      /**

      • <p>
      • Will return a reference to the current action if the action name matches
      • the requested variable name. Otherwise it will attempt to resolve the
      • name from the value stack. Otherwise it will delegate to the original jsf
      • resolver.
      • </p>
      • @param name
      • Variable name to be resolved
        */
        public Object resolveVariable(FacesContext context, String name)
        throws EvaluationException {

      String actionName = ActionContext.getContext().getName();

      if(actionName.equals(name))

      { return ActionContext.getContext().getActionInvocation().getAction(); }

      Object obj = ActionContext.getContext().getValueStack().findValue(name);
      if (obj != null)

      { return obj; }

      else

      { return original.resolveVariable(context, name); }

      }

      }

        Attachments

        1. StrutsVariableResolver_diff.txt
          2 kB
          Christopher Waring

          Activity

            People

            • Assignee:
              mrdon Donald J. Brown
              Reporter:
              cwaring Christopher Waring
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: