Java-Spring Web Application – Maven

Here we learn to create a simple web application in Java and Spring using Maven build and dependency management tool. There are several articles written with various alternatives. Java-Spring Web Application - Maven, is the simplest article and it helps in creating the simplest web application. It just demonstrates integrating Spring MVC using maven. It also demonstrates the capabilities of maven, how we can get rid of the traditional approach of including each jar in the class path by manually searching and downloading it.

First for the impatient programmers:

Complete the following prerequisites and then download the zipped archive, and run the command
 mvn clean install
to get set go. Maven will generate a .war (web archive) which you can deploy on your favorite web container. if you deploy this on tomcat, you can access the application by typing in the URL http://localhost:8080/SimpleProject/helloWorld/

Prerequisites for Java-Spring Web Application using Maven

Following is a list of prerequisites to start this project. 1) Java must be set up in the development system. Read more about installing java on Windows. 2) Maven must be set up in the development system. Read more about installing maven on Windows.

Detailed Explanation

Use the below command to prepare a project skeleton for a java web project/
mvn archetype:generate -DgroupId=in.techieme.springmaven -DartifactId=SimpleProject -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
Note: To know more about maven archetypes, please visit Maven Website You can run the above command after navigating to a directory where you want to create the project.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: in.techieme.springmaven
[INFO] Parameter: packageName, Value: in.techieme.springmaven
[INFO] Parameter: package, Value: in.techieme.springmaven
[INFO] Parameter: artifactId, Value: SimpleProject
[INFO] Parameter: basedir, Value: C:\Users\dprasad\ws
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\dprasad\ws\SimpleProject
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.708s
[INFO] Finished at: Mon Jun 03 18:53:14 IST 2013
[INFO] Final Memory: 7M/13M
[INFO] ------------------------------------------------------------------------
In the above case a project having name SimpleProject is created in the directory C:\Users\dprasad\ws. You should see the below directory structure once the command is successfully completed. |--SimpleProject |----pom.xml |------src |--------main |----------resources |------------webapp |--------------index.jsp |----------------WEB-INF |------------------web.xml Instructing Maven to use specific versions of Spring and Java Development Kit: Edit the project's pom file and add the below code:
<spring.version>3.1.1.RELEASE</spring.version>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
maven will use the above properties while building the project. Maven provides a compile plugin to compile the projects. Read more about Maven Compiler plugin. This can be done by editing the pom file and adding the below plugin information anywhere inside the build tag.
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.5.1</version>
</plugin>
As we are building a spring application, it is necessary to inform maven to use spring. In maven this can be done by adding those jar information as dependencies. Edit the project's pom file to add the below code in the dependencies tag:
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
</dependency>
Now the coding part Create a directory named java inside the src/main directory to place your java classes. Create a directory structure inside the java directory to resemble your package structure. In my case the SimpleController.java class is inside a package named in.techieme.springmaven.web In my case the directory structure at this point is: |--SimpleSpringMavenProject |----pom.xml |------src |--------main |----------java |------------in |--------------techieme |----------------springmaven |------------------web |--------------------SimpleController.java |------------resources |--------------webapp |----------------index.jsp |------------------WEB-INF |----------------------web.xml Now add the below code to the SimpleController.java
package in.techieme.springmaven.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/Simple")
public class SimpleController {

	@RequestMapping(value="/helloWorld", method = RequestMethod.GET)
	public String welcome(ModelMap model) {

		model.addAttribute("msg", "Hello World from Simple Maven based Web Application.");

		return "index";

	}

	@RequestMapping(value="/helloWorld/{user}", method = RequestMethod.GET)
	public String welcomeName(@PathVariable String name, ModelMap model) {

		model.addAttribute("msg", "Hello "+name+"  from Simple Maven based Web Application.");
		return "index";

	}
}
We have coded our controller class which will receive the requests from the web. But we still need to inform our web container, i.e. web.xml to send our requests to the SimpleController. Edit the web.xml file and add the following code in it.
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
	 
<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
As web applications have their web container which is named as web.xml similarly we have spring container. The name of the spring container is upon the choice of the user. In our case we put the name as spring-servlet.xml. Just make sure that the name of the file and the name in the entry
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
in the web.xml must match. Create a spring configuration file xml (spring-servlet.xml) file and place it in the WEB-INF directory of the project. Edit the spring-servlet.xml and add the following code:
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/" />
  <property name="suffix" value=".jsp" />
</bean>
Stay connected and stay Subscribed