SourceForge.net Logo
Version: 0.27 - Last update: 2004-05-30 Home Page - SourceForge Project Page - Contact

Jodd
  Overview
  News
  Download
  License
  References

Development
  Using Jodd
  Javadoc
  JUnit report

Community
  Contribute
  Report a bug

More reading
  Milestone 0.30



Top 25%

BSD
 

Framework details

Jodd provides lightweight mvc2 JSP framework. This text explains how it works and gives step-by-step usage instruction. This examples works with Tomcat4, although it can be used for any other JSP container as well.

Step #1 - web.xml

First thing to do is to 'register' controller part of the framework. This controller will dispatch and handle clients requests. Registration is simple, just add something like this into the web.xml:

web.xml
 
<servlet>
	<servlet-name>controller</servlet-name>
	<servlet-class>jodd.servlet.ActionController</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>controller</servlet-name>
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

Step #2 - actions.xml

On initialization, Jodds controller reads WEB-INF/actions.xml configuration file (file name can be changed). This file enumerates all actions and, additionally, all global forwards. What is nice is that one configuration file can simply import any number of other action files - the effect is the same as we have one big action file.

Here is how action may be defined in the configuration file:

action.xml - action definition
 
<action path="/mvc2/act.do" type="MvcAction">
	<forward name="ok" path="/mvc2/world.jsp"/>
</action>

Above action definition has the following meaning: when controller receives request to /mvc2/act.do it will first check to see if an object of MvcAction exists, and if not one will be instanced and stored in the pool. Once controller got the action object, method MvcAction.doAction() will be invoked.

Jodd alternativly can invoke specific method as action hadler, instead of default doAction(), so one action class may be used to handle many actions (requests).

action.xml - method mapping
 
<action path="/mvc2/direct.do" type="MvcAction2" method="go"/>

On /mvc2/direct.do request controller will invoke MvcAction2.go().

Which of this two approachs to use is completely developers choice.

In the first example one forward string ("ok") has been assigned to the action. Every action handler (i.e. method of an action class that is mapped to a request, either default one or user specified) must return a string that represents a forward, to tell controller what view to present back to client.

In the second example no forwards has been assigned to the actions. This means that action will use on of the global forwards - forwards that are not assigned to any particular action.

action.xml - global forwards
 
<global-forwards>
	<forward name="ok" path="/mvc2/world2.jsp"/>
</global-forwards>

When actions forward and global forward are equal strings, actions forward will be used.

The only thing left is an example how to import another configuration file:

action.xml - include example
 
<include file="/WEB-INF/actions-more.xml"/>

Step #3 - ActionServlet

All actions simply inherit the ActionServlet class:

ActionServlet example
 
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import jodd.servlet.*;

public class MvcAction2 extends ActionServlet {

	public String go(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		System.out.println("MvcAction2.go()");
		return "ok";
	}
}

A tip: instead of returning new String instance it is better to have one global class with static final strings that represents mapping. In the future release of Jodd, a simple tool will be created that will be able to create such a class automatically from the action.xml configuration file.

As explained above, Action handler returns a string that represent either action or global mapping, i.e. forward. However, handler may returns page name itself instead of mapping:

ActionServlet without mapping
 
public class MvcAction2 extends ActionServlet {
	public String go(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		return "page2.jsp";
	}
}

There is a working example in the distribution archive.


http://jodd.sourceforge.net
najgor at users.sourceforge.net