Spring MVC Annotation Example

This tutorial shows how to create a Spring MVC Annotation Example or build a basic Spring MVC Web Application without web.xml configuration. Spring MVC 4 is a main part of Spring Frameworks and is great for you to build web applcation.

Table of contents:
1. Project Structure
2. Maven Dependencies
3. Create the Controller
4. Configure Spring MVC with Java Annotation Configuration
5. Configure the Dispatcher Servlet
6. Create the View
7. Deploy Spring MVC Annotation Example

Other interesting posts you may like

Project Structure
Make sure your project looks similar to the following structure.

Spring MVC Annotation Example

Maven Dependencies

We are using the latest version of Spring MVC in this example. One more thing, we are configuring our application using Java Annotation Configuration, we don’t use the web.xml anymore. We need to instruct Maven to ignore the web.xml file by setting the failOnMissingWebXml element to false. Below is the pom.xml file for our project.

Create the Controller
By annotating the class with @Controller annotation. It means that a class serves the role of a controller.

The dispatcher servlet will automatically map the methods defined in the class using the @RequestMapping annotation.

The @RequestMapping annotation to map URLs such as “/” onto an entire class or a particular handler method. In our case, we have applied it on class level too, which says that this class is default handler for all HTTP requests of type ‘/’. @RequestMapping have several attributes [value,method,params,..] which can be used to narrow down the mapping to more specific selection.

The return value is the name of the view. The InternalResourceViewResolver will prefix and suffix the return value to form the real path of the view file name.

Configure Spring MVC with Java Annotation Configuration
The @Configuration annotation indicates that the class can be used by the Spring IoC container as a source of bean definitions.

The @EnableWebMvc is equivalent to in XML. The @EnableWebMvc enables support for the @Controller annotation that uses @RequestMapping to map incomming requests to certain methods.

The @ComponentScan will instruct spring which packages it may scan to discover spring annotated beans.

The InternalResourceViewResolver will prefix and suffix the return value of the controller to construct the real path of the view file. Here The real path of the view file is /WEB-INF/views/index.jsp.

Configure the Dispatcher Servlet
The DispatcherServlet is used to dispatch the requests to the appropriate controller methods. We configure it by extending the AbstractAnnotationConfigDispatcherServletInitializer. Then we register the Spring MVC java Configuration file (Here is MyWebMvcConfig class) with DispatcherServlet in the getServletConfigClasses method.

Create the View
Finally, we build a simple view displaying the value of the message attribute we added earlier in the controller.

Deploy Spring MVC Annotation Example
After building the project by maven we deploy the file war on application server (Tomcat 8 for example). Access the address to see how it works:
URL: http://localhost:8080/spring-mvc-annotation-example/mycontroller
Here is the screen shoot
Spring mvc annotation example deploy

Download complete source code of example, please click link below

Spring-MVC-Annotation-Example.zip (58 downloads)

Source code on Github https://github.com/javabycode/spring-mvc-annotation-example

Leave a Comment


Please share it if you found this useful
Hide Buttons