Spring MVC 3 Hello World Quick Start with XML and annotations

CODEBASE

Setting up an initial Spring MVC 3 can be trivial but lengthy process since this is not spring boot. There is so much to understand and think about before doing this. But if you are curious about a quick hello world this is a guide to kind of remind you the basics and provide some side notes.

1 - Setup your maven project for the webapp archetype.

2 - Costomize your basic POM values if needed.

<groupId>com.dhis</groupId>
<artifactId>partial-experiments</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

The packaging attribute lets maven know that we are trying to package a WAR-Web Application Archive. A typical lib might use JAR. The rest is self explanatory

3 - Basic maven dependencies

Add the following dependencies which spring is based and relies on the pom.xml.

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>

JSTL is used in xml configurations to define bean specs. You need the tag lib to describe the xlm configs required conveniently.

Javax Servlet API is the underlying framework for delivering web applications. One can come up with a servlet based website obviously using this dependency. Spring builds on top of this. Look here for more information.

One should then try and setup tomcat on your IDE. In eclipse you have to create a server and add the module. In Intellij commercial edition you can add a run config using the tomcat container. If its IJ community you will have to remote debug your app while using maven to deploy and defining the MAVEN_OPTS for remote debug that listens for a debugger to connect. And remote debugging on the defined port.

4 — Configuring web.xml

Next we need to do some changes to our default web.xml file. This file should be at the src/main/webapps/WEB-INF/web.xml directory by convention. Let’s upgrade it first the default generated DOCTYPE tag from the archetype to a web-app.

remove the Doctype tag in the web.xml and include this tag

<web-app 
version=”2.5"
xmlns=”http://java.sun.com/xml/ns/javaee"
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

This will be the schema definition deployment descriptor for our web app.

Web XML is where we wire up our url mapping to our dispatcher servlet. Dispatcher servlet is Springs implementation of the Front Controller Pattern. Here spring will be facing all our request to this web apps and routing them to them to the relevant controller.

This will be the final view of the total web xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


<servlet>
<servlet-name>partialRenderServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet. DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>partialRenderServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<display-name>Archetype Created Web Application</display-name>
</web-app>

You can define your servlet anyway you want. I have named it as partialRenderServlet. But this is like a variable name which will be mapped to by our servlet-mapping so make sure both servlet-names are equal.

We also do another important thing here where we define our contextConfigLocation this overrides the default location of our ApplicationContext Configuration. This contains the central configuration of our application. This is overriding convention for the placement of this config file. To see how convention works look at this section.

5-AppContext Configuration

This is an example of a typical appconfig xml [servlet-config.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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-3.2.xsd">

<mvc:annotation-driven/>
<context:component-scan base-package="com.dhis"/>

<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

The namespaces in the beans tag are required to define the tags shown within it. The annotation driven and component scan tags activate mvc and defines the location within which to search for components. More details here.

The view resolver is a key part of this configuration whcih provides a mapping to our view locations. Here the location of the jsp pages to be returned is suffixed and prefixed. So in the controller if we return “xview”, it will try to find /WEB-INF/jsp/xview.jsp and return it.

6-First Java Code — First Controller

So lets write Java for the first time.

kage com.dhis.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

@RequestMapping(value ="/greeting")
public String sayHello (Model model) {

model.addAttribute("greeting", "Hello World");

return "hello";
}

}

This is our first controller and our first entry point to our web app.

@Controller defines this as a controller to which the DispatcherServlet can talk to.

@RequestMapping allows us to define the url mapping

7-JSP Files

We put our jsp files according to our configuration done at servlet-config.xml above. The view resolver we have used is jsp.

<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

Now one just has to run mvn package generate the war and include it in a tomcat container. Or use your IDE to run the project on a tomcat server and launch.

http://localhost:8080/greeting.jsp

If in doubt or if you are stuck ,this is the working codebase for this example. If something is not work look at that code, build it and see where you might need some quick help.

This is what the final project structure should look like at a glance.

Like what you read? Give Dehan de Croos a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.