Friday 29 January 2016

Configure Url rewrite filter to make url seo friendly in struts 2

UrlRewrite Filter


Configuring Rules

1. type="forward"
This is the same as doing:
RequestDispatcher rq = request.getRequestDispatcher([to value]);
rq.forward(request, response);

- Use if you want to have friendly url's / RESTful urls / transparently forward request,
i.e. from /abc to /xyz without the user noticing it

- Also need to add :
 <dispatcher>FORWARD</dispatcher>
 <dispatcher>REQUEST</dispatcher>
entries for all filters to the <filter-mapping/> section of your apps
web.xml. By default filters are only run for REQUEST (i.e. from an external
http request) not for forwarded (or included for that matter) requests.

2. type="permanent-redirect" (301)
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", [to value]);
(note, SC_MOVED_TEMPORARILY is HTTP status code 301)

3. type="temporary-redirect" (302)
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", [to value]);
(note, SC_MOVED_TEMPORARILY is HTTP status code 302)

4. Adding Request Parameters to from
- Add it as &amp; E.g.:
<to>/world.jsp?country=$1&amp;city=$2</to>

Dynamically Reload Config
Change in web.xml
 <!-- set the amount of seconds the conf file will be checked for reload
    can be a valid integer (0 denotes check every time,
          empty/not set denotes no reload check) -->
    <init-param>
    <param-name>confReloadCheckInterval</param-name>
    <param-value>60</param-value>
    </init-param>

Debugging
Change in web.xml
 <!-- sets up log level (will be logged to context log)
    can be: TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL, log4j, commons, sysout:{level} (ie, sysout:DEBUG)
          if you are having trouble using normal levels use sysout:DEBUG -->
    <init-param>
    <param-name>logLevel</param-name>
    <param-value>DEBUG</param-value>
    </init-param>

No comments:

Post a Comment