URL Binding in Stripes

The best thing about Stripes is that it follows the convention over configuration practice. One of the most convenient conventions is the automatic generation and binding of URLs to the action bean class names. During development this is very helpful since you don’t need to keep in sync a configuration file as you add or delete action beans. To generate the URLs Stripes follows certain rules, of course as with everything else in Stripes you can change the default rules if they don’t meet your needs.

First lets see the default rules and then I’ll discus¬† how to change them if needed.

Stripes will automatically load all your action beans at start up, but we need to tell Stripes where to start looking for action beans, we need to declare at least one root package. We can do this by specifying the ActionResolver.Packages init-param in the declaration of the StripesFilter in web.xml which is the only mandatory configuration parameter. If you keep your action beans in different packages you can add all of them separated by comma as shown in the following spinet.

<filter>
  <filter-name>StripesFilter</filter-name>
  <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
  <init-param>
    <param-name>ActionResolver.Packages</param-name>
    <param-value>com.pk1.action,com.pk2.action</param-value>
  </init-param>
</filter>

Now that Stripes knows how to load the action beans lets see how the URLs are generated and bound to the action bean class names. Lets suppose we have the following action bean:


package com.action.test;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

public class HelloActionBean implements ActionBean {
	private ActionBeanContext context;

    public ActionBeanContext getContext() {
        return context;
    }
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @DefaultHandler
    public Resolution view () {
	return new ForwardResolution("/WEB-INF/jsp/home.jsp");
    }
}

Our action bean class name is HelloActionBean and it’s located under the package com.action.test; to refere to this action in a jps we can use the following name /test/Hello.action.

As mentioned previously to generate the URLs Stripes follows certain rules.
1. It will remove the package name up to and including the the following names if present in the package [action, stripes, web, www]. That’s why we refer to the /test/Hello.action without the full package name since the package name of this bean includes the token action and is removed up to that token.
2. It will remove from the class name the following suffixes [ActionBean, Action or Bean]. That’s why we have the /test/Hello.action instead of /test/HelloActionBean.action.
3. It will convert package name and class names to paths and change all periods to forward slashes. Instead of [test.Hello.action] we have /test/Hello.action
4. It will append the .action to the end of the striped bean name.

Fast forward:
rule 1: from com.action.test.HelloActionBean we get test.HelloActionBean
rule 2: from test.HelloActionBean we get test.Hello
rule 3: from test.Hello we get /test/Hello
rule 4: from /test/Hello we get /test/Hello.action

Lets see some examples of how this works:
com.pk1.action.HelloActionBean –> /Hello.action
com.pk1.web.HelloAction –> /Hello.action
com.pk1.www.HelloBean –> /Hello.action
com.pk1.stripes.HelloActionBean –> Hello.action
com.pk2.module.HelloActionBean –> /com/pk2/module/Hello.action

Some controversial examples:
com.pk1.web.MyActionBeanX¬† –> /MyActionBeanX.action (the ActionBean is not removed since it is not at the end)
com.pk1.www.ActionBean –> /.action (the ActionBean is removed based on the rule number 2)
http://www.web.action.stripes.HelloBean –> /Hello.action (all package tokens are removed since they are in the list of rule number 1)

Now that we know the rules for generating the URLs, lets see how we can change them to meet our needs. This process is quite easy.
The class that implements the rules for URL binding is the NameBasedActionResolver. To change the rules for URL binding you just need to extend this class and override the getBasePackages() and/or getBindingSuffix(), (if you change the suffix do not forget to change url-pattern of the servlet mapping in the web.xml). You can change completely the behavior of URL binding by overriding the getUrlBinding(String). Your custom class for the URL bind rules should be registered as a Stripes extension.

Here is an example of how to change the suffix from [action] to [go]. As I said this is quite easy, it requires two steps:

1. Change the servlet mapping in web.xml


<servlet-mapping>
	<servlet-name>DispatcherServlet</servlet-name>
	<url-pattern>*.go</url-pattern>
</servlet-mapping>

2. Extend the NameBasedActionResolver and override the getBindingSuffix()


package com.ext;

import net.sourceforge.stripes.controller.NameBasedActionResolver;

public class CustomActionResolver extends NameBasedActionResolver {
	@Override
	protected String getBindingSuffix() {
		return ".go";
	}
}

Note that this is an extension and extensions are loaded automatically by Stripes but as with action beans we need to tell Stripes where to look for. To configure extensions we need to add another init-param to the StripesFilter as we did for the action beans. The parameter name for the extensions is [Extension.Packages].
Here is an example:


<init-param>
	<param-name>Extension.Packages</param-name>
	<param-value>com.ext</param-value>
</init-param>

Extending the NameBasedActionResolver we can tweak the URLs in any way we want and as you can see it is quite easy, an interesting tweak would be to implement clean URLs but that will be another post.

Advertisements

Getting started with Stripes & Google App Engine

This is a basic example of using Stripes Framework with the cloud infrastructure of Google. I am using Stripes in one of my projects that I am thinking to move it to google app engine so I decided to experiment before the migration. More posts will follow in this topic as I progress with it. The first objective is to create the basic project structure, configure and deploy, well… a hello world web application. This is a trivial application but there are some limitations using google app engine so trying to use the framework as is will fail with some non-sense stack traces.

