Spring MVC Hello World Application
In this article, we are going to create a simple hello world application using the Spring MVC. We created a maven based Spring project that contains a controller, a view(JSP file), and some XML files. Our project looks like below.
Project Structure
Project Files Source Code:
//HelloController.java
It is a controller class file that uses @Controller
annotation and returns an index JSP page as a view component of MVC.
package com.studytonight.controller;
import org.springframework.stereotype.Controller;
@Controller
public class HelloController {
public String hello() {
return "index";
}
}
// spring-mvc-app-servlet.xml
This is a configuration file that contains base-package locator and InternalResourceViewResolver
to fetch view by adding suffix and prefix to the view(JSP) file.
<?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"
xsi:schemaLocation="
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.studytonight.controller" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
// web.xml
This XML file is required when we work with Spring MVC and used to configure the front controller DispatcherServlet and application context file.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-app</display-name>
<absolute-ordering />
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
// index.jsp
It is a view file that will be returned by the controller to the client.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Hello Spring</title>
</head>
<body>
<p>Hello Welcome to Spring MVC</p>
</body>
</html>
Output:
Run the application and you will get the output like below in your browser.
No XML Configuration of Spring MVC
In case, we want to configure the Dispatcher servelet using Java code only (No XML) then include the following files into you project and delete the spring-mvc-app-servlet.xml and web.xml file.
Project Structure
The config files has the following source code.
// MyWebInitializer.java
This class is similar to web.xml and used to configure DispatcherServlet for the Spring application. The getServletConfigClasses()
method is used to load AppConfig.java class.
package com.studytonight.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
// AppConfig.java
This is similar to the application context where we configure our views file. Here, we used InternalResourceViewResolver
class and its methods setPrefix()
, setSuffix()
to configure the view file.
package com.studytonight.config;
import org.springframework.context.annotation.Bean;
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.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
public class AppConfig {
// define a bean for ViewResolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
Add these files to your project and run the project. It will produce similar output.