Mastering Spring MVC

In this article I will be covering Spring MVC web framework. There are separated and different articles written regarding the framework. I will try to keep it simpler and more clear for you.

Spring MVC web framework is established on Model View Control web design patter which has been commonly and widely used in many of web applications. You may read the main architecture of MVC pattern here on wikipedia

When our web application has been launched, we expect and requests from the users, first of all entire request comes to the spring servlet called dispatcherServlet.xml which handles requests, moreover it does address and forward the upcoming user requests to controllers. The below schema explains us the further details of the inner architecture of spring web framework dispatcherServlet

spring1

(Image courtesy spring in practice)

To get started with the project first of all in our deployment configuration file web.xml, we need to add the required parameters for our project to meet spring framework and accept incoming user requests. The following web.xml should be like this:

WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>SimpleSpringWebApp</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Hereby what we see with this configuration file is that we are defining our spring configuration to web.xml and showing spring configuration file. There is one important point here is that I named spring config file as mvc-dispatcher-servlet.xml. You may name it they way you want as long as it has been also defined in servlet file as well, but the last part has to ve XYZ-servlet.xml

WEB-INF/mvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		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">

    <!-- Uncomment and your base-package here:
         <context:component-scan
            base-package="org.springframework.samples.web"/>  -->

    <mvc:annotation-driven />

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <property name="prefix" value="/WEB-INF/view/"/>
	        <property name="suffix" value=".jsp"/>
	</bean>

</beans>

 

Leave a Reply

Your email address will not be published. Required fields are marked *