Struts 2 - Mapped Actions working with any URL
Asked Answered
F

1

5

I am creating a web app with Struts2, and I am having an issue with the mapped actions working with any url.

In my struts.xml file, I have configured a package with a namespace of "/registration" with a few actions, with the main one being "register". The context root of my app is "app/test".

To access the registration form, I can go to "localhost:8080/app/test/registration/register.action" and it loads up my form and works great.

However, if anything is added to the URL after the namespace, such as "localhost:8080/app/test/registration/arbitrary/text/here/register.action", the form is still loaded up.

I would like to prevent this from happening, so that you can only access the form the proper URL. I have tried many different configuration options in struts.xml and web.xml to no avail, and I cannot find knowledge on this issue easily on the web.

Any help will be appreciated, thanks!

struts.xml

<struts>
    <package name="myPackage" namespace="/registration" extends="struts-default">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
        </result-types>
        <action name="register" class="edu.uconn.test.action.RegistrationAction" method="input">
            <result name="input" type="tiles">/register.tiles</result>
        </action>
    </package>
</struts>
Fogbound answered 7/12, 2011 at 23:34 Comment(0)
R
7

Set the struts.mapper.alwaysSelectFullNamespace constant to true:

<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />

This may have unintended consequences when leveraging S2's support for arbitrary parameters in URLs (e.g., wildcarding, regex pattern matching).

Representationalism answered 8/12, 2011 at 1:6 Comment(5)
Thanks Dave! Funny thing is I gave the struts configuration documentation a few glance overs, and completely missed this. Much appreciated regardless!Fogbound
@A.Cusano It's a bit buried; I've made a note to mention it in the next release somewhere, so thanks :)Representationalism
@DaveNewton: That's the main problem with struts2 have hidden documentation :)Reeta
honestly i am planning to do it :)Reeta
I have encountered the same problem as A. Cusano, i.e. the "arbitrary/text/here" problem. I try to add @DaveNewton 's solution, to add the constant in struts.xml, but the problem is remained. However, I add this < constant name="struts.action.excludePattern" value="/[^/]*/.*"/> constant to struts.xml, it is work and show the 404 page. Is it a proper solution, and any clue for preventing the struts.mapper.alwaysSelectFullNamespace solution work?Oxycephaly

© 2022 - 2024 — McMap. All rights reserved.