Help needed in integrating UrlRewritingFilter with Struts 2.X & tiles

673 views
Skip to first unread message

vivek

unread,
May 8, 2012, 5:35:19 AM5/8/12
to UrlRewrite, pa...@tuckey.org
Dear Tech geeks,

Greetings of the day.

First of all heartiest accolades for such a great utility from this
newbie. This utility shall allow me exactly what i am trying to
achieve. However as of now i am unable to set up the UrlRewriteFilter
with my application.

I am running an web application on Tomcat (for testing) & WebSpehere
(for Production). The application is made on Struts 2.1.6 framework &
have integration with tiles 2.1.2.

The concern i am having is that i wish to rewrite the following url
<b>http://localhost:8080/myapp/musicLink.action</b>
to
<b>http://localhost:8080/myapp/music</b>

And

url <b>http://localhost:8080/myapp/musicLink.action?
param1=1212&param2=1313&param3=1414</b>
to
<b>http://localhost:8080/myapp/music/1212/1313/1414</b>

Below is my web.xml configuration
<code>
<listener>
<listener-class>com.ibm.vas.controller.impl.StartupListener</
listener-class>
</listener>

<context-param>
<param-
name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</
param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>

<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>sysout:DEBUG</param-value>
</init-param>
</filter>

<filter>
<filter-name>struts-prepare</filter-name>
<filter-
class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</
filter-class>
</filter>

<filter>
<filter-name>struts-execute</filter-name>
<filter-
class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</
filter-class>
</filter>

<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</
listener-class>
</listener>
</code>

My Urlrewrite Configuration
<code>
<urlrewrite>

<rule>
<from>^/music/([a-z0-9]+)/([a-z0-9]+)/([a-z0-9]+)$</from>
<to>/musicLink.action\?param1=$1&amp;param2=$2&amp;param3=$3</to>
</rule>

<outbound-rule>
<from>^/musicLink\.action\?param1=([a-z0-9]+)&amp;param2=([a-
z0-9]+)&amp;param3=([a-z0-9]+)$</from>
<to>/music/$1/$2/$3</to>
</outbound-rule>

</urlrewrite>
</code>

My Struts.xml Configuration
<code>
<struts>
<constant name="struts.action.extension" value="action" />
<package name="default" extends="struts-default">
<result-types>
<result-type name="tiles"
class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="musicLink" class="com.controller.MusicAction">
<result name="success" type="tiles">/music.tiles</result>
</action>
</code>

Can anyone please throw some light upon where am i doing wrong in this
following configuration or why am i unable to acheive my desired
rewritten url.

Any help in this regard shall be highly appreciated.

Kindly respond. Looking forward to read from you very soon.

Thanks & Regards
Vivek J

Wim De Smet

unread,
May 10, 2012, 4:27:22 AM5/10/12
to urlre...@googlegroups.com
Hi,

Could you try enabling debug logging and having a look at what is
actually going wrong? Are URLs not being rewritten, is struts handling
the results wrongly, are outbound URLs not working? It's quite hard to
just tell from the configuration what might or might not be the
problem...

regards,
Wim

vivek

unread,
May 14, 2012, 6:03:19 AM5/14/12
to UrlRewrite
Hi,

As per the Wim's statement & some more changes made, now i am able to
rewrite the url's with same configuration however i am in a great fix
now as the context root of my application is changed for all resources
like jsp's, Js, css etc...

Here is my current configuration
<urlrewrite use-query-string="true">
<rule>
<from>^/music/hindi$</from>
<to>/musicLink.action?param1=212001001&amp;param2=212001001001</to>
</rule>
</urlrewrite>

There are no outbound rules written bcoz as of now i just want to
redirect the address bar url to application specific Url not vice
versa.
I have tried with the below code but nothing works

<rule>
<from>\.(bmp|gif|jpeg|jpg|png|css|js|jsp|txt|pdf|doc|xls|xml|htm|html|
swf)$</from>
<to last="true">/$1</to>
</rule>

and

<rule match-type="regex">
<from>^/(css|html|images|js|pages|tiles|manifest)/(.*)</from>
<to last="true">/$1</to>
</rule>

Neither of this approach worked for me.

Wim De Smet

unread,
May 14, 2012, 8:33:54 AM5/14/12
to urlre...@googlegroups.com
On Mon, May 14, 2012 at 12:03 PM, vivek <er.jadi...@gmail.com> wrote:
> Hi,
>
> As per the Wim's statement & some more changes made, now i am able to
> rewrite the url's with same configuration however i am in a great fix
> now as the context root of my application is changed for all resources
> like jsp's, Js, css etc...
>
> Here is my current configuration
> <urlrewrite use-query-string="true">
>        <rule>
>                <from>^/music/hindi$</from>
>                <to>/musicLink.action?param1=212001001&amp;param2=212001001001</to>
>        </rule>
> </urlrewrite>

I assume this works?

> There are no outbound rules written bcoz as of now i just want to
> redirect the address bar url to application specific Url not vice
> versa.
> I have tried with the below code but nothing works
>
> <rule>
>        <from>\.(bmp|gif|jpeg|jpg|png|css|js|jsp|txt|pdf|doc|xls|xml|htm|html|
> swf)$</from>
>        <to last="true">/$1</to>
> </rule>

