Spring MVC construction case

Keywords: Java Interview Programmer architecture

Article catalog

  1. What is spring MVC

A long time ago, SSH was the most popular architecture mode(   Spring   Struts encapsulates servlet s   hibernate  );–> Baidu Encyclopedia SSH Framework

Then came SSM(   Spring   Struts   Mybatis  ) ; Note that it's not working here yet   Spring MVC  , Because the web module was not very good during the early development of spring, the web part here is used in the early stage   Struts  ;

Later, the SSM framework says(   Spring   Spring MVC   Mybatis  ) Combination;

Now let's talk about this   spring mvc  ; spring mvc is a module of spring; they do not need any intermediate layer to connect; spring mvc encapsulates servlet s;

Spring mvc is a web framework based on mvc;   Receive external requests, parse parameters and pass them to the service layer

In fact, spring MVC encapsulates the servlet, receives the request sent by the client, calls the service layer for processing, and then responds the processing result to the client

Well, let's see what it is   MVC  ; MVC is an early development architecture idea;

from   M layer (module model layer)   [including pojo entity class, dao data interaction and service service];

V layer (view layer)   [displayed page, such as jsp (actually servlet)];

Layer C (Controller control layer)   [place servlet]

2. Talking about the operation process

Initial contact, a better understanding is to start developing and using it first, and then slowly analyze its specific process, and the learning effect will be better

You can put this   DispatcherServlet   As a request distributor;

First, after DispatcherServlet receives the request, the processing mapper is invoked.   HandlerMapping  ;

This mapper first checks whether there is an interceptor in the request path, if any;

Then the process mapper will find the corresponding url according to the url of the request path   Processor Handler (i.e. Controller)   Concrete control layer; return it to   DispatcherServlet  ;  DispatcherServlet   Start again, according to   Handler   Find a processing adapter; then process the view and model, and then return to   DispatcherServlet  , Distribution processing; response data;

lower left quarter   View resolution   That piece is misspelled;

However, if the print stream is widely used to send response data in JSON format, the process is relatively simple; the specific process can be drawn as follows;

Directly in the control layer, the data information is responded by printing stream

3. Set up spring MVC and use it

The first is the guide package


Pay attention to this   spring-webmvc   The package actually contains   spring-context   Therefore, after using the jar package of spring MVC, I can not use the spring context package;

This case is the case in the previous note. After spring integrates mybatis, it is matched by adding a configuration file;

Learning notes on Spring framework (5) - [integration of Spring framework with Mybatis framework]

stay   webapp   Lower   WEB-INF   Under directory   web.xml   File, configure DispatcherServlet;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        <!--Created at startup servlet-->
    <!--servlet Map all paths-->

Have you noticed that when you use the initial parameters here again, you should refer to the integrated spring configuration file; and the classpath path is marked in front of it; because the case will be deployed and run on the tomcat server at that time; the resource access path should be pointed to correctly;

Well, this should also be added before the paths of several reference files written before; in fact, it has been added when writing this case in a note. Here's a reminder to deepen your memory



Then, configure and enable MVC annotation scanning;


stay   resources   New under directory   spring-mvc.xml  , Placing the configuration of spring MVC

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


Here   spring.xml   Configure the merge file under the;

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

    <!--Enable annotation scanning-->
    <context:component-scan base-package="com.xiaozhire0.ssm"></context:component-scan>

    <!--Merge data connections and transaction management profiles-->
    <import resource="spring-dbandtx.xml"></import>

    <!--merge spring integration mybatis Configuration file for-->
    <import resource="spring-mybatis.xml"></import>
    <!--merge springmvc configuration file-->
    <import resource="spring-mvc.xml"></import>

Controller class construction;

use  @ Controller   Annotation annotation   Class is the spring MVC control class object  ;

Or use annotations  @ RestController  ; Because it contains annotations  @ Controller   Function of

You can use @ RequestMapping(path = "/...") to mark the corresponding request path

Here, I will create the controller package under the ssm package   PersonController  ;

 * @author by CSDN@Xiaozhi RE0
 * @date 2021-11-21 16:51
 * Human control layer;
//Mark this class as control class;
@RequestMapping(path = "/person")
public class PersonController {
    //Automatic assembly service layer;
    PersonService personService;

    //The access path to call this method is actually project / person/save
    @RequestMapping(path = "/save")
    public void toSavePeople(){
       System.out.println("Access the path to the added user---->");
        People people = new People();
        people.setName("A different world from scratch Java development");
        people.setBirthday(new Date());

Configure tomcat server;

Start up test;

An error was also made here: the package path was created incorrectly, and then the server started and reported an error all the time;

This error is reported

21-Nov-2021 18:30:12.796 warning [http-nio-8080-exec-4]
org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping for GET ;

Then I checked the configuration files one by one, redeployed the server, maven cleaned up and recompiled; no errors were found;

It took me a long time to find out that I had built the package in the wrong place

Access path
http://localhost:8080/ssmdemobyxiaozhi/person/save   Successful execution

  For more information, you can pay attention to the official account "w's programming diary" and reply to Java for more information.

Posted by cwiddowson on Wed, 24 Nov 2021 08:01:56 -0800