Spring MVC with XML Configuration

When making a Spring Framework web application, this Spring Framework is provided with the Spring MVC module. Some of the important points are:
     • Spring Framework for making web applications in Java
     • It is based on the Model-View-Controller design pattern for making web applications
     • Supports features of the Core Spring Framework(IoC, DI)

Model-View-Controller(MVC)

spring_mvc_config_xml

→ The Front controller is called a DispatcherServlet.
→ Model is the Java Bean class or POJO class.
→ Controller class does not contain the business logic of the application. It has delegation capability only. 
→ View is a web page(JSP, Thymeleaf, etc.).    

Development Process

    1. Creating a Project
    2. Project Structure
    3. Adding the jar file 
    4. Creating a View page 
    5. Configuring Spring DispacherServlet
    6. Add configuration to a file
    7. Creating a Controller Class
    8. Run the App

1. Creating a Project

We are creating a Project in the Eclipse IDE. You can create a project in another IDE which you are using. 

2. Project Structure 

3. Adding the jar file

common-logging-<version>.jar
spring-aop-<version>.jar
spring-context-<version>.jar
spring-core-<version>.jar
spring-expression-<version>.jar
spring-web-<version>.jar
spring-webmvc-<version>.jar

→ Paste these jars into the WEB_INF/lib directory of the project and these jars are automatically added to the Java Build Path/Classpath.

4. Creating a View page

We are creating jsp view page home.jsp for this project
home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<body>
<h2>Spring MVC Demo - Home Page</h2>
</body>
</html>

→ This JSP page is created in the WEB-INF/view directory.

5. Configuring Spring DispacherServlet

web.xml

<?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_4.xsd"
id="WebApp_ID" version="4.0">

<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Spring MVC Configs -->
<!--  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/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

→ This web.xml file is created in the WEB-INF directory of the application.
→ Client requests are entered into the web application via FrontController. In the Spring MVC DispatcherServlet is the FrontController. This is the gateway only which does not contain real web application logic or business logic of the application. It is delegated to the POJO class as annotated with the @Controller annotation. The Controller class delegates to the Server class which contains business logic and delegates to the Repository class which contains persistence logic. Once work has been done, the FrontController is responsible for producing output as a view page (JSP page, Velocity template, or Thymeleaf template). The techniques are used to decide which Controller and which method inside that Controller contains which handles the request and renders the view page as a response.
→ DispatcherServlet is a class available in this package org.springframework.web.servlet.
→ This tag <url-pattern>/</url-pattern> means the FrontController is ready to handle that start from ” / “. We can change this according to our needs.
→ The DispatcherServlet initialises once when deploying the application on the Tomcat server.
→ The <load-on-startup>1</load-on-startup> tag means DispatcherServlet initialized during the time of the server startup.

6. Add configuration to file

dispatcher-servlet.xml

<?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">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.poc" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- 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>

7. Creating a Controller Class

HomeController.java

package com.poc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

@RequestMapping("/")
public String showPage() {

return "home";

       }
}

→ @Controller: Specialization of the @Component annotation, useful to configure classes as the controller of the Spring MVC app to process requests given by clients.
→ @RequestMapping: This is used to map the web requests onto specific handler classes and handler methods.

8. Run the App

spring_mvc_config_xml

You can check out this article Spring MVC with Spring Boot Configure to create web applications in Spring Framework in an easy way.

Conclusion

This example is explained How to configure a web application in Spring Framework. What is the use of the FrontController class in Spring MVC? What is the use of @RequestMapping annotation in the application?

Leave a Comment