Tuesday, January 8, 2008

ITMill Toolkit 5 & Spring

Once again, in the perfect world of the Java frameworks, everything is simple, everything is easy.

Ok, I'll admit, I've been helped on this one, by a post from Joonas Lehtinen .

He say everything: toolkit application can be treated as servlet.
So.

Set up Spring as usual:
Add the Spring dependencies to your pom.xml :

<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.0.3</version>
</dependency>
</dependencies>

Add the Spring listener to your web.xml :

<web-app>
..
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

And add the relevant informations about your application context files :

<web-app>
..
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
</web-app>

Following Joonas advice, I've made a very simple helper class allowing me to access Spring beans:

import com.itmill.toolkit.Application;
import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringContextHelper {

private ApplicationContext context;

public SpringContextHelper(Application application) {
ServletContext servletContext =
((WebApplicationContext) application.getContext())
.getHttpSession().getServletContext();
context = WebApplicationContextUtils.
getRequiredWebApplicationContext(servletContext);
}

public Object getBean(final String beanRef) {
return context.getBean(beanRef);
}
}


And... That's it. Simple, eh ?

Usage example:

public class SpringHelloWorld extends com.itmill.toolkit.Application {

public void init() {
final Window main = new Window("Hello window");
setMainWindow(main);

SpringContextHelper helper = new SpringContextHelper(this);
MyBeanInterface bean = (MyBeanInterface)
helper.getBean("myBean");
main.addComponent(new Label( bean.myMethod() ));
}
}

ITMill Toolkit 5 & Maven2

Using ITMill Toolkit 5 & Maven2 together is a matter of minutes.

You do not know or use Maven2 ? And you are developing a Java Web (or not) Project ? What a shame ! You should.
Maven2 allow you to manage your project dependencies, build, and reporting in an easy and cool way. I will not spend too much time detailing Maven2 use here, check http://maven.apache.org/ if you want to know more.

As ITMill Toolkit (gosh, I need an acronym for that ^^), from the developer point of view, is a totally classic web application, nothing special is required to unleash the power of Maven2 on it.

Before anything else, download ITMill jar file . The jar is inside all this stuff, as this is a demo application, in WEB-INF/lib .
Install it in your local Maven2 repository:
mvn install:install-file
-Dfile=
-DgroupId=itmill
-DartifactId=
itmill-toolkit
-Dversion=5.0.0
-Dpackaging=jar
-DgeneratePom=true
Then, create a Maven web project, using the archetype:
mvn archetype:create
-DarchetypeGroupId=org.apache.maven.archetype
-DarchetypeArtifactId=maven-archetype-webapp
-DarchetypeVersion=1.0-SNAPSHOT
-DgroupId=your.company
-DartifactId=project-name
-Dversion=1.0
-Dpackaging=war
Open the project in your favorite IDE (I'm a huge supporter of NetBean6, but I'm sure using Eclipse change nothing).
Edit you project pom.xml . Add the ITMill dependency:
<project>
..
<dependencies>
..
<dependency>
<groupid>itmill</groupid>
<artifactid>itmill-toolkit</artifactid>
<version>5.0.0</version>
</dependency>
</dependencies>
</project>
And there, everything is done.

Just set up a main ITMill class, update your web.xml to add a servlet & servlet mapping for it, and your Maven2 / ITMill project is fully operational.

ITMill ToolKit 5: GWT without the inconveniences

I always liked GWT.
Being able to code a web application without having to write a single line of HTML or JavaScript, without bothering to use ten hack per page, without banging your head on a wall due to a strange IE compatibility bug... Priceless.

But GWT come at a price.
You have to use a quite inconvenient RPC mechanism (having 4 classes just to call a server side function from your client is NOT funny) ; you have to declare every one of your RPC function in a separate XML file; and you have to put all your client classes in a "client" package.
Oh, and you have to use a specific Google compiler to create your HTML & JS, too.

But the real trouble begin when you want to use GWT not alone, but in conjunction with all those so nice tools, like Maven2 and Spring. The pain, oh, the pain !
I'm sure it can be done. I'm even sure that a lot of people managed it, and are happily all those things together. But I do not think that anyone would say that it is easy, clean and quick.
After a week of failures, I stopped, and begin to reverse my project to Struts2.

Do not misunderstand me: GWT is a superb framework, with many usages, and can be used to do marvelous things. I just guess it is not for me.

But here come a new challenger: ITMill Toolkit 5 ( http://www.itmill.com/ ) .

ITMill Toolkit is a powerful framework based on GWT.
It use GWT, but under the hood. It remove all the pain from it.
In less than a day, I managed to integrate it with my previous Maven2 / Spring / Acegi project, and to begin to create my UI. And that's a LOT more that what I can claim I accomplished with GWT.
Using ITMill Toolkit is pretty much straightforward: grab the jar, copy in your webapp a folder containing a default theme, add a servlet to your web.xml , and voilĂ , you're ready to go.

The HelloWorld tutorial on the IT Mill site is a good example of the way it work.
If you know GWT, or even if you know AWT / Swing, you will not be surprised.
The Application class is you entry point, called by the servlet; the Window class is, well, your main window (ie. the main page user see). You can add Components to your window, like Labels, Buttons, or TextFields.

Like GWT, it's a pure Java framework. Even more: no XML configuration, no special compiler, no rules on packages, no RPC framework. You've got classes, you do whatever you want, the toolkit takes charge of everything for you.

For me, it's the revolution in web applications developping GWT should have been.