Spring MVC learning notes to create the first spring MVC program

Premise:
1. Introduce dependency with maven
2. Use intellij idea development tool
3. Deploy in Tomcat

The construction steps are as follows:
1. Create a Web application and import a jar package
2.Spring MVC configuration: on the web Configure the Servlet in XML and create the configuration file of spring MVC
3. Create Controller (the Controller that handles the request)
4. Create a view (this tutorial uses jsp as the view)
5. Deployment and operation

I Create a Web application and introduce a jar package

Create a Web application springmvcDemo, and add the JAR package that Spring MVC depends on in the lib directory of springmvcDemo.

Spring MVC relies on JAR files, including spring's core JAR package and Commons logging JAR package.

Maven project in POM Add the following to the XML file:

<!--test-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<!--journal-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.21</version>
</dependency>
<!--J2EE-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<!--mysql Driver package-->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>
<!--springframework-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.github.stefanbirkner</groupId>
    <artifactId>system-rules</artifactId>
    <version>1.16.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>
<!--Other packages required-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

II. Spring MVC configuration

Spring MVC is based on Servlet. Dispatcher Servlet is the core of the whole spring MVC framework. It mainly intercepts requests and sends them to corresponding processors for processing. Therefore, to configure spring MVC, first define the dispatcher Servlet. Like all servlets, users must be on the web Configuration in XML
1) Define DispatcherServlet
When developing Spring MVC applications, you need to use the web Deploying DispatcherServlet in XML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>springMVC</display-name>
    <!-- deploy DispatcherServlet -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- Indicates that the container loads immediately when it restarts servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- Process all URL -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

During Spring MVC initialization, the configuration file will be found in the WEB-INF directory of the application. The naming rule of the configuration file is servletname servlet XML, such as Spring MVC servlet xml

You can also store the Spring MVC configuration file anywhere in the application directory, but you need to use the init param element of the servlet to load the configuration file, and specify the location of the Spring MVC configuration file through the contextConfigLocation parameter. The example code is as follows.

<!-- deploy DispatcherServlet -->
<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:springmvc-servlet.xml</param-value>
    </init-param>
    <!-- Indicates that the container loads immediately when it restarts servlet -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The Spring resource path is specified here, that is, classpath: springmvc servlet xml.

The above code configures a Servlet named "Spring MVC". This Servlet is of type DispatcherServlet, which is the entry of Spring MVC. It loads this DispatcherServlet at startup through 1 configuration tag container, that is, it starts automatically. Then map to "/" through Servlet mapping, that is, the dispatcher Servlet needs to intercept and process all URL requests of the project.

2) Create Spring MVC configuration file
Create a springmvc servlet in the WEB-INF directory XML file

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

    <!-- LoginController Controller class, mapping to"/login" -->
    <bean name="/login"
          class="net.biancheng.controller.LoginController"/>
    <!-- LoginController Controller class, mapping to"/register" -->
    <bean name="/register"
          class="net.biancheng.controller.RegisterController"/>
</beans>

III. create controller

Create net. Net in src directory biancheng. Controller package, and create two traditional controller classes (implementing the controller interface) of RegisterController and LoginController in this package to process the requests for "registration" and "login" hyperlinks in the home page respectively.

Controller is the controller interface. There is only one method handleRequest in the interface, which is used to process requests and return ModelAndView.

The specific code of RegisterController is as follows.

package controller;

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

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

public class LoginController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        return new ModelAndView("/WEB-INF/jsp/register.jsp");
    }
}

The specific code of LoginController is as follows.

package controller;

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

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

public class RegisterController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception {
        return new ModelAndView("/WEB-INF/jsp/login.jsp");
    }
}

IV. create view

index. The JSP code is as follows.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    Unregistered user, please
    <a href="${pageContext.request.contextPath }/register"> register</a>!
    <br /> Registered users, go to
    <a href="${pageContext.request.contextPath }/login"> Sign in</a>!
</body>
</html>

Create a jsp folder under WEB-INF and add login jsp and register jsp into the jsp folder. login. The jsp code is as follows.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    Login page!
</body>
</html>

register. The JSP code is as follows.

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
<body>
    Registration page!
</body>
</html>
</head>

V. deployment and operation

To deploy the spring mvcdemo project to the Tomcat server, first visit index JSP page, as shown in the following figure.

Tags: Java Spring Spring MVC mvc

Posted by eddedwards on Fri, 17 Dec 2021 12:37:57 +1030