The following is a list of things that we will be need to complete this application:
1. Stripes Framework (home page: http://www.stripesframework.org/display/stripes/Home )
2. Google app engine sdk – if you use eclipse and google plugin the sdk is included in the plugin.
3. Eclipse (you can use whatever IDE or environment you like, it’s not mandatory to use eclipse, it’s just easier).

Note: For information on how to install the google plugin visit http://code.google.com/appengine/docs/java/tools/eclipse.html

I assume you are familiar with java web frameworks and that you will use eclipse with the google plugin.

The first step is to create a project in the eclipse IDE:

Give a name to the project and an initial package name. For the moment we do not need GWT so just deselect it.

After pressing finish we have the following project structure.

Before continuing copy the Stripes jars in the the lib directory under hello_project/web/WEB_INF/lib and add them to the project’s build path by selecting them, right click-> Build Path -> Add to Build Path. Also copy and paste the StripesResources.properties in the directory “src” (not in a package).

By default the google plugin will generate a servlet for us, under the package name that we entered in the previews step. It will also generate the following configuration files:
Servlet spec requirement:
1. web.xml (this is the well known deployment descriptor required by the servlet specification)

Google App engine specific (we will not use them for this application):
1. appengine-web.xml
2. jdoconfig.xml

Logging property files:
1. log4j.properties
2. logging.properties

For the purpose of this trivial application we will only need to edit the web.xml (a.k.a Deployment Descriptor so from know on I will call it DD).
By default the plugin registers the generated servlet in the DD, so open the DD and delete the servlet and servlet-mapping it’s not needed.

The following is the generated DD that need to be edited:

The following is the new and final DD containing the Stripes filter and dispatcher servlet:

Note that the web-app tag version has changed from 2.5 to 2.4 there are some jsp exceptions with the 2.5.

The Stripes filter includes two initial parameters the ActionResolver.Packages which tells Stripes where to find the ActionBeans (more on this in next post or in Stripes documentation) and the MultipartWrapperFactory.Class, a factory class used to upload files, but uploading files is not supported by google app engine and the Stripes filter will fail to initialize. Therefore we need to disable this by providing an empty configuration.

The first step to disable file uploading is to add MultipartWrapperFactory.Class initial parameter to the Stripes filter as shown in the DD and the second step is to create the appropriate class. So create a package in the project (I created the com.helloworld.exclude but you can create anything that make sense for your project) and add the following class:


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import net.sourceforge.stripes.config.Configuration;
import net.sourceforge.stripes.controller.FileUploadLimitExceededException;
import net.sourceforge.stripes.controller.multipart.MultipartWrapper;
import net.sourceforge.stripes.config.ConfigurableComponent;
import net.sourceforge.stripes.controller.multipart.MultipartWrapperFactory;

/**
 * GAE does not support file uploading so we need to disable this feature from Stripes.
 *
 * @author 110j
 */
public class EmptyMultipartWapper implements ConfigurableComponent, MultipartWrapperFactory {

	/**
	 * @see net.sourceforge.stripes.config.ConfigurableComponent#init(net.sourceforge.stripes.config.Configuration)
	 */
	public void init(Configuration conf) throws Exception {
	}

	/**
	 * @see net.sourceforge.stripes.controller.multipart.MultipartWrapperFactory#wrap(javax.servlet.http.HttpServletRequest)
	 */
	public MultipartWrapper wrap(HttpServletRequest request) throws IOException, FileUploadLimitExceededException {
		return null;
	}
}

Now that we disabled the file uploading lets create an ActionBean that will handle our request. Create a package in the project called com.helloworld.action and add the following class:


package com.helloworld.action;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

/**
 * The action bean that will handle the our simple request.
 *
 * @author 110j
 */
public class EasyActionBean implements ActionBean {
	private static final String VIEW = "/WEB-INF/jsp/hello_world.jsp";
	private static final String MY_MESSAGE = "hello world";
	private ActionBeanContext ctx;

	private String message;

	@DefaultHandler
	public Resolution showMe() {
		this.setMessage(MY_MESSAGE);
		return new ForwardResolution(VIEW);
	}

	/**
	 * @see net.sourceforge.stripes.action.ActionBean#getContext()
	 */
	public ActionBeanContext getContext() {
		return this.ctx;
	}

	/**
	 * @see net.sourceforge.stripes.action.ActionBean#setContext(ActionBeanContext)
	 */
	public void setContext(ActionBeanContext ctx) {
		this.ctx = ctx;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
}

Next lets create a jsp file that will render the response, create the hello_world.jsp under WEB_INF/jsp/ and add the following content:

Finally create a welcome file called index.jsp if it does not exist or rename the index.html to index.jsp and add just a forward to the action:

You can delete the servlet generated by the google plugin there is no need for it.

In order to test it you just need to run the hello_world in the Run history of eclipse as the google plugin will create one for us, or under the run button in the toolbar, and then visit the address localhost:8080

NOTE: if your application require session you need to enable it in the appengine-web.xml since it is disabled by default (but keep in mind the implication it may have in a distributed environment such as google app engine, maybe your attributes will need to implement the HttpSessionActivationListener if you plan to add them in the session).

You can upload your application by using eclipse or the command line tool, for more information visit http://code.google.com/appengine/docs/java/gettingstarted/uploading.html

To be continued!