2

In struts2 I am writing an app where I need to make sure that the url redirection works the same whether or not there is a trailing slash at the end.

E.g. example.com/app should behave same way as if user entered example.com/app/. Currently I changed mapping in struts.xml like so -

<struts>
    <package name="default" namespace="/" extends="secure">
        <interceptors> ... <interceptors>

        <action name="app">
            <result type="redirectAction">/app/</result>
        </action>
    </package>
</struts>

and

<struts>
    <package name="app" namespace="/app" extends="secure">
        <interceptors> ... <interceptors>

        <action name="" class="com.example.action.app.LoginAction" method="display">
        <interceptor-ref name="store">
            <param name="operationMode">RETRIEVE</param>
        </interceptor-ref>
        <interceptor-ref name="basic" />

        <result type="redirectAction">home</result>
        <result name="display">/jsp/base/content/login/loginForm.jsp</result>
    </action> 
</package>
</struts>

But this seems hackish since if I go to example.com/app it will show example.com//app/.html in the URL.

Any help appreciated.

Chantz
  • 5,883
  • 10
  • 56
  • 79
  • example.com/app/ it will look for the action app/ we can add slash to action name but in that case example.com/app will not work. – MohanaRao SV Apr 02 '12 at 17:53
  • What is the purpose of such a requirement? – Quaternion Apr 02 '12 at 18:00
  • 3
    Personally I would write all my urls with out the trailing slash... and then I would use something external to the application to rewrite urls as appropriate, perhaps iptables could determine if there is a trailing slash and if so always strip it. – Quaternion Apr 02 '12 at 18:02
  • @Chantz: i am with Quaternion – Umesh Awasthi Apr 02 '12 at 18:36
  • 1
    As suggested above follow without tailing slash. And override StrutsPrepareAndExecuteFilter one of the filter job is from the url it has to identify the namespace and action name and invoke respective action. So here remove tailing slash from url. – MohanaRao SV Apr 02 '12 at 19:01
  • @Quaternion Hmm. Ok. But how would I write a url without trailing slash when I am creating a package with base namespace as "/app"? The only thing I could think of is in the parent package I create package with the action with name "app". And then doing what you have mentioned. – Chantz Apr 02 '12 at 19:24
  • 1
    In namespace "/" you have an action called app. That is all there is to it to invoke CONTEXT_ROOT/app (that is what struts2 expects), you don't ever expect to see a "/" on the end of the url, so you want to find a method that parses the url before struts2 resolves the mapping. What you have described only requires something to remove a trailing "/" if it exists. I'd look to iptables because I've used it before or some other url rewriter... Mahana would keep it all part of the web app and use a filter, methods differ but the effect is the same. – Quaternion Apr 02 '12 at 19:57
  • Bah, well you could expect a slash on in the action... but be consistent, is the point. It seems like a few less characters in the long run to avoid putting slashes on actions and removing them if they are found on the url vs always putting them in actions and adding a slash if it is missing from the url. – Quaternion Apr 02 '12 at 20:04
  • @Chantz If it isn't too much trouble, if you've decided (found) a particular solution jot down the method you used as an answer =) You might get a couple up votes. – Quaternion Apr 03 '12 at 05:30
  • @Quaternion Ok. I think I will go for the StrutsPrepareAndExecuteFilter method. But I would accept the answer you have provided as well. If you can just copy/paste your comment I will go ahead and accept it. – Chantz Apr 03 '12 at 17:37

1 Answers1

0

Answer was derived in comments under the answer.

Quaternion:

Personally I would write all my urls with out the trailing slash... and then I would use something external to the application to rewrite urls as appropriate, perhaps iptables could determine if there is a trailing slash and if so always strip it.

Mohana Rao SV:

As suggested above follow without tailing slash. And override StrutsPrepareAndExecuteFilter one of the filter job is from the url it has to identify the namespace and action name and invoke respective action. So here remove tailing slash from url.

Quaternion:

In namespace "/" you have an action called app. That is all there is to it to invoke CONTEXT_ROOT/app (that is what struts2 expects), you don't ever expect to see a "/" on the end of the url, so you want to find a method that parses the url before struts2 resolves the mapping. What you have described only requires something to remove a trailing "/" if it exists. I'd look to iptables because I've used it before or some other url rewriter... Mahana would keep it all part of the web app and use a filter, methods differ but the effect is the same.

Quaternion
  • 10,380
  • 6
  • 51
  • 102