reset password

Spring MVC

Make the following changes to the Maven web application we created in the previous step:

1. Add the dependency org.springframework:spring-webmvc:3.2.3.

At the time of writing the latest Spring Framework release is 4.0.3. The reason we use 3.2.3 instead is that later we'll add Spring Security which depends on Spring 3.2.3. If you don't plan to use Spring Security, feel free to use the latest Spring Framework; otherwise be aware that Spring Security usually lags a little behind the main framework release.

2. Add the following to web.xml:

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

The DispatcherServlet is loaded when the web application is started (notice the <load-on-startup>), and it is responsible for initializing the Spring servlet context, i.e. creating all the beans specified in a bean configuration file. The default name for the bean configuration file is /WEB-INF/<dispatcher-servlet-name>-servlet.xml. In our case, the name of the DispatcherServlet is springmvc (notice the <servlet-name>) so the bean configuration file is /WEB-INF/springmvc-servlet.xml. During operation DispatcherServlet also serves as the front controller that dispatches requests to the proper controller based on controller URL mapping.

3. Create the Spring bean configuration file /WEB-INF/springmvc-servlet.xml as follows:

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven />

    <context:component-scan base-package="springmvc.web" />

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

</beans>

<mvc:annotation-driven /> enables request dispatching to @Controllers among some other things. <context:component-scan> finds all the annotated beans in springmvc.web and its sub-packages. The InternalResourceViewResolver maps a view name to a JSP file. It has two properties: prefix and suffix, so a view name like "home" would be mapped to a JSP file /WEB-INF/jsp/home.jsp.

And this is pretty much it - your web application is now a Spring MVC application. You can create a controller and a view like the following:

And after that you can run the project and check if it works at the URL http://localhost:8080/springmvc/home.html .

This page has been viewed 26951 times.