Spring MVC (an entry-level case to learn about spring MVC)

Keywords: Spring xml JSP Maven

Article directory

1. What is spring MVC?

Spring spring: a lightweight framework
MVC - Model, View, Controller: a design pattern, layered development
Schematic diagram of MVC:

Spring MVC can be said to be a follow-up product of spring. In fact, spring provides the MVC module of web Application on the basis of the original. Spring MVC can be simply understood as a module of spring (such as AOP and IOC). It is often said that spring MVC and spring integrate seamlessly on the network. In fact, spring MVC is a sub module of spring, so it is not necessary at all Integrate with spring.

2. Create your first spring MVC project

2.1. Create a maven project first, and add the support of web Framework


So the web folder has~
Many small partners don't know how to add web framework support to a maven project. I'll show you how to do it here~

First of all: create a maven project

Check the web again

So the web framework is added

All right, get back to business and move on

2.2. Configure the server

Since it is a web project, the server must be used. We need to configure our server in IDEA. See the figure:

It's not finished. Configure this one again

OK, the second part is finished

2.3 code subject

2.3.1 import dependency and add resource filter

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.9.RELEASE</version>
    </dependency>
</dependencies>
<!--Because Maven There may be the problem of resource filtering. We will perfect the configuration-->
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

2.3.2. Configuration of 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">


    <!--  Register one DispatcherServlet  Spring Provided-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--        Associated with one SpringMVC Profile for-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--        Start when the server starts-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--
        /  Delegate matches all requests excluding  .jsp file
        /*   Match all requests including  .jsp file
    -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>



</web-app>

2.3.3. Configure the spring MVC core configuration file spring MVC servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!-- Fixed configuration -->
    <!-- Scan package configuration,Control layer, interacting with the front end  controller -->
    <context:component-scan base-package="com.gang.controller"/>
    <!--Annotation drive on-->
    <mvc:annotation-driven/>
    <!--stay web In development, we usually have static resources, css,js,img....-->
    <mvc:default-servlet-handler/>


    <!-- Important: View parser , internalResourceViewResolver-->
    <!-- /WEB-INF/views/hello.jsp -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

2.3.4. Create a new views folder under the WEB-INF folder, and create a JSP page: hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello</title>
</head>
<body>
<h1>
<%-- el Expression value   --%>
     ${msg}
</h1>
</body>
</html>

2.3.5 create a new controller folder and write the code of our control layer in it

The webpage successfully jumps to see if it has entered our method. Print it out
If we want to pass the value to the front end, we just need to add the Model parameter and set the value. The front end uses the EL expression to get the value

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

/*
Description:Control layer
Author:32259
Time:2020 February 2020 / 2 / 2618:07
*/
@Controller
public class HelloController {

    @RequestMapping("/hello")//Address of request mapping
    public String hello(Model model){

        System.out.println("Come in this way!");
        model.addAttribute("msg","Hello,SpringMVC");
        //Return to a page auto splicing / WEB-INF/views/hello.jsp
        return "hello";
    }


}

2.3.6 start the server and view the results

Let's take a look at the background output

OK, see you next~

52 original articles published, praised 12, 2121 visitors
Private letter follow

Posted by imperialized on Wed, 04 Mar 2020 21:51:02 -0800