Musings about Coding, Business and other Geek Stuff Live and Direct from somewhere on the planet
August 02, 2004
How to get started with NanoWeb

Since I first read about NanoWeb a couple of months ago, I’ve been fascinated by it’s easy approach. Unlike any other java mvc framework there are no g’damn xml configuration files.

One of the problems with NanoWeb is that it is not very well documented. Like much of the innovative stuff happening at codehaus they refactor at will, which they should definitely do with experimental stuff like this. They have also just recently moved everything around in CVS. NanoWeb and a few other related projects have now been merged under a new project NanoWar where all the development is taking place now. NanoWar includes NanoContainer like configuration of Struts, Axis, Webwork 1 &2 as well as of course nanoweb.

They have a Sample NanoWeb that you can checkout from CVS and build with maven. I’ve been playing around with it a bit over the weekend and wrote my own extremely useless personel web application using it. You can download a working NanoWeb Sample Application WAR here which includes their game sample (with my cheat hack) and my personel application. Just drop it in /opt/tomcat/webapps (you know the score).

I will now go over some of the basics of nanoweb configuration and development:

Configuration

We all hate configuring stuff in J2EE right? Well judging by the sheer amount of xml configuration files going on in your average struts, spring, webwork etc, etc framework yall gotta be loving it now. Well I can’t stand it. I’ve got a perfectly good Java IDE, I’m a semi capable java developer. For god’s sake let me write code and not spend half my life in xml.

I’ve always been a fan of PicoContainer as it allows me to build configurable apps without all the extra imports and external configuration files. PicoContainer let’s you configure stuff in Java. NanoContainer extends that and lets you configure stuff in xml or your favorite scripting language. No change needed in your code.

NanoWeb is part of NanoContainer and lets you configure your PicoContainer’s within your web.xml file using Groovy or any other java scripting language:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>NanoWeb Demo</display-name>

    <context-param>
        <param-name>nanocontainer.groovy</param-name>
        <param-value><![CDATA[
             pico = new org.picocontainer.defaults.DefaultPicoContainer(parent)
            if(assemblyScope instanceof javax.servlet.ServletContext) {
		pico.registerComponentImplementation(java.util.Map,java.util.HashMap)
            } else if(assemblyScope instanceof javax.servlet.http.HttpSession) {
                // Session level components
                pico.registerComponentImplementation(org.nanocontainer.sample.nanoweb.NumberToGuess)
            } else if(assemblyScope instanceof javax.servlet.ServletRequest) {
		
            }
        ]]></param-value>
    </context-param>

    <listener>
        <listener-class>org.nanocontainer.nanowar.ServletContainerListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>NanoWeb</servlet-name>
        <servlet-class>org.nanocontainer.nanowar.nanoweb.NanoWebServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>NanoVelocity</servlet-name>
        <servlet-class>org.nanocontainer.nanowar.nanoweb.NanoWebVelocityServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    
    <servlet-mapping>
        <servlet-name>NanoWeb</servlet-name>
        <url-pattern>*.nano</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>NanoWeb</servlet-name>
        <url-pattern>*.groovy
        </url-pattern>
    </servlet-mapping>

    
    <servlet-mapping>
        <servlet-name>NanoVelocity</servlet-name>
        <url-pattern>*.vm</url-pattern>
    </servlet-mapping>
</web-app>

The above is the web.xml file used in the sample app. The top part is an inline groovy script to configure PicoContainer for each of the possible scopes within a J2EE web app.

You simply register classes (and implementations) in the PicoContainer for each scope. Application (ServletContext), Session or Request. In the above, I have registered a HashMap under the Application context. This is my Personel database (I told you it was simple). As part of the game demo there is a NumberToGuess class registered for the Session. The game keeps track of who’s playing using this session scope object.

Creating your App

So how do you write your actual app? Firstly create a groovy class directly in the web apps. Each method within that class is an action. So an example URL for the file game.groovy is /game/play.groovy

