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

Still can't pass parameters with dashes to tags

    XMLWordPrintableJSON

Details

    Description

      The latest freemarker now supports dashes in attribute names, so I can write something like:

      <@s.form name="sendToPhone" data\-ajax="false">
      </@s.form>
      

      Unfortunately, the parameters are set using ognl internally, so it blows up with an error like:

      Caused by: ognl.InappropriateExpressionException: Inappropriate OGNL expression: data - ajax
              at ognl.SimpleNode.setValueBody(SimpleNode.java:312)
              at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
              at ognl.SimpleNode.setValue(SimpleNode.java:301)
              at ognl.Ognl.setValue(Ognl.java:737)
              at com.opensymphony.xwork2.ognl.OgnlUtil$1.execute(OgnlUtil.java:287)
              at com.opensymphony.xwork2.ognl.OgnlUtil$1.execute(OgnlUtil.java:282)
              at com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
              at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:282)
      

      I think there is a simple solution, which is to send any parameters with an dash directly to the parameters map like so:

      Component.java
          /**
           * Pushes this component's parameter Map as well as the component itself on to the stack
           * and then copies the supplied parameters over. Because the component's parameter Map is
           * pushed before the component itself, any key-value pair that can't be assigned to component
           * will be set in the parameters Map.
           *
           * @param params  the parameters to copy.
           */
          public void copyParams(Map params) {
              stack.push(parameters);
              stack.push(this);
              try {
                  for (Object o : params.entrySet()) {
                      Map.Entry entry = (Map.Entry) o;
                      String key = (String) entry.getKey();
                      
                      if (key.indexOf('-') >= 0) {
                          // UI component attributes may contain hypens (e.g. data-ajax), but ognl
                          // can't handle that, and there can't be a component property with a hypen
                          // so into the parameters map it goes.
                          parameters.put(key, entry.getValue());
                      } else {
                          stack.setValue(key, entry.getValue());
                      }
                  }
              } finally {
                  stack.pop();
                  stack.pop();
              }
          }
      

      Hoping this can make it into 2.3.24, thanks!

      Attachments

        Activity

          People

            lukaszlenart Lukasz Lenart
            perfnorm Jasper Rosenberg
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: