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

With javatemplate, dynamic attribute value evaluates to expression text if null

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.12
    • 2.3.15
    • None

    Description

      When using javatemplate plugin, if a dynamic attribute has an expression value that evaluates to null, the full expression text is displayed instead of just an empty string.

      Example:

      <s:textfield placeholder="%{null}" name="%{null}"/> 
      

      will output

      <input type="text" placeholder="%{null}" name=""/> 
      

      in the HTML.

      By debugging, I found it comes from AbstractUITag.setDynamicAttribute():

      dynamicAttributes.put(localName, String.valueOf(ObjectUtils.defaultIfNull(findValue(value.toString()), value)));
      

      That problem does not occur with FTL themes as dynamic-attributes.ftl uses TextParseUtil.translateVariables() which does what the doc says ("If an item cannot be found on the stack (null is returned), then the entire variable %{...} is not displayed, just as if the item was on the stack but returned an empty string.").

      Suggested fix #1

      Change org.apache.struts2.views.java.simple.DynamicAttributesHandler.start() so that it does the same than dynamic-attributes.ftl:

      DynamicAttributesHandler.java
          @Override
          public void start(String name, Attributes a) throws IOException {
      	Map<String, String> dynamicAttributes = (Map<String, String>) context.getParameters().get("dynamicAttributes");
      	for (String key : dynamicAttributes.keySet())
      		a.put(key, TextParseUtil.translateVariables(dynamicAttributes.get(key), context.getStack()));
              super.start(name, a);
          }
      

      Suggested fix #2

      Or change org.apache.struts2.views.jsp.ui.AbstractUITag.setDynamicAttribute() so that it returns an empty string if the expression evaluates to null:

      AbstractUITag.java
          public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
              if (ComponentUtils.altSyntax(getStack()) && ComponentUtils.isExpression(value)) {
                  dynamicAttributes.put(localName, String.valueOf(ObjectUtils.defaultIfNull(findValue(value.toString()), "")));
              } else {
                  dynamicAttributes.put(localName, value);
              }
          }
      

      (but I have no idea on the possible side effects it could produce).

      Attachments

        1. placeholder.zip
          3 kB
          Walid Ghafir

        Activity

          People

            lukaszlenart Lukasz Lenart
            w.ghafir@ictjob.be Walid Ghafir
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: