Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top