You are redirecting anything that ends in the named extension, for
example .bmp to the URL /bmp (that would become /yourcontext/bmp if
you're not in the root context). If you want to extract anything else
from the URL you should try to match it in the regex specifically. Or
use conditions.


>  and
>
> <rule match-type="regex">
>        <from>^/(css|html|images|js|pages|tiles|manifest)/(.*)</from>
>        <to last="true">/$1</to>
> </rule>

Again, this just redirects to /css, /html, ... Which is probably not
what you want. Maybe you should give some background on what you are
trying to do?

Regards,
Wim

Wim De Smet

unread,
May 14, 2012, 9:24:49 AM5/14/12
to UrlRewrite
Hi,

On Mon, May 14, 2012 at 2:56 PM, vivek <er.jadi...@gmail.com> wrote:
> @wim Thanks for replying. I really appreciate the pain you are taking
> for helping me out.
>
> Well here is some extra input to understand my concern
>
> I have a url like http://www.abc.com/myApp/music/hindi/newarrivals
> As soon as this url is fired from the browser i wish to get this url
> redirected to
> http://www.abc.com/myApp/musicLink.action?param1=112233&amp;param2=11223344
>

You indeed don't need to include query string for this, just make sure
to not terminate too soon, i.e. your rule:
<from>^/music/hindi$</from>

Should be
<from>^/music/hindi</from>
or
<from>^/music/hindi/newarrivals$</from>

Be aware that if you have both, only the first would match, since you
rewrite right after.

> The redirection happens perfectly but when it encounters any other
> resource like images, js, css which have relative paths in application
> it changes the context.

With the configuration as above, I simply don't see why this could be.
Are you sure tiles is encoding the URLs on the pages correctly? Is
there perhaps an outbound-rule you forgot about?

> First thing is the rule is appending the parameters for all requests
> (including static resources).
> May be this is because of setting use-query-string="true", but as soon
> as i remove this clause the code stops working.

It is probably because your urlrewritefilter is listening on FORWARD
and firing twice. Remove the mapping to the FORWARD dispatcher (for
urlrewritefilter, not the other ones).

> Also at the same time the the call that ought to be mpApp/tiles/
> base.jsp
> gets converted to music/tiles/base.jsp?
> param1=112233&amp;param2=11223344

I honestly don't see that in the inbound rules you gave, and since
there are not supposed to be any outbound rules, it should just work.

regards,
Wim

vivek

unread,
May 14, 2012, 9:50:14 AM5/14/12
to urlre...@googlegroups.com
Dear Wim,

I had removed the query string for this.
I have a concern though, lets say i have a url at my browser like http://www.abc.com/myApp/music/hindi/newarrivals/pop which will match with the rule 
<rule>
<from>^/music/hindi/newarrivals/pop$</from>
<to>/musicLink.action?param1=111111&amp;param2=22222&amp;param3=33333</to>
</rule>
then how am i suppose to match this Url http://www.abc.com/myApp/music/hindi or http://www.abc.com/myApp/music/hindi/newarrivals 
as the results yielded by these Url's will be entirely different from each other.

Also I had removed the UrlConfig to listen at Forward from web.xml.

Regarding Url Encoding at tiles i would like to share this info that the resources are included in header like

<link href="page_resource/css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="page_resource/js/jquery-1.4.2.min.js"></script>

The log traces are attached with this email for more information

Thanks
logs.txt
Message has been deleted

vivek

unread,
May 15, 2012, 10:30:30 AM5/15/12
to UrlRewrite, kro...@gmail.com
Hi guys,

Kindly respond.

Thanks

On May 14, 6:50 pm, vivek <er.jadiyavi...@gmail.com> wrote:
> Dear Wim,
>
> I had removed the query string for this.
> I have a concern though, lets say i have a url at my browser like *http://www.abc.com/myApp/music/hindi/newarrivals/pop*which will match with
> the rule
> <rule>
> <from>^/music/hindi/newarrivals/pop$</from>
> <to>/musicLink.action?param1=111111&amp;param2=22222&amp;param3=33333</to>
> </rule>
> then how am i suppose to match this Url *http://www.abc.com/myApp/music/hindi
> or **http://www.abc.com/myApp/music/hindi/newarrivals*
> as the results yielded by these Url's will be entirely different from each
> other.
>
> Also I had removed the UrlConfig to listen at Forward from web.xml.
>
> Regarding Url Encoding at tiles i would like to share this info that the
> resources are included in header like
>
> *<link href="page_resource/css/style.css" rel="stylesheet" type="text/css"
> />
> *
> *<script type="text/javascript"
> src="page_resource/js/jquery-1.4.2.min.js"></script>*
>
> The log traces are attached with this email for more information
>
> Thanks
>
>
>
>
>
>
>
> On Monday, May 14, 2012 6:54:49 PM UTC+5:30, Wim De Smet wrote:
>
> > Hi,
>
> > On Mon, May 14, 2012 at 2:56 PM, vivek <er.jadiyavi...@gmail.com> wrote:
> > > @wim Thanks for replying. I really appreciate the pain you are taking
> > > for helping me out.
>
> > > Well here is some extra input to understand my concern
>
> > > I have a url likehttp://www.abc.com/myApp/music/hindi/newarrivals
> > > As soon as this url is fired from the browser i wish to get this url
> > > redirected to
>
> >http://www.abc.com/myApp/musicLink.action?param1=112233&param2=11...<http://www.abc.com/myApp/musicLink.action?param1=112233¶m2=11223344>
>  logs.txt
> 55KViewDownload
Reply all
Reply to author
Forward
0 new messages