Web MVC framework
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet
that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files.
"Open for extension…" A key design principle in Spring Web MVC and in Spring in general is the
"Open for extension, closed for modification" principle.
Features of Spring Web MVC
Clear separation of roles.
Powerful and straightforward configuration of both framework and application classes as JavaBeans
Adaptability, non-intrusiveness, and flexibility
Reusable business code, no need for duplication
Customizable binding and validation.
Customizable handler mapping and view resolution
Flexible model transfer
Customizable locale, time zone and theme resolution.
A simple yet powerful JSP tag library known as the Spring tag library that provides support for features
such as data binding and themes.
If you do not want to use Spring’s Web MVC, but intend to leverage other solutions that Spring
offers, you can integrate the web MVC framework of your choice with Spring easily.
The DispatcherServlet
Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed
around a central Servlet that dispatches requests to controllers and offers other functionality that
facilitates the development of web applications.
Now we can look into how to Create a sample mvc web application using STS.
Open the STS and select the
File-> New Maven Project.
Leave the selected options available.
In the architypes selection select the web
Give the maven artifact and package details.
click finish to create the project.
The Project structure will be like below.
Now its our time to add the dependencies .Add the Following dependencies. Navigate to pom and paste the below Maven dependencies.
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
</dependencies>
We have added the dependencies, now We need to update the web.xml.
As we already know that the "DispatcherServlet" is required for the dispatching the requests the corresponding controllers. Define in the following way .
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Simple web application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Now we have defined the web.xml contents . Next is to Create the servlet-config.xml. By Default sprinng looks fot the <ServletName>-servlet.xml. Hence my servlet file name will be like spring-servlet.xml.
Here is my config file.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
// we are defining that it works based on the mvc annotation-driven hence we are mentioning like this.
<mvc:annotation-driven />
//Scans for the controller classes.
<context:component-scan
base-package="com.searchendeca.springmvc" />
<mvc:default-servlet-handler />
//View resolver is used to resolve the view. This is very much required what the outout must be.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Create a Controller
Always create the java classes in src/main/java.
package com.searchendeca.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@RequestMapping("/")
public String greetmessage(Model model) {
model.addAttribute("message", "Hello Syed Welcome!");
return "index";
}
}
From the above controller we are defining the mapping be '/' and dispatcher servlet fwds the request for / and in the model attribute we are setting and sending it .
In the view resolver we have defined as the /WEB-INF/jsp/ for the location of jsp. Create a folder there and save the jsp as index.jsp.
<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
${message}
</body>
</html>
This will print the jsp message. If you access with any other context you will get 404. Happy Learning!!!
No comments:
Write comments