Spring MVC quick start spring MVC annotated development project

The so-called annotated development of spring MVC means that the processor can be registered in the spring MVC container through the annotation of classes and methods in the code. Annotated development is the focus.

Spring 2.5 adds the Spring MVC annotation function to replace the traditional XML based Spring MVC configuration.

stay The first configured spring MVC program A traditional style Controller is created in. It is a class that implements the Controller interface. The traditional style Controller not only needs to deploy the mapping in the configuration file, but also can only write one processing method, which is not flexible enough.

The first annotated spring MVC project

Annotated items also need a central fetcher, view parser. Create a new Moudle, spring mvc-03-hello-annotation

The examples in this section are based on The first configured spring MVC program The code implementation in the section.

Write Controller

Next, we write a common Controller class, which is the most written class in the process of developing Spring MVC programs. The Controller class is usually called Controller, in which logic such as receiving parameters, calling business methods, and returning to the view page are written.

Add corresponding annotations on classes and methods.

Method is declared to bring the data in the Action to the view

The result returned by the method is the view name hello, which is not the full page path. Finally, it will be parsed into the full page path and jump through the view parser.


@The Controller annotation is used to automatically scan the Controller class when the Spring IOC container is initialized

Register component scanner

The component here is the processor. You need to specify the basic package where the processor is located.

Spring MVC uses the scanning mechanism to find all annotation based controller classes in the application. Therefore, in order for the controller class to be scanned by the spring MVC framework, you need to declare the spring context in the configuration file and specify the basic package of the controller class using the < context: component scan / > element (please ensure that all controller classes are under the basic package and its sub packages).


There are multiple methods to process requests in a controller, such as adding users, modifying user information, deleting specified users, obtaining user list according to conditions, etc. Each method is responsible for different request operations, and @ RequestMapping is responsible for mapping the request to the corresponding controller method.

In the annotation based controller class, the corresponding processing method can be written for each request. Use the @ RequestMapping annotation to correspond the request to the processing method one by one.

@The RequestMapping annotation can be used on a class or method. Used on a class to indicate that all methods in the class that respond to requests take this address as the parent path. Here, because there are mappings on classes and methods, the access should be / HelloController/hello

If multiple request paths can match the execution of the processor method, an array can be written in the value attribute of @ RequestMapping.

Configure springmvc.xml

Add the springmvc-servlet.xml configuration file in the resource directory. The configuration form is basically similar to the Spring container configuration. In order to support annotation based IOC, the function of automatic package scanning is set. The specific configuration information is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

   <!-- Automatically scan the package to make the annotations under the specified package effective,from IOC Unified container management -->
   <context:component-scan base-package="com.kuang.controller"/>
   <!-- Give Way Spring MVC Do not process static resources -->
   <mvc:default-servlet-handler />
   support mvc Annotation driven
       stay spring Generally used in@RequestMapping Annotation to complete the mapping relationship
       To make@RequestMapping Note effective
       You must register with the context DefaultAnnotationHandlerMapping
       And one AnnotationMethodHandlerAdapter example
       These two instances are handled at the class level and method level, respectively.
       and annotation-driven Configuration helps us automatically complete the injection of the above two instances.
   <mvc:annotation-driven />

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


In the view parser, we store all views in the / WEB-INF / directory, which can ensure the view security, because the files in this directory cannot be accessed directly by the client.

  • Make IOC comments effective
  • Static resource filtering: HTML. JS. CSS. Pictures, videos
  • Annotation driven MVC
  • Configure view parser

Define view page

Create hello.jsp in the WEB-INF/ jsp directory, and the Controller method will jump to this view after execution; The view can directly fetch and display the information brought back from the Controller

The value or object stored in the Model can be retrieved through EL representation;

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

Configure Tomcat run

Configure Tomcat, start the server and access the corresponding request path!

Tags: Java Spring mvc

Posted on Thu, 21 Oct 2021 14:44:44 -0400 by thephpguy