Friday, August 24, 2007

web.xml specification header 2.3 or 2.4

This is a quick tip. There are two versions of the servlet specification in use now: 2.3 and 2.4.
The header for the web.xml file will be different in both cases.
In version 2.3 it is used a DTD do specify the XML format, and it is as follows:



<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>



In version 2.4 it is used a Schema to specify the XML Format. Here it is the format:


<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

Sunday, August 12, 2007

Struts in development mode

Struts has a very nice feature which increases development productivity. It is called development mode (or dev mode) and it forces reloading of configuration files and resource bundles, it raises the debugging level etc. As you, smart developer who reads this blog, might have noticed it has an impact on performance, so use it wisely.
It is very easy to tell struts that you want it running in dev mode. You may set a property in struts.properties like this:

devMode=true

It is also possible to set this same property in the struts.xml file:
<constant name="struts.devMode" value="true" />

Struts 2 wired by Spring

Struts 2 is a framework for aiding Java Developers into building their web applications, using Inversion of Control (IoC) and MVC Architecture for a multi-tier architecture.

In this post we will see how to wire your action and service classes in Struts 2 using the spring framework.

To start, if you are using Maven there is a dependency you need to add to your pom file.


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>2.0.6</version>
<scope>compile</scope>
</dependency>


When this is done, it is necessary to add a listener to the WEB-INF/web.xml file. This is a spring listener, used to intercept calls and redirect them to the Application Context.

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


Now, we will need to create the Application Context file for Spring. It will use the configuration information in this file to inject the necessary beans into each needed class. The file format is like below and it should be called WEB-INF/applicationContext.xml. I added some example beans to explain how Struts will later identify these beans and inject them into the Actions being called by the framework.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


<bean id="personService" class="my.service.PersonServiceImpl" />

<bean id="personAction" scope="prototype"
class="my.web.action.PersonAction">
<constructor-arg ref="personService" />
</bean>

</beans>


Last, but not least it is necessary to setup the struts.xml file to tell the framework that it should use struts' object factory. The example below will show the entries you need to add to struts.xml file. Setting "struts.objectFactory" to "spring" will force Struts to instantiate the actions using Spring, injecting all the defined dependencies on applicationContext.xml.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />

<package name="person" extends="struts-default">

<action name="list" method="execute" class="personAction">
<result>pages/list.jsp</result>
<result name="input">pages/list.jsp</result>
</action>

....


</package>

</struts>


The "class" attribute for each action alias is set to "personAction", which is the bean id that we defined on applicationContext.xml for the PersonAction class. This is all that is needed to make Struts work with Spring.

Have fun!

Maven Download Sources

When using maven, it normally downloads only the binaries from the dependencies your project has.
It is possible, with simple commands, to also download the source and/or documentation from the dependencies you are interested in.

To download sources:

mvn [goal] -DdownloadSources=true


To download javadocs:
mvn [goal] -DdownloadJavadocs=true

Maven Eclipse

The maven eclipse plugin is provided for those who have a ready .pom file for a maven project and want to start using eclipse as the IDE for the project.

The command is as simple as running:

mvn eclipse:eclipse

This command have a number of parameters, but one of the most interesting ones would be the one which allows to create a WTP (Web Tools Platform) aware eclipse project.

mvn eclipse:eclipse -Dwtpversion=1.0

This command will create an eclipse project with WTP 1.0 support.

Friday, August 3, 2007

Adding Repositories to Maven 2

Maven uses on-line repositories to find the dependencies you set in your pom file. By default, maven uses the repository specified in the

If you want to use other repositories, you are able to do so by adding a ~/.m2/settings.xml file. This file should be provided in the following format:


<settings>
<profiles>
<profile>
<id>standard-extra-repos</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jboss-snapshot</id>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>jboss-plugins</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>jboss-snapshot-plugins</id>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>


Some repositories I've collected and you might be interested in using, because some artifacts are available only through some of these repositories and not all of them. It is important to remember that the more repositories you add, the slower will be your build, so use them with wisely.

Repository
http://www.ibiblio.org/
http://download.java.net/maven/2
http://repo1.maven.org/maven2
http://repository.jboss.com/maven2
http://maven.apache.org/
http://mojo.codehaus.org/


Snapshot
http://snapshots.jboss.org/maven2
http://people.apache.org/repo/m2-snapshot-repository
http://snapshots.maven.codehaus.org/maven2
http://snapshots.repository.codehaus.org


If you want to download snapshot version of maven plugins, there is a page which explains how to setup your settings.xml file.

Using HSQLDB database - the basics

HSQLDB is a powerful database engine. It is normally used to provided an embedded database functionality for java applications.

It is as simple as downloading a zip file, extracting it into your system and you have a functioning RDBMS.

After extracting the zip file, you should have the following (resumed) structure in your system:


/hsqldb
/bin (scripts for running the server and utilities)
/demo (scripts and applet examples)
/doc (guess what?)
/lib (all binaries - including hsqldb.jar)
/src (all source code)
index.html (documentation about the distribution)


The hsqldb.jar is everything you need to run the database application. It contains the RDBMS, the driver and some tools (the database manager, the query tool and the command line tool).

Running the tools

To run some of the tools, enter the lib folder and run the following command, which will run the database manager.

java -cp hsqldb.jar org.hsqldb.util.DatabaseManager


The main classes for the tools deployed with the hqsldb.jar are:


  • org.hsqldb.util.DatabaseManager

  • org.hsqldb.util.DatabaseManagerSwing

  • org.hsqldb.util.Transfer

  • org.hsqldb.util.QueryTool

  • org.hsqldb.util.SqlTool



HSQLDB may run in standalone (in-process) or server mode. There are three server modes, based on the protocol used for communications between the client and server and they are HSQLDB Server, Web Server and Servlet. It is also possible to run HSQLDB in a way that the database is not persistent and exists entirely in random access memory.

The default user and password for connecting to the database is "sa" / "sa".

For more information on this powerful tool, read the online manuals or the documentation provided with the distribution zip file in the doc folder.