Spring MVC learning Controller configuration summary

Controller configuration summary

Controller controller

  • The controller is responsible for providing the behavior of accessing the application, which is usually implemented by interface definition or annotation definition.
  • The controller is responsible for parsing the user's request and transforming it into a model.
  • In Spring MVC, a controller class can contain multiple methods.
  • In Spring MVC, there are many ways to configure the Controller

Implementation method:

Implement Controller interface

Controller is an interface. Under the org.springframework.web.servlet.mvc package, there is only one method in the interface;

// The class implementing the interface obtains the controller function
public interface Controller {
    // Process the request and put back a model and view object
    ModelAndView handleRequest(HttpServletRequest var1,HttpServletResponse var2) throw Exception{


  1. Write a Controller class, ControllerTest1
package com.kuang.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// As long as the Controller interface class is implemented, it indicates that this is a Controller
public class ControllerTest1 implements Controller {

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        // Returns a model view object
        ModelAndView mv = new ModelAndView();
        return mv;

  1. After writing, register the requested bean in the Spring configuration file; Then it corresponds to the request path, and class corresponds to the class that processes the request
<bean name="/t1" class="com.kuang.controller.ControllerTest1"/>
  1. Write the front-end test.jsp. Note that it is written in the WEB-INF/jsp directory, corresponding to our view parser
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
  1. Configure Tomcat to run the test. We don't have a project release name here. We configure a /, so we don't need to add a project name to the request,


  • It is an old method to implement the interface Controller and define the Controller
  • The disadvantages are: there is only one method in a Controller. If you want multiple methods, you need to define multiple controllers; The method of definition is troublesome;

Use annotation @ Controller

  • @The Controller annotation type is used to declare that the instance of the Spring class is a Controller;

Annotation with the same effect as @ Controller annotation

  • @Component component
  • @Service service
  • @Controller controller
  • @Respository dao
  • Spring can use the scanning mechanism to find all annotation based controller classes in the program. In order to ensure that spring can find your controller, you need to declare component scanning in the configuration file.
<!-- Automatically scan the specified package, and submit all the following annotation classes to IOC Container management -->
<context:component-scan base-package="com.kuang.controller"/>
  • Add a ControllerTest2 class and implement it with annotations;
package com.kuang.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

// @The class annotated by the Controller is automatically added to the Spring context
* Represents that this class will be taken over by Spring. All methods in the annotated class,
* If the return value is String and a specific page can jump, it will be parsed by the view parser;
public class ControllerTest2 {

	// Map access path
    public String test1(Model model){
    	// Spring MVC will automatically instantiate a Model object to pass values to the view
        // Return to view location
        return "test";

    public String test3(Model model){
        return "test";

  • Run Tomcat and test results

    It can be found that both of our requests can point to a view, but the results of the page results are different. It can be seen from here that the view is reused, and there is a weak coupling relationship between the controller and the view.

Tags: Java Spring SSM mvc

Posted on Sun, 03 Oct 2021 18:04:06 -0400 by keith73