tags:

views:

542

answers:

2

How to redirect www.mysite.com/12345 to www.mysite.com/action?id=12345 using struts2 ?

A: 
<action name="12345">
   <result type="redirect-action">
        <param name="actionName">action</param>
        <param name="id">12345</param>
    </result>
</action>

UPDATE Ok. Based on the comment below.

I have managed something like this in this way in the past. Create a package in struts with a catch all action.

<package name="numbers">
    <action name="*" class="my.package.ActionClass" method="urlrewrite">
        <result type="redirect-action">
            <param name="actionName">${nextpage}</param>
            <param name="id">${id}</param>
        </result>
    </action>
</package>

Then in the urlrewrite method of the action class:

public String urlrewrite(){
     //parse the url and determine the ID and the next page
     nextpage = "action";
     id = "12345";
     return SUCCESS;
}

So in calling the action you will have to do like this:

http://www.mysite.com/numbers/12345.action

If you do not want a new package then you can do this in the default package.

Vincent Ramdhanie
I think he wants something that will work on all numerics under the root, not just 12345
wds
Thanks! But as http://stackoverflow.com/users/10098/wds said I want the shortest url that will work on all numerics under the root, such as https://bugs.eclipse.org/291547
Yau ML
A: 

I use URL rewriting to get these kind of flexible mappings working (though you could probably do it in struts proper, possibly with your own interceptor or something). There's a great little project, urlrewritefilter that gets the job done. In your URL rewriting configuration you'd have a rule like:

<rule>
  <from>^/(\d+)$</from>
  <to>/action?id=$1</to>
</rule>

Have a look at the manual to see if it is what you are looking for.

wds