Simple Spring MVC with java config

In this tutorial we will create simple web application with java-based (annotations) configuration, without any spring xml files.

Technologies used:

1. Spring 3.2.9.RELEASE
2. Maven 3

The maven dependencies are as follows:

<dependencies>
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-beans</artifactId>  
        <version>3.2.9.RELEASE</version>  
    </dependency>  
    <dependency>  
        <groupId>org.springframework</groupId>  
        <artifactId>spring-context</artifactId>  
        <version>3.2.9.RELEASE</version>  
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.2.9.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>
</dependencies>

For the simple project, all java-based configuration is defined in one class:

package com.devcases.springMVC.config;

import org.springframework.context.annotation.Bean;
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.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = {"com.devcases.springMVC.controller"})
@EnableWebMvc
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Bean 
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

The @Configuration annotation tells spring, that this class can be used as a source of bean definitions. The @Bean annotation tells, that the methos return the object, that should be registered as bean in application context, in our example we are returning InternalResourceViewResolver.
@EnableWebMVC is equivalent to <mvc:annotation-driven/> element in XML configuration  – it enables support for @Controller, @RequestMapping and others.
@ComponentScan is the same as <context:component-scan/> element in XML.

In web.xml we have to declare dispatcherServlet and tell it, where the configuration is located:

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>sample</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextClass</param-name>
      <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
    </init-param>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
                com.devcases.springMVC.config
            </param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>sample</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

To test our configuration, we will create one controller:

>package com.devcases.springMVC.controller;

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

@Controller
public class AppController {
    
    @RequestMapping(value="/index")  
    public ModelAndView goToHelloPage() {
        
        ModelAndView view = new ModelAndView();  
        view.setViewName("index");
          
        return view;  
    }  
}

And the page, which controller will be serving:

<html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Hello Spring MVC!</h1>
    </body>
</html>

The source code can be downloaded from here.

Leave a Reply

Your email address will not be published. Required fields are marked *