Java Reference
In-Depth Information
For example, instead of this:
/ContactList.action?view=&contact=7
we might have this:
/action/contact_list/view/7
Notice the “cleanliness” of the second URL compared to the first: no
extension, no question mark, no ampersand, no equal sign. The first
thing we need to do to use these clean URLs is change the mapping of
the Stripes dispatcher servlet in the
web.xml
file from a suffix, such as
.
action
, to a prefix, such as
/action
:
<servlet-mapping>
<servlet-name>
DispatcherServlet
</servlet-name>
<url-pattern>
/action/
*
</url-pattern>
</servlet-mapping>
With this mapping, Stripes handles all URLs with the
/action
prefix.
Next, we'll make the corresponding changes in
NameBasedActionResolver
by overriding
getBindingSuffix
( ) and
getUrlBinding
( ). At a minimum,
get-
BindingSuffix
( ) must return an empty string, and
getUrlBinding
( ) must call
super.getUrlBinding
( ) and add the
"/action"
prefix to the result. Just for
fun, we'll also convert the URL binding to all lowercase with under-
scores:
package
stripesbook.ext;
public class
MyActionResolver
extends
NameBasedActionResolver {
@Override
protected
String getBindingSuffix() {
return
"";
}
@Override
protected
String getUrlBinding(String actionBeanName) {
String result =
super
.getUrlBinding(actionBeanName);
result = convertToLowerCaseWithUnderscores(result);
return
"/action" + result;
}
private
String convertToLowerCaseWithUnderscores(String string) {
StringBuilder builder =
new
StringBuilder();
for
(
int
i = 0, t = string.length(); i < t; i++) {
char
ch = string.charAt(i);
if
(Character.isUpperCase(ch)) {
ch = Character.toLowerCase(ch);
if
(i > 1) {
builder.append('_');
}
}
builder.append(ch);
}
Search WWH ::
Custom Search