Everyone can learn how to quickly build the Spring MVC framework (IDEA environment, Gradle construction, introduction to xml configuration file) to make the introduction to Spring simple and clear

preface

What is MVC mode

In the classic MVC mode, m refers to the business model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation codes of M and V, so that the same program can use different forms of expression—— An idea of design pattern

Spring MVC is a module belonging to the spring framework, which can quickly develop MVC pattern applications. It is an implementation of MVC pattern and a tool out of the box.

The MVC mode process is summarized as follows:

What is Spring

  • Spring is the most popular enterprise Java application development framework. Millions of developers from all over the world use spring framework to create high-performance, easy to test and reusable code.

  • The Spring framework is an open source Java platform. It was originally written by Rod Johnson and first released under the Apache 2.0 license in June 2003.

  • Spring is a lightweight framework, and its basic version is only about 2 MB in size.

  • The core feature of the Spring framework is that it can be used to develop any Java application, but building web applications on the Java EE platform needs to be extended. The goal of Spring framework is to make J2EE development easier to use and promote good programming practice by enabling POJO based programming model.

-w3cshool

Simply put, Spring is a set of tools that enable developers to quickly build enterprise applications (including security components, control management and other common modules)

1, Tool introduction

1.1 IDEA development environment

The full name of IDEA is IntelliJ IDEA. It is an integrated environment for Java language development. IntelliJ is recognized as one of the best java development tools in the industry. Here I use the following version

The most prominent function of Intellij IDEA is debugging, which can debug Java code, JavaScript, JQuery, Ajax and other technologies. In addition, there are many convenient plug-ins for developers to download with one click.

Because there are many online installation tutorials, I won't repeat them here.

1.2 Gradle build tool

Gradle is one of the core tools recommended in the Java developer's Guide. Many open source projects are built with gradle. Thanks to the flexible syntax of gradle, developers can flexibly arrange construction tasks according to their actual needs, but gradle is not the only choice for project construction.

Simply put: Gradle is equivalent to a treasure chest. By simply explaining what you need, it can automatically help you download to the corresponding location. The Spring framework is just one of the tools.

IDEA comes with common construction tools, such as the familiar mevan.

1.3 Tomcat server program

Tomcat server: it is a free open source Web application server. It is a lightweight application server. It is widely used in small and medium-sized systems and when there are not many concurrent access users. It is the first choice for developing and debugging JSP programs. The latest Servlet and JSP specifications can always be reflected in Tomcat.

See my article for the configuration of Tomcat—— Simple construction of IDEA's Web Framework (full illustration)

2, Configuration process

The overall process is as follows:

Project - New - empty project - set location and project name - prompt to create a new module (as shown in Figure 1)

Select Gradle and check Java, Web Library - set file location and project name - finish (Figure 2)

Wait for IDEA to automatically import Gradle tools and Web related libraries. The structure directory is shown in Figure 3

stay https://mvnrepository.com/ This website searches for spring web and spring webmvc. I chose 4.3 Version 18 (Figure 4-figure 5)

Copy the above path to build The dependencies {/ / here} of the gradle file, as shown in Figure 6

The following icon will appear - just click (wait a long time and Gradle will load the relevant package) (Figure 7)

Then write the relevant configuration file - web xml,spring-mvc.xml, as like as two peas, are built exactly the same. (Figure 8)

——The contents of the file are at the end and can be copied

Figure 1

Figure 2

Figure 3

Figure 4 - Figure 5

Figure 6

Figure 7

Figure 8

spring-mvc.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--    Configure automatic scanning package, automatic scanning controller-->
    <context:component-scan base-package="person.test.controller"/>


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

    <mvc:annotation-driven></mvc:annotation-driven>

    <mvc:resources mapping="/views/**" location="WEB-INF/views/"/>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

MyController

package person.test.controller;

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

@Controller
public class MyController {
    @RequestMapping("/student/list.do")
    public String list(){
        //Go to WEB-INF / views / liststudents jsp
        return "listStudents";
    }
}

listStudents.jsp can write anything

3, Configure Tomcat and start

The URL of the following interface is the default URL for startup

The following context is the URL that actually accesses the project

That is, I start Tomcat to run, automatically start the browser and jump to——
http://localhost:8080/Gradle_Model/
This indicates default access
http://localhost:8080/Gradle_Model/index.jsp

To access the controller, you need to access
http://localhost:8080//Gradle_Model/student/list.do
——Interception is mentioned in the configuration of spring MVC do ends the request and forwards it to the controller
——The method annotation of the controller receives the "/ student/list.do" request

    @RequestMapping("/student/list.do")
    public String list(){
        //Go to WEB-INF / views / liststudents jsp
        return "listStudents";
    }

After execution, the returned to liststudents JSP page

That is, it implements C (controller) - "M (method process, no operation at present) -" V (generally jsp pages)

ending

The author has been updated for a long time. If you think this article is good, please give me a great praise!!!
If you agree with this article, please follow me and keep learning more wonderful blog posts!!!

The author focuses on the direction of Java Web. He usually updates the articles on basic concepts of Java Web, as well as the series of algorithms and data structures -- [one-day double questions - see what you know]. At the same time, the series of design patterns will be completed within one month as soon as possible

Keywords: Gradle Spring Spring MVC intellij-idea

Added by wendu on Mon, 27 Dec 2021 08:38:04 +0200