Struts 2
  1. Struts 2
  2. WW-3571

Anchor tag is providing a default value for href attribute when it should not

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.1.8, 2.2.1.1
    • Fix Version/s: 3.0
    • Component/s: None
    • Labels:
      None

      Description

      The Anchor tag is providing a default value for href attribute when it should not.

      Previous versions of Struts2 did not do this, and in fact the documentation for the tag also says there is no default value.

      However it is placing a default value of the current URL, rather than leaving the href attribute off altogether. (it is legal per HTML to not have an href attribute, and in fact we had existing code that was trying to purposely leave it off, but now instead of being left off, it is rendered with the current URL).

      I believe the problem was introduced with the changes related to WW-3037. This block of code (from Anchor.java) precisely:

          @Override
          protected void evaluateExtraParams() {
              super.evaluateExtraParams();
      
              if (href != null)
                  addParameter("href", ensureAttributeSafelyNotEscaped(findString(href)));
              else {
                  //no href, build it from URL attributes
                  StringWriter sw = new StringWriter();
                  urlRenderer.beforeRenderUrl(urlProvider);
                  urlRenderer.renderUrl(sw, urlProvider);
                  String builtHref = sw.toString();
                  if (StringUtils.isNotEmpty(builtHref))
                      addParameter("href", ensureAttributeSafelyNotEscaped(builtHref));
              }
          }
      

      To preserve documented and previous functionality of allowing href to not be rendered in the tag, the entire "else" block should be removed (of course that significantly negates WW-3037 - which apparently didn't think about the fact that it is legit to not have an href for the anchor).

        Activity

        Hide
        James House added a comment -

        A lame work-around for this issue is to visit all of your anchor tags that look like this:

        <s:a onclick="...."/>

        And change them to this:

        <s:a href="" onclick="...."/>

        (In other words, explicitly set the value of the href to an empty string).

        Show
        James House added a comment - A lame work-around for this issue is to visit all of your anchor tags that look like this: <s:a onclick="...."/> And change them to this: <s:a href="" onclick="...."/> (In other words, explicitly set the value of the href to an empty string).
        Hide
        Maurizio Cucchiara added a comment -

        I would agree with you except that I myself use the default value of href, I find it very useful. Moreover if someone need an anchor tag without href attribute (hence without the url render facility) (s)he probably don't need to choose the struts tag library.
        With said that, I think we should find a way to allow the user to take advantage of the url render stuff and/or to leave undefined the href attribute.
        Any thought?

        Show
        Maurizio Cucchiara added a comment - I would agree with you except that I myself use the default value of href, I find it very useful. Moreover if someone need an anchor tag without href attribute (hence without the url render facility) (s)he probably don't need to choose the struts tag library. With said that, I think we should find a way to allow the user to take advantage of the url render stuff and/or to leave undefined the href attribute. Any thought?
        Hide
        James House added a comment -

        I don't have an immediate thought of how to redesign/re-write it such that it fills both needs, however I will put forth an argument that explains why someone would want to use the struts tag to render an anchor without an href: it's still useful to use expressions/pull property values from the action context when generating the values for other properties, such as onclick.

        Show
        James House added a comment - I don't have an immediate thought of how to redesign/re-write it such that it fills both needs, however I will put forth an argument that explains why someone would want to use the struts tag to render an anchor without an href: it's still useful to use expressions/pull property values from the action context when generating the values for other properties, such as onclick.
        Hide
        Jason Pyeron added a comment -

        I think the use of s:a without a href attribute is such a fringe case it could only be justified to either

        1: augment the test with an else if checking for the OGNL null literal, and then not using the attribute

        2: add an attribute noHref="true" defaults to false ( I don't like this)

        Show
        Jason Pyeron added a comment - I think the use of s:a without a href attribute is such a fringe case it could only be justified to either 1: augment the test with an else if checking for the OGNL null literal, and then not using the attribute 2: add an attribute noHref="true" defaults to false ( I don't like this)

          People

          • Assignee:
            Maurizio Cucchiara
            Reporter:
            James House
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:

              Development