Discussion:
Struts2: How can I make a Filter redirecting to struts actions
O***@posten.se
2006-12-07 08:32:17 UTC
Permalink
Hi everybody,
I have experimented with a filter that triggers for keywords on the
request URL
and when found it should redirect to a struts action that processes the
keyword.

My dilemma is that I don't want to use:
response.sendRedirect("/some.action");
becoz redirect should be done internally in the servlet container, not
via browser.

I have tested:
request.getRequestDispatcher("/some.action").forward(request, response);
and it don't work. I guess becoz an action mapping is not a servlet, an
JSP page is.

Does anyone have an idea how to solve this? Any input welcome!

One approach could be to inherit:
org.apache.struts2.dispatcher.FilterDispatcher
and override doFilter() method and insert my keyword code. Kinda ugly
thou.

Regardings,
Ove Oldberg, Sweden.
Dariusz Wojtas
2006-12-07 08:38:47 UTC
Permalink
Hi,

If your filter is called before the struts filter, then what about
preparing your own HttpServletRequest instance (some wrapper) and
passing it deeper into doFilter(..)?
You could copy all properties of the original request but change the
URI (or whatever property is responsible for it).
The next filter/servlet would be fooled by this switch and their
calculations (ie. finding what action should be executed) would be
done on data prepared by you.

Best regards
Dariusz Wojtas, Poland
Post by O***@posten.se
Hi everybody,
I have experimented with a filter that triggers for keywords on the
request URL
and when found it should redirect to a struts action that processes the
keyword.
response.sendRedirect("/some.action");
becoz redirect should be done internally in the servlet container, not
via browser.
request.getRequestDispatcher("/some.action").forward(request, response);
and it don't work. I guess becoz an action mapping is not a servlet, an
JSP page is.
Does anyone have an idea how to solve this? Any input welcome!
org.apache.struts2.dispatcher.FilterDispatcher
and override doFilter() method and insert my keyword code. Kinda ugly
thou.
Regardings,
Ove Oldberg, Sweden.
O***@posten.se
2006-12-08 09:40:48 UTC
Permalink
Hi Dariusz,
Thanks for the input. After some coding and reading in the Struts 2.0.2 sourcecode I got this to work.

In general it seems that Struts2 uses two methods from the request to examine URI (in this order):

- request.getServletPath()
- request.getRequestURI()

The real trick is, as you suggested, to create my own instance of HttpServletRequest and implement set-methods for those two, manipulate the values and pass on the reference to filter.doFilter(req, res). The nice thing is that there is a javax.servlet.http.HttpServletRequestWrapper class that do a base implementation of the request. Extend that and implement setServletPath(String arg0) and setRequestURI(String arg0) methods.

Very nice!

------
Martin Gainty-
Thanks for your input to my question. As you understand of my answer above, a redirect from an JSP page is way to late in the request sequence. The thing with keywords is to implement Wiki functionallity into a normal Struts built website. If a client request http://somesite.com/keyword my filter ask the WikiEngine if it recognizes the keyword. If it does, I modify the request to my template.action instead. I store the Wiki keyword as an request attribute to be picked up and presented by the template. By this I can serve simple easy editable pages in Wiki format and use Struts for more advanced pages. An important goal is to hide the redirect for a web browser so when a user bookmarks a page, the url point to a keyword and not an ugly redirect.
------

