Spring MVC

Introduction

  • Modal: Any java class that containing data

  • View: JSP which is used to display the modal

  • Controller: A Java class marked with controller annotation which will be used to receive the data, return the modal with result data to the view

Configuration (by xml)

  1. web.xml

  • To tell where the config file is

  • Setup the dispatcherServlet so as to send the user request to corresponding controller to handle the request and return the correct view

  • The output file(war) will be generated based web.xml so as to do deployment

<?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-demo</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>
		<!-- Spring creates a WebApplicationContext object 
			based on the bean definitions 
		and stores it in the ServletContext of one's web application. -->
		<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>

2. spring-mvc-demo-servlet.xml

  • To perform component scanning

  • Allow the support of validation

  • Define the pattern of getting the view

Configuration (By Annotation)

Binding with modal

  • Basically, just put the java object into model

Validation

  • To validate whether the user input is matched to the ideal pattern or not

  • If not , return to the same page with error message

Controller

View

Last updated

Was this helpful?