Spring 4 MVC Hello World Example using Annotation

Spring 4 MVC Hello World Example using Annotation

in this Tutorial we will learn how to create spring 4 mvc hello world example. in spring 4 mvc we need to replace web.xml with initializer class and we need to replace spring configuration file(dispatcher-servlet.xml) with Configuration java class. With out wasting time let,s begin.

Tools Used In This Article

  • Spring 4.3.4.Release
  • Maven 4.0.0
  • JDK 1.6
  • Tomcat 7.0

create maven project from Eclipse like below


Step 1:In Eclipse Goto File>>New>>Other>>Maven Project


Press Next from the above dialog box


now in dialogbox select maven-archetype-webapp


Next enter the Artifact Id and Group Id in dialogbox and click Finish.



Now Your Project Structure will be look like below




Step 2:Add Maven dependencies to pom.xml

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>com.java2study</groupId>
<artifactId>spring4MvcDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring4MvcDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring4MvcDemo</finalName>
</build>
</project>



Step 3:now we need to write java based spring configuration class . because in spring 4 mvc we can use java based configuration instead of spring configuration file(spring-servlet.xml).so we need to write one configuration class that extends the WebMvcConfigurerAdapter class it can give support to java based configuration.

package com.java2study.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.java2study.controller")
public class WelcomeConfiguration extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver ViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setViewClass(JstlView.class);
internalResourceViewResolver.setPrefix("/WEB-INF/jsps/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;

}

}

the above code which is equalant to in spring mvc applications like spring-servlet.xml below.


>

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven>
<context:component-scan base-package = "com.java2study.controller"/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

Step 4:we have to create WelcomeIntializer class that is implementing WebApplicationInitializer.here WebApplicationInitializer is an interface provided by Spring 4 mvc that ensures your java code based configuration is detected and automatically used to initialize any Servlet 3 container. here we need to create servlet configuration class and here we need to use servlet 3.0 + jar file.


package com.java2study.configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WelcomeIntializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(WelcomeConfiguration.class);
context.setServletContext(container);
ServletRegistration.Dynamic registration = container
.addServlet("dispatcher", new DispatcherServlet(context));
registration.setLoadOnStartup(1);
registration.addMapping("/");

}

}

The above code is equalant to web.xml file like following


<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Step 5:now create controller class annotated with @Controller.and need to use @RequestMapping with method to map input url request to controller requests.

package com.java2study.controller;

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

@Controller
public class WelcomeController {
@RequestMapping("/welcome")
public String welcome() {
return "welcome";

}

}

Step 6:now create view pages like below


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 4 MVC</title>
</head>
<body>
<h2>welcome to spring 4 mvc tutorials </h2>
</body>
</html>

Step 7:now build and run the application

to do this go to project and choose RunAs maven install

after do the maven install you will see the project structue like below


Step 8: now copy the war file to webapps folder in tomcat folder. now run the below url in browser


output

-->