How to create a webapp? A step by step tutorial about the springframework, maven and eclipse.

12 Dec 2009 at 00:00:00 - 8 comment(s)

This post is a short introduction on how to create a web application in java from scratch. We will use the springframework, a really popular java framework. The current stable version of this framework is 2.5.6 but we will see the features of the next major version: 3.0. At the time I write these lines it is in RC3 so it should be released pretty soon. I will do a short introduction about maven as we will use it. The IDE I am using is eclipse so if you don't already know about it, you can learn few things there.

Install maven

That is the first step to make our life easier so download maven.

Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.

In short, it's going to be what will build the war file and manage the dependencies.

If you're using linux (pretty much the same for every OS), you unzip the file (I expect you've downloaded the .tar.bz2 or .tar.gz):

tar jxf apache-maven-2.0.10-bin.tar.bz2
or
tar zxf apache-maven-2.0.10-bin.tar.gz

I personally put this in the /opt folder.

You need to edit your PATH so you can edit /etc/profile (in order it applies for all the users otherwise you can edit your .bashrc and you add the following to the already existing line:

PATH="/bin:/usr/bin:/sbin:/usr/sbin:/opt/apache-maven-2.0.10"

We are done with the maven installation. Next on our list: tomcat.

Install tomcat

You'll need a web container to run your webapp. I choose tomcat for this tutorial but you could use an other such as glassfish for example. Anyway, you can download tomcat and you unzip it wherever you like.

Eclipse

You can jump to Create the project step if you already have eclipse installed and you have the m2eclipse installed and working.

First setup

So first of all you get eclipse by downloading the Eclipse IDE for Java EE Developers.

You do the same thing than for maven. Unzip it, add it to your path. And you start it. You select the workspace (the folder where you'll have your code for your webapp) and you switch to the Workbench (top right corner).

You are now here:
eclipse

So the first step is going to be to install the following plugins: m2eclipse, spring-ide and subclipse (subclipse is not necessary, it's for subversion which I use, if you don't then you don't need it). For spring-ide, it's very convenient to have it when you use the springframework.

To install plugins, on the menu on top:
Help -> Install New Software...
You click on the button Add...

In Name you write subclipse and in Location:
http://subclipse.tigris.org/update_1.6.x
(that's for subversion 1.6 obviously, if you use a different version then check the Download and Install page of subclipse).

You click on OK and check all the select box then click on Next and continue with the rest of the installation. Same process for m2eclipse and spring-ide excepted that for m2eclipse you should uncheck Maven Integration for AJDT in Maven Project Configurators otherwise it's probably not going to work.

The Location urls are:
m2eclipse = http://m2eclipse.sonatype.org/update/
spring-ide = http://springide.org/updatesite

Create the project

File -> New -> Other...

You choose Maven Project as below

Eclipse new project - Select a wizard

Click on Next -> Next

You have to choose an archetype in the list proposed. Just keep the default one and click on Next (it's maven-archetype-quickstart)

In the next step you need to choose a group ID for the artifact.

I give an example so you understand what is this about. For the springframework the group id will be org.springframework and then the artifcat id is the project so you'll have spring-core for example. It follows the package names of your java classes basically. I am not an expert in this. What I say just comes from my experience.

So you can have as a group id com.mycompany and as an artifact id mywebapp

After you've clicked on Finish, you'll see your project in the Project Explorer.

Make your project a webapp

Click on the arrow to expand the content of the project and double click on pom.xml to open it.

We are going to add the dependencies we need. In the section dependencies, add the following:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>3.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>

You don't need spring-jdbc in this tutorial but as most webapps use database, I put it there anyway. We'll see in an other post how to make use of spring-jdbc.

You need to add 1 more thing in your pom file, the Spring Portfolio Milestone Repository. So just above the tag dependencies you add the following:

<repositories>
  <repository>
    <id>spring-milestone</id>
    <name>Spring Portfolio Milestone Repository</name>
    <url>http://s3.amazonaws.com/maven.springframework.org/milestone</url>
  </repository>
</repositories>

We're almost there. You now need to change the packaging tag value to war instead of jar so you'll have:

<packaging>war</packaging>

You should as well add the following to make sure maven is using at least java 1.5 as we will use Annotations:

<build>
  <finalName>mywebapp</finalName>

  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.0.2</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>

That's it for the pom file you can save and close it. Right click on mywebapp then Maven -> Update project configuration. You can do Maven -> Update dependencies as well but it should have been automatically by m2eclipse. Let's create the missing files/folders now.

In src/main create a folder webapp. In this new folder create a folder WEB-INF and create a folder views in WEB-INF

In WEB-INF create 3 files:
applicationContext.xml
spring-ws-servlet.xml
web.xml

Here is the content of web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  id="WebApp_ID" version="2.5">
  
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/applicationContext.xml</param-value>
  </context-param>
   
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>spring-ws</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/web/*</url-pattern>
    </servlet-mapping>

The content of spring-ws-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.mycompany.mywebapp.web" />
  
</beans>

The component-scan base-package corresponds to the package that will contain your Controllers.

The content of applicationContext.xml

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

  <import resource="classpath:com/mycompany/webapp/applicationContext.xml"/>  
</beans>

As you can guess you have to create a source folder src/main/resources so right click on mywebapp and choose New -> Other... then in Java choose source folder. Finally in this source folder create a package so right click on it and choose New -> Other... then in Java choose package and in Name put com.mycompany.mywebapp

You can now create the file applicationContext.xml in there and its content is the following:

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

</beans>

We'll add things there later on.

You need to create a package web in the already existing com.mycompany.mywebapp (if you check in src/main/java). Create a Java class Application in the web package. You can delete App.java created by maven.

The content of Application.java

package com.mycompany.mywebapp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Application {

  @RequestMapping(value="/",method=RequestMethod.GET)
  public ModelAndView home(HttpServletRequest request, HttpServletResponse response) {
    
    return new ModelAndView("home");
  }
}

Create a file home.jsp in the folder views you created previously and write something in it like "Hello, that's my webapp". Finally We need to edit applicationContext.xml that you have in src/main/resources/com/mycompany/mywebapp/ and add the following:

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass">
      <value>org.springframework.web.servlet.view.JstlView</value>
    </property>
    <property name="prefix"><value>/WEB-INF/views/</value></property>
    <property name="suffix"><value>.jsp</value></property>
</bean>

If you want to know more about viewResolver, then go to read the documentation there: http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html. Even if you don't want you should read this from the beginning to the end.

That's it, you can now run your webapp. Right click on mywebapp, Run As -> Run on Server. Choose Apache -> Tomcat v6.0 Server. Click Next and select the folder where you've unzipped Tomcat and click the Finish button.

You can now go on http://localhost:8080/mywebapp/web/ and you'll see the content of your home.jsp file

I said we'll use spring 3 but we haven't actually used any of the new features in spring 3 so far so I invite you to visit the post of Arjen Poutsma on the spring blog. You'll find this really useful.

Download the webapp created following this tutorial

In order to create the war file to deploy your webapp in a web container, you can either use m2eclipse (Run As -> Maven package) or using maven you've installed at the beginning of this tutorial. I personally don't use m2eclipse for this. So here is the command you need to use to build the war file:

mvn clean package

That's it for today. Don't hesitate to let a comment, ask questions or point out things I missed.

8 comments

Notify me of follow up comments