Basic configuration of spring MVC

Record the steps to build the spring MVC framework

I. create a new Java Web project, springmvc1, and import the jar package in lib

This project uploads GitHub, which is convenient to download jar package

2. web.xml file under Web inf

Among them, serve name's spingmvc will be used in the next step.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" version="2.4">

3. Create springmvc-servlet.xml in the same path

The spring MVC in the filename corresponds to the spring MVC in the servlet name in the previous step

<?xml version="1.0" encoding="UTF-8" ?>
    <bean id="simpleUrlHandlerMapping"
        <property name="mappings">
                <prop key="/hello">indexController</prop>
    <bean id="indexController" class="controller.IndexController"></bean>

/hello specifies the path entered on the waiting browser

IV. create a new IndexController class in src to implement the Controller interface

package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class IndexController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("index.jsp");
        mav.addObject("message", "Hello Spring MVC");
        return mav;

The model in ModelAndView represents the model and view represents the view. This name explains the function of this class well. After the business processor calls the model layer to process the user's request, it stores the result data in the model attribute of the class, stores the view information to be returned in the view attribute of the class, and then returns the model and view to the Spring MVC framework. The framework parses the object by calling the view parser defined in the configuration file, and finally displays the result data on the specified page (index.jsp).

V. index.jsp is used to display

Use the el expression ${message}

Vi. deploy to tommcat for testing

Browser input http://localhost:8080/springmvc1/hello


Tags: Java xml Spring JavaEE

Posted on Sat, 09 Nov 2019 10:45:44 -0500 by Saeven