javaurl-rewritingstruts2friendly-urlaction-mapping

How to map a path parameters in URL to the action in Struts 2


We are using Struts 6.2 on Tomcat 9, and map all .action extensions to actions, for example save-user.action maps to an action correctly.

The web application needs to handle this path parameters ( path variables) ex: save-user.action/name/joe/age/20. As you can see the parameters are send via URL path. The caller is not a browser.

Is there any way that I can configure Struts to handle this URL and map it to correct action?


Solution

  • we are using convention-plugin, it seems that we should get the /my.action/param1/value1 url before struts filter. change it to valid url/my.action?param1=value1 and then let struts do the rest of jobs.

    It's not easy to do it yourself, fortunately there's a solution to use urlrewrite filter in front of struts filter. See Why URL rewriting is not working in Struts 2:

    You can rewrite any URL if you place Tuckey URL rewrite filter before the Struts2 filter in the web application deployment descriptor. Use this guide: Tuckey URLRewrite How-To.


    Then you add a rule to rewrite URL similar like this:

    <rule match-type="regex">  
      <from>/my.action/param1/(\w+)</from>
      <to>/my.action?param1=$1</to>
    </rule>