public class UriMappingServletDispatcher
extends ServletDispatcher
This is a custom servlet dispatcher whose functionality is loosely based off
of
com.opensymphony.webwork.dispatcher.CoolUriServletDispatcher.
The purpose of this dispatcher is to allow for urls where the http parameters
are not passed in as ?key=value&key=value pairs but as /key/value/key/value
pairs. Some call these pretty or cool urls.
This servlet differs from the webwork included CoolUriServletDispatcher in
that it:
- doesn't throw a String.subtring index out of bounds exception on my
deployment ;)
- can handle conventional actionName.action (or any other extension) url
formats
- assumes a format of actionName/value to set the id property on the
action and NOT to set the id property on the actionName property of the
action
- can simultaneously handle both pretty url parameters (/key1/value1...)
and conventional parameters (/key1/value1?key2=value2)
Usage:
- Determine the url path you want to use for urls that will utilize the
pretty format. I find that using: context/link/actionName works
well. In this case "/link" is extra url path that will signify a pretty url.
If you will always be using a pretty url format and never the standard
context/actionName.action format, this step is unecessary. This is only
useful when both formats should be supported and helps in the trickiness that
is the servlet and web.xml request mapping.
- Set this servlet to be your webwork dispatcher servlet in web.xml and
add an init-param signifying the url portion that you've added to your pretty
urls (if any - again, if you are only using pretty formats, this is
unecessary):
<servlet>
<servlet-name>webwork</servlet-name>
<servlet-class>your.package.UriMappingServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>ignoreURIPortion</param-name>
<param-value><b>/link</b></param-value>
</init-param>
</servlet>
- Set up your request mappings. If you are using both conventional and
pretty url formats then your mappings in web.xml will look something like
this:
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern><b>/link/*</b></url-pattern>
</servlet-mapping>
If you are only using pretty url formats then your mappings in web.xml
will look something like more like this (or whatever will map all requests to
the servlet)
<servlet-mapping>
<servlet-name>webwork</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Once your web.xml is set up, you should be good to go. Now for the
functionality you can expect to see:
- Conventional requests will be handled as always:
http://HOST/CONTEXT/ACTION_NAME.action?id=1&key1=value1
will be handled
by the ACTION_NAME action with the given parameters - Pretty urls in this format (when using both conventional and pretty url
formats):
http://HOST/CONTEXT/link/ACTION_NAME/id/1/key1/value1
will be handled by the ACTION_NAME action with parameters (id=1, key1=value1) - Pretty urls in this format (when using both conventional and pretty url
formats):
http://HOST/CONTEXT/link/ACTION_NAME/id/1?key1=value1
will be handled by the ACTION_NAME action with parameters (id=1, key1=value1) - Pretty urls in this format (when using both conventional and pretty url
formats):
http://HOST/CONTEXT/link/ACTION_NAME/1?key1=value1
will be handled by the ACTION_NAME action with parameters (id=1, key1=value1) - When only the pretty url format is used and all requests are mapped to
this dispatcher servlet, the /link portion can be removed and you
will see the same functionality:
http://HOST/CONTEXT/ACTION_NAME/id/1/key1/value1
will be handled by the
ACTION_NAME action with parameters (id=1, key1=value1)
Same goes for:
http://HOST/CONTEXT/ACTION_NAME/1/key1/value1
protected String | getAdjustedString(String adjustable)- Get the portion of the request URI that should be analyzed for action
names, parameters etc...
|
protected String | getUriMappedActionName(HttpServletRequest request)- The action could not be determined when the vanilla action.xxx format was
used - revert to other more manual means to determine the action name.
|
void | init(ServletConfig servletConfig)
|
void | service(HttpServletRequest request, HttpServletResponse response)
|
protected void | serviceUriMappedRequest(HttpServletRequest request, HttpServletResponse response, String actionName)- This request has been identified as not being a vanilla request, so
handle it as though it's a URI mapped request.
|
protected void | serviceVanillaRequest(HttpServletRequest request, HttpServletResponse response, String actionName)- This request has been identified as being a vanilla request:
actionName.action?key=value&key=value...., handle it appropriately
|
URI_IGNORE_CONFIG_KEY
public static final String URI_IGNORE_CONFIG_KEY
getAdjustedString
protected String getAdjustedString(String adjustable)
Get the portion of the request URI that should be analyzed for action
names, parameters etc... This is basically the URI minus the text
specified in the "ignoreURIPortion" servlet init param. (only takes out
the first occurance of the string - override for other functionality)
getUriMappedActionName
protected String getUriMappedActionName(HttpServletRequest request)
The action could not be determined when the vanilla action.xxx format was
used - revert to other more manual means to determine the action name.
init
public void init(ServletConfig servletConfig)
throws ServletException
service
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException
serviceUriMappedRequest
protected void serviceUriMappedRequest(HttpServletRequest request,
HttpServletResponse response,
String actionName) This request has been identified as not being a vanilla request, so
handle it as though it's a URI mapped request.
request - response - actionName -
serviceVanillaRequest
protected void serviceVanillaRequest(HttpServletRequest request,
HttpServletResponse response,
String actionName) This request has been identified as being a vanilla request:
actionName.action?key=value&key=value...., handle it appropriately
request - response - actionName -