Each method doesn’t take any parameters. You define local variables in your class and put any required components from the PicoContainer we configured earlier in the Constructor. This allows PicoContainer to easily auto configure the app automatically. For each method you must return a View name. Just return a string like “view”.

class Person {
 	name=""
 	age=0
 	java.util.Map map
 
 	Person(java.util.Map database) {
  	   map=database;
  	   if (name!=""&&age==0) {
   		age=map.get(name)
   	   }
  	}
 	
 	enter() {
  		if (age==0&&name!="")
  			age=map.get(name)
  		return "input"
  	}
 	
 	search() {
  		return "search"
  	}	
 	register() {
  		if (name==""||age==0) 
  			return "error"
  		map.put(name,age)
  		return "success"	
  	}
 	view() {
  		if (name=="")
  			return "error"
  		age=map.get(name);
  		return "view"
  	}
 
 	list(){
  		return "list"
  	}		
}

I am no Groovy expert. As a matter of fact this was my first attempt at groovy and I have no idea what I’m doing, but it seems easy enough. It appears I can just write Java but I don’t need semi colons and it’s not vital to declare variable types.

Views

As you can see above several of the actions just simply go straight to a view. So what is a view? The current implementation just uses velocity templates. I’ve looked at the code and it should be simple enough to add jsp and friends to it as well. The only requirement for using velocity and not jsp is that the extension .vm is hardcoded in the NanoWebServlet at the moment.

Simply speaking you create velocity templates which are resolved using the following naming structure:

  • /context/folder/script_action_result.vm
  • /context/folder/script_result.vm
  • /context/folder/result.vm
  • /result.vm

Where “result” is the string you return from your action method. Action is the method name and script is the name of the script.

Here is an example of a simple view register_list.vm which lists the contents of the HashMap:

<html>
    <body>
<ul>
#foreach( $key in $action.map.keySet() )
    <li><a href="view.groovy?name=$key">$key</a></li>
#end
</ul>
<a href="enter.groovy">Add new person</a>
    </body>
</html>

As you can see you can access the Groovy classes instance variables through the $action variable within velocity.

End thoughts on NanoWeb

NanoWeb is very simple as it is, both to write for as well as the actual implementation, which consists of 5 classes and one interface. The main addition I would make to it is add jsp and freemarker as supported views ( I might have a go at that myself).

Secondly and probably less important is to add other languages to it. I would like to be able to write real java code and not just groovy. The reason being that IntelliJ IDEA could then help me be a bit more productive. If we did that we might as well add support for Python and Ruby as well. Who knows I might try my hand at that as well.

Please, please don’t start adding extra fluff like AOP, Validation etc to it though. Keep it simple. Let people add their own fluff. That is what annoys me with things like webwork. It is far more complex than it needs to be. The thing I really like about NanoWeb is the ASP/PHP like simplicity it brings to real MVC.

Posted by pelleb at August 02, 2004 10:51 AM
This entry was posted in the following Categories: Java
Comments

Very cool stuff here. Can you tell us how to get nanowar?

Posted by: Kris Thompson on August 3, 2004 01:12 AM

You can either download the war file I've linked to above or check it out from cvs. There currently isn't a distribution for it.
-P

Posted by: Pelle on August 3, 2004 10:35 AM

What Tomcat version were you using? I tried 5.0.25 and got an error on the listener at startup. I submitted a bug report in Jira.

I'll check out your war you have.
Steve

Posted by: Steve on August 3, 2004 02:58 PM

For using FreeMarker as view, please refer to http://jira.codehaus.org/browse/NANO-72

Posted by: Kouhei Mori on August 3, 2004 03:22 PM

Cool. This one works. Thanks, man!
Steve

Posted by: Steve on August 3, 2004 08:36 PM

Hey I noticed that you don't have a request scope. Is this not necessary? The build I was trying had it, I think.

Steve

Posted by: Steve on August 4, 2004 02:44 PM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?