4

I want to rewrite the URL for my Struts2 based application(currently in development environment). I have searched for it and found about Tuckey URL Rewrite and set it up in my project. Now i want my login url which is currently http://localhost:8080/MyProject/loadLogin.action (I am using Wildcard mapping in Struts2) to look like http://localhost:8080/login. Below is my configuration code:

struts.xml:

<action name="*Login" method="{1}" class="com.myproject.controller.LoginController"> 
            <result name="login" type="tiles">mylogin</result>
       </action>

web.xml:

 <filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
         <init-param>
                <param-name>logLevel</param-name>
                <param-value>DEBUG</param-value>
            </init-param>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
      </filter-class>
   </filter>
   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

Now here's the urlrewrite.xml which has the rule and I do not know if I have configured the rule correctly:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
        "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<!--

    Configuration file for UrlRewriteFilter
    http://www.tuckey.org/urlrewrite/

-->
<urlrewrite>

       <rule>
            <from>^/*</from>
            <to type="redirect">/login</to>
        </rule>


</urlrewrite>
Roman C
  • 49,761
  • 33
  • 66
  • 176
Hasan K
  • 610
  • 8
  • 20
  • Why do you want to use urlrewrite? To remove `/MyProject`? -> Just deploy your app to root. To change `loadLogin.action`? -> Just rename your action. – Aleksandr M Apr 12 '16 at 12:00
  • @AleksandrM About your second suggestion of renaming the action, I am using wildcard mapping where load() is the method in my LoginController which displays the login page, so how can i rename it to get the same result. Thanks for your reply though.. – Hasan K Apr 12 '16 at 19:08
  • Why wildcard? What else you are mapping to login controller? Create action w/o wildcard. – Aleksandr M Apr 13 '16 at 08:13
  • @hasan i have the same question, Alek please help – Ravi Rajput Apr 13 '16 at 09:49
  • @Aleksandr M, how do I remove that ".action" thing from my login page. – Ravi Rajput Apr 13 '16 at 09:50
  • Actually with the default conf it is working w/o `.action`. See http://stackoverflow.com/q/12607075/1700321. – Aleksandr M Apr 13 '16 at 09:55
  • What are you trying to do? If you rewrite URL Struts2 will invoke the action mapper to map the action and it doesn't find the action config for this url. – Roman C Apr 13 '16 at 11:54
  • @AleksandrM I have other methods too for redirecting the user to the home page, performing logout(invalidating the session) etc in my login controller. So you suggest i have a separate controller for this functionality alone and move the others to separate actions? – Hasan K Apr 13 '16 at 11:58
  • 1
    @RomanC I want my url's not to contain the actual Project and action names. They have to be masked. I referred one of the links here : http://stackoverflow.com/questions/25256628/how-can-url-rewriting-in-struts2-x where you suggested yo use URL rewrite techniques. Thanks – Hasan K Apr 13 '16 at 12:04
  • @HasanKagalwala No, there is `method` attribute in `action` tag. – Aleksandr M Apr 13 '16 at 12:20
  • @HasanK I love this answer, especially the link where they talk about Norwegian Blue Parrot :D But I don't like the action name `login` hope it's not related to the form based authentication. – Roman C Apr 13 '16 at 14:08

1 Answers1

2

To rewrite to this URL http://localhost:8080/login you have to deploy urlrewrite filter to the root context. But the application is running at the /MyProject context. You can't have both filters in the same application deployed to the same context to perform desired.

How rewrite URL works: first you access the URL that you want to show, then urlrewrite filter searches the rules and if it find the match the rule is executed and request is forwarded (by default) to the new URL which is hidden for the user.

If you use type="redirect" in the <to> tag the new URL will show up, because it means

Requests matching the "conditions" and the "from" for this rule will be HTTP redirected. This is the same a doing: HttpServletResponse.sendRedirect([to value]))

To rewrite URL you should use this rule deployed to the app at the root context

<rule>
  <from>^/login$</from>
  <to type="redirect">/MyProject/loadLogin.action</to>
</rule>
Roman C
  • 49,761
  • 33
  • 66
  • 176