Thanks again for the help!
Regards,
Ove Oldberg
-----Ursprungligt meddelande-----
Skickat: den 7 december 2006 09:39
Till: Struts Users Mailing List
Ämne: Re: Struts2: How can I make a Filter redirecting to
struts actions
Hi,
If your filter is called before the struts filter, then what about
preparing your own HttpServletRequest instance (some wrapper) and
passing it deeper into doFilter(..)?
You could copy all properties of the original request but change the
URI (or whatever property is responsible for it).
The next filter/servlet would be fooled by this switch and their
calculations (ie. finding what action should be executed) would be
done on data prepared by you.
Best regards
Dariusz Wojtas, Poland
Post by O***@posten.se
Hi everybody,
I have experimented with a filter that triggers for keywords on the
request URL
and when found it should redirect to a struts action that
processes the
Post by O***@posten.se
keyword.
response.sendRedirect("/some.action");
becoz redirect should be done internally in the servlet
container, not
Post by O***@posten.se
via browser.
request.getRequestDispatcher("/some.action").forward(request,
response);
Post by O***@posten.se
and it don't work. I guess becoz an action mapping is not a
servlet, an
Post by O***@posten.se
JSP page is.
Does anyone have an idea how to solve this? Any input welcome!
org.apache.struts2.dispatcher.FilterDispatcher
and override doFilter() method and insert my keyword code.
Kinda ugly
Post by O***@posten.se
thou.
Regardings,
Ove Oldberg, Sweden.
---------------------------------------------------------------------
Martin Gainty
2006-12-07 14:03:52 UTC
Permalink
Ove-

in your jsp
You can hook redirect-action to yout submit button as in
<a:submit name=&quot;redirect-action:someAction&quot; value=&quot;OK&quot;/>
http://struts.apache.org/WW/actionmapper.html

code
in your Action class execute method
mapping.findForward("someAction")

where struts.xml has
<action name="someAction" class="org.sverige.someAction">
<result>someAction.jsp</result>
</action>

bistå ?
Martin --
This e-mail communication and any attachments may contain confidential and privileged information for the use of the
designated recipients named above. If you are not the intended recipient, you are hereby notified that you have received
this communication in error and that any review, disclosure, dissemination, distribution or copying of it or its
contents
----- Original Message -----
From: <***@posten.se>
To: <***@struts.apache.org>
Sent: Thursday, December 07, 2006 3:32 AM
Subject: Struts2: How can I make a Filter redirecting to struts actions


Hi everybody,
I have experimented with a filter that triggers for keywords on the
request URL
and when found it should redirect to a struts action that processes the
keyword.

My dilemma is that I don't want to use:
response.sendRedirect("/some.action");
becoz redirect should be done internally in the servlet container, not
via browser.

I have tested:
request.getRequestDispatcher("/some.action").forward(request, response);
and it don't work. I guess becoz an action mapping is not a servlet, an
JSP page is.

Does anyone have an idea how to solve this? Any input welcome!

One approach could be to inherit:
org.apache.struts2.dispatcher.FilterDispatcher
and override doFilter() method and insert my keyword code. Kinda ugly
thou.

Regardings,
Ove Oldberg, Sweden.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@struts.apache.org
For additional commands, e-m
Martin Gainty
2006-12-10 14:26:55 UTC
Permalink
Hi Ove

struts2 recommended way to establish a forward when your Action class
returns "success" is
to set the type attribute to "dispatcher" as in this example which forwards
to SverigeAltid.jsp

<result name="success" type="dispatcher">
<param name="location">/SverigeAltid.jsp</param>
</result>

i found this at
http://struts.apache.org/2.x/docs/result-configuration.html

HTH,
Martin --

______________________________________________
Disclaimer and confidentiality note
Everything in this e-mail and any attachments relates to the official
business of Sender. This transmission is of a confidential nature and Sender
does not endorse distribution to any party other than intended recipient.
Sender does not necessarily endorse content contained within this
transmission.
Post by O***@posten.se
Hi everybody,
I have experimented with a filter that triggers for keywords on the
request URL
and when found it should redirect to a struts action that processes the
keyword.
response.sendRedirect("/some.action");
becoz redirect should be done internally in the servlet container, not
via browser.
request.getRequestDispatcher("/some.action").forward(request, response);
and it don't work. I guess becoz an action mapping is not a servlet, an
JSP page is.
Does anyone have an idea how to solve this? Any input welcome!
org.apache.struts2.dispatcher.FilterDispatcher
and override doFilter() method and insert my keyword code. Kinda ugly
thou.
Regardings,
Ove Oldberg, Sweden.
---------------------------------------------------------------------
_________________________________________________________________
View Athlete’s Collections with Live Search
http://sportmaps.live.com/index.html?source=hmemailtaglinenov06&FORM=MGAC01
Continue reading on narkive:
Loading...