Enterprise Java

Getting started with annotation based Spring MVC web application

Here is a minimal way to get a Spring 3 MVC project started with Maven.

First create spring-web-annotation/pom.xml file and include the Spring dependency:
 
 
 
 
 
 
 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="UTF-8"?>
    xsi:schemaLocation="
        http://maven.apache.org/POM/4.0.0
        http://maven.apache.org/maven-v4_0_0.xsd">
 
    <modelVersion>4.0.0</modelVersion>
    <groupId>spring-web-annotation</groupId>
    <artifactId>spring-web-annotation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
    </dependencies>
</project>

Now create the Servlet 3 web initializer and the Spring annotation config for the MVC parts in spring-web-annotation/src/main/java/springweb/WebApp.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package springweb;
 
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];
    }
 
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{ WebAppConfig.class };
    }
 
    @Override
    protected String[] getServletMappings() {
        return new String[]{ "/" };
    }
 
    @Configuration
    @EnableWebMvc
    @ComponentScan("springweb.controller")
    public static class WebAppConfig {
    }
}

The WebApp class extends Spring’s built in Servlet3 web initializer code. It allows Servlet3 container such as Tomcat7 to auto detect this web application without the need of web.xml configuration setup. Because of we do not use web.xml, we need this class to allow Spring to hook into the Servlet container to bootstrap their dispatcher servlet. Also instead of typical Spring beans xml file configuration, we now can use all annotation based using WebAppConfig.

Noticed that I have combined the WebAppConfig as inner class, but you can easily move it out as top level class in a full scale application. This is the Spring annotation version of container configuration. You can easily customize the application by adding new @Bean here.

Note: Do not forget to overwrite getServletMappings method with "/", or else your URL request will not direct to the Spring dispatcher for processing! A step that can easily forgotten and you might find your self chasing why Spring controllers are not working.

Above are really the minmal setup you need to start a war project. Next you want to add at least one controller to have some output to verify. Create this controller file spring-web-annotation/src/main/java/springweb/controller/IndexController.java

01
02
03
04
05
06
07
08
09
10
11
12
package springweb.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class IndexController {
    @RequestMapping(value="/")
    public String index() {
        return "index";
    }
}

And now you would need the JSP view spring-web-annotation/src/main/webapp/index.jsp

1
Hello World.

Now cd into spring-web-annotation and execute mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run. You should see your Spring application starts and able to browse http://localhost:8080/spring-web-annotation URL.

There are lot of cool stuff you can do with Spring MVC. Checkout their awesome docs for more details.
 

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button