1

I tried to include dot . in a dynamic URL pattern in Struts 2 but it is not working

My Struts 2 configuration for dynamic URL is as follows

<struts>
<constant name="struts.enable.SlashesInActionNames" value="true"/>
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.patternMatcher" value="regex" />
<package name="api" extends="struts-default" namespace="/">
    <action name="api/v1/{sid}/function/{id}/version/{ver}" class="com.test.Main" method="test">
        <param name="sid">{sid}</param>
        <param name="id">{id}</param>
        <param name="ver">{ver}</param>
    </action>
</package>

if I give trigger a URL without any dot in URL then it is working

/api/v1/test/function/1234/version/1

but if I use it with dot as follows then it is giving 404

/api/v1/test/function/1234/version/1.1

Why is it not working? How to make it work?

Roman C
  • 49,761
  • 33
  • 66
  • 176
mrrk
  • 11
  • 2
  • I didn't use it myself butthe JavaDoc on `RegexPatternMatcher` says: "The regular expressions can be in the form {FIELD_NAME} or {FIELD_NAME:REGULAR_EXPRESSION}." - Thus you could try `{ver:\d+(\.\d+)?}`. – Thomas Sep 28 '17 at 14:07
  • Good one. The dot in the last section is tricky because usually it is used to denote action extension. You can create your own `ActionMapper` to handle this or put something extra after the `/{ver}` part. – Aleksandr M Sep 28 '17 at 18:53
  • You can also create an improvement request - https://issues.apache.org/jira/projects/WW. – Aleksandr M Sep 28 '17 at 18:54

1 Answers1

0

Dots . in URL means a file extension delimiter. While you are using data in the URL it should be encoded. The encoded value for the dot is %2E.

It's not Struts2 problem to use data with the parameters in the action name, but some characters if not properly encoded has special meaning. For detaild explanation of URL specification see RFC 1738.

Reserved:

Many URL schemes reserve certain characters for a special meaning: their appearance in the scheme-specific part of the URL has a
designated semantics. If the character corresponding to an octet is
reserved in a scheme, the octet must be encoded. The characters ";", "/", "?", ":", "@", "=" and "&" are the characters which may be
reserved for special meaning within a scheme. No other characters may be reserved within a scheme.

Usually a URL has the same interpretation when an octet is
represented by a character and when it encoded. However, this is not
true for reserved characters: encoding a character reserved for a
particular scheme may change the semantics of a URL.

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used
unencoded within a URL.

On the other hand, characters that are not required to be encoded
(including alphanumerics) may be encoded within the scheme-specific
part of a URL, as long as they are not being used for a reserved
purpose.

Roman C
  • 49,761
  • 33
  • 66
  • 176