Sunday, February 3, 2008

RESTFUL URLs with Stripes

I came across with Stripes Framework 3 months ago. At first I said "yet another java mvc framework" but after reading its documentation I was suprised to see how easy a web application can be developed with Stripes.

I have developed several web applications with Struts and Spring MVC. The main difference between Stripes and other MVC frameworks is the configuration. Struts uses struts-config.xml to configure its action classes and its action forms. Stripes requires minimal configuration to get up and running (dispatcher conf. in web.xml), and zero configuration per ActionBean or page. If you have never heard of Stripes, go read Quick Start Guide, you will thank to me ;). I do not want to duplicate documentation in this post, because Stripes team has prepared a good and comprehensive documentation.

In this post I will explain how you can create clean urls with Stripes. Stripes does not need a configuration file to configure its action beans. If you does not supply any additional configuration Stripes configures your web application with the default RuntimeConfiguration class. An ActionResolver is responsible for finding your action beans and mapping HTTP requests to action bean methods. Stipes uses NameBasedActionResolver by default.
As it is explained here the default behaviour of NameBasedActionResolver is as follows:

  1. All classes that implements the ActionBean interface is searched. (By default Stripes searches for ActionBean implementations in classpath. But you can narrow the scope of this search by changing the default configuration. This will improve startup performance of your application. See ActionResolver Properties)
  2. Packages "web, www, stripes, action" and their parents are removed. For example "com.companyname.superproject.web.user.DetailsActionBean" becomes "user.DetailsActionBean".
  3. Action or ActionBean suffix is trimmed. "user.DetailsAactionBean" becomes "user.Details"
  4. "." is replaced with "/". "user.Details" becomes "/user/Details"
  5. Finally NameBasedActionResolver appends ".action" to the end of the action URL and we get "/user/Details.action"

If you want to use clear URLs like "http://www.mysite.com/user/details/12345" instead of "http://www.mysite.com/user/Details.action?userid=12345" you must write your custom ActionResolver implementation. Don't worry it is very easy. First in your web.xml tell Stripes to use your ActionResolver class:
web.xml

<filter>
<display-name>Stripes Filter</display-name>
<filter-name>StripesFilter</filter-name>
<filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
<init-param>
<param-name>ActionResolver.UrlFilters</param-name>
<param-value>/WEB-INF/classes</param-value>
</init-param>
<init-param>
<param-name>ActionResolver.Class</param-name>
<param-value>com.companyname.superproject.RestfulActionResolver</param-value>
</init-param>
</filter>

Then write a class that extends NameBasedActionResolver. You only need to override getBindingSuffix() and getUrlBinding(String s) methods. An implementaion (RestfulActionResolver.java) is listed below:
RestfulActionResolver.java

package com.companyname.superproject.RestfulActionResolver
import net.sourceforge.stripes.controller.NameBasedActionResolver;
import org.apache.log4j.Logger;

public class RestfulActionResolver extends NameBasedActionResolver {
@Override
protected String getBindingSuffix() {
return "";
}
@Override
protected String getUrlBinding(String s) {
String urlBinding = super.getUrlBinding(s);
return urlBinding.toLowerCase();
}
}

RestfulActionResolver finds ActionBean instances and performs HTTP requests bindings for you.
"com.companyname.superproject.web.user.DetailsActionBean" action bean is mapped to URL "/user/details".

I used Stripes version 1.4.3 in the above example.
I hope you enjoy the post.

5 comments:

Praveena M said...

Good one, I have been playing with stripes (after struts, JSF) & is amazingly simple.
I have 2 concerns with stripes;
1. There is no critical mass around it (I am willing to part of it.)
2. It's difficult to develop components (Like wicket, tapestry & JSF) limiting it to grow organically alhough we have StreamResult to support AJAX.

Anyway I guess for developing simple page-centric sites, Stripes beats all other frameworks hands down

İlkin Ulaş BALKANAY said...

Yes I agree with you. Stripes is a clean easy to use MVC framework for java. I have worked with struts too. Stripes have not take the wind behind it yet. But i think it will gain popularity as developers realize how easy it is to develop java web applications with stripes.

As i was writing this post i didn't know this url rewrite filter

Anonymous said...

Where is the rest of this post?

You show how do define a custom NameBasedActionResolver class, but your example doesn't support URLs like "http://www.mysite.com/user/details/12345" at all.

BTW, so far, Stripes is great, but this is an important feature.

Anonymous said...

The writer of ilkinbalkanay.blogspot.com has written a superior article. I got your point and there is nothing to argue about. It is like the following universal truth that you can not disagree with: Baseball caps and sandals are almost uniformly ugly, yet there’s not a single person who doesn’t own one or the other I will be back.

Anonymous said...

Predilection casinos? corroborate this raw [url=http://www.realcazinoz.com]casino[/url] cranium and opt after up online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also discontinuation our lately [url=http://freecasinogames2010.webs.com]casino[/url] poor auspices at http://freecasinogames2010.webs.com and engender down spot on target determined licit skiff !
another unsurpassed [url=http://www.ttittancasino.com]casino spiele[/url] grade is www.ttittancasino.com , as opposed to of german gamblers, from strong material manumitted online casino bonus.