1. The first annotated SpringMVC program

ch01-hello-springmvc: the first annotated springmvc project

The so-called annotated development of SpringMVC means that the processor can be completed by annotating classes and methods in the code

Registration in the springmvc container. Annotated development is the focus .

Requirement: The user initiates a request on the page, and the request is handed over to the springmvc controller object
and the processing result of the request is displayed (a welcome statement is displayed on the result page)

Implementation steps:

  1. New web maven project
  2. Add dependency
    spring-webmvc dependency, indirectly add spring dependency to project
    jsp, servlet dependency
<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!--servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

      <!--springmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>

	<build>
      <plugins>
        <!-- 编码和编译和JDK版本 -->
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
      </plugins>

    </build>

  1. Key point: Register the core object DispatcherServlet of the springmvc framework in web.xml
    1) DispatcherServlet is called the central dispatcher, it is a servlet, and its parent class inherits HttpServlet
    2) DispatcherServlet is also called a front controller (front controller)
    3) DispatcherServlet is responsible for receiving The request submitted by the user calls other controller objects and displays the processing result of the request to the user
  2. Create a request page index.jsp
  3. Create the controller class
    1) Add @Controller annotation to the class, create an object, and put it in the springmvc container
    2) Add @RequestMapping annotation to the method in the class
  4. Create a JSP as the result, and display the processing result of the request.
  5. Create springmvc configuration file (same as spring configuration file)
    1) Declare the component scanner, specify the package name where the @Controller annotation is located
    2) Declare the view resolver to help process the view
<!--声明 springmvc框架中的视图解析器,帮助开发人员设置视图文件的路径-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀:视图文件的路径-->
        <property name="prefix" value="/WEB-INF/view/"/>
        <!--后缀:视图文件的扩展名-->
        <property name="suffix" value=".jsp"/>
    </bean>

Insert picture description here

pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ch01-hello-springmvc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!--servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

      <!--springmvc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
      <plugins>
        <!-- 编码和编译和JDK版本 -->
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
          </configuration>
        </plugin>
      </plugins>

    </build>
</project>

0. Project structure:
Insert picture description here

1. Configure the web.xml file:

<?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">

 <!-- 声明,注册springmvc的核心对象DispatcherServlet
  需要在tomcat服务器启动后,创建DispatcherServlet对象的实例
  为什么要创建DispatcherServlet对象的实例呢?
  因为DispatcherServlet在他的创建过程中,会同时创建springmvc容器对象
  读取springmvc的配置文件,把这个配置文件中的对象都创建好,当用户发起
  请求时,就可以直接使用对象了。

  servlet的初始化会执行init()方法。DispatcherServlet在init()中{
      // 创建容器,读取配置文件
      WebApplicationContext ctx = new ClassPathXmlApplicationContext("springmvc.xml");
      // 把容器对象放入到ServletContext中
      getServletContext().setAttribute(key,ctx);
  }
注册中央处理器-->

    <!--启动tomcat报错
     读取这个文件/WEB-INF/springmvc-servlet.xml失败
     springmvc创建容器对象时,读取的配置文件默认是/WEB-INF/《servlet-name》-servlet.xml
     我们可以不使用这种默认的读取文件的方式,自定义springmvc读取的配置文件所在的位置
    -->

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <!--自定义springmvc读取的配置文件的位置-->
      <init-param>
          <!--springmvc的配置文件的位置的属性-->
          <param-name>contextConfigLocation</param-name>
          <!--指定自定义文件的位置-->
          <param-value>classpath:springmvc.xml</param-value>
      </init-param>

      <!--在tomcat启动后,创建Servlet对象
        load-on-startup:表示tomcat启动后创建对象的顺序。
        它的值是整数,数值越小,tomcat创建对象的时间越早。
        大于等于0的整数
      -->
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
      <!--
            使用框架的时候,url-pattern可以使用两种值
            1. 使用扩展名方式,语法:*.xxx, xxx是自定义的扩展名。常用的方式 *.do、*.action、*.mvc等等
                http://localhost:8080/springmvc/some.do
                http://localhost:8080/springmvc/other.do
            2. 使用斜杠“/”

      -->
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

1. 全限定性类名
该中央调度器为一个 Servlet,名称为 DispatcherServlet。中央调度器的全限定性类名在
导入的 Jar 文件 spring-webmvc-5.2.5.RELEASE.jar 的第一个包 org.springframework.web.servlet
下可找到。


2. <load-on-startup/>:
在<servlet/>中添加<load-on-startup/>的作用是,标记是否在Web服务器(这里是Tomcat)
启动时会创建这个 Servlet 实例,即是否在 Web 服务器启动时调用执行该 Servlet 的 init()方
法,而不是在真正访问时才创建。
它的值必须是一个整数。 
➢ 当值大于等于 0 时,表示容器在启动时就加载并初始化这个 servlet,数值越小,该 Servlet
的优先级就越高,其被创建的也就越早;
➢ 当值小于 0 或者没有指定时,则表示该 Servlet 在真正被使用时才会去创建。 
➢ 当值相同时,容器会自己选择创建顺序。

3. <url-pattern/>
对于<url-pattern/>,可以写为 / ,建议写为*.do 的形式。


4. 配置文件位置与名称
注册完毕后,可直接在服务器上发布运行。此时,访问浏览器页面,控制台均会抛出
FileNotFoundException 异常。即默认要从项目根下的 WEB-INF 目录下找名称为 Servlet 名称
-servlet.xml 的配置文件。这里的“Servlet 名称”指的是注册中央调度器<servlet-name/>标签
中指定的 Servlet 的 name 值。本例配置文件名为 springmvc-servlet.xml。

而一般情况下,配置文件是放在类路径下,即 resources 目录下。所以,在注册中央调
度器时,还需要为中央调度器设置查找 SpringMVC 配置文件路径,及文件名。

Insert picture description here
Insert picture description here

Insert picture description here

2. MyController.java file:

@Controller
	 创建处理器对象,对象放在springmvc容器中
	 使用位置:在类的上面
和Spring中讲的@Service、@Component一样能处理请求的都是控制器(处理器):
MyController能够处理请求,叫做后端控制器(back controller)

@RequestMapping:请求映射,作用时把一个请求地址和一个方法绑定在一起
              一个请求指定一个方法处理
      属性:
         	 1.value是一个String,表示请求的uri地址的(some.do),
         	 value的值必须是唯一的,不能重复。在使用时推荐地址以“/”开头
      使用的位置:
		     1.在方法的上面,常用的
		     2.在类的上面
		       说明:使用@RequestMapping修饰的方法叫做处理器方法或者控制器方法
		                使用@RequestMapping修饰的方法是可以处理请求的,类似Servlet中
		                的doGet()、doPost()。
		                若有多个请求路径均可匹配该处理器方法的执行,则@RequestMapping 
		                的 value 属性中可以写上一个数组。

The addObject() method in the ModelAndView class is used to add data to its Model. The bottom layer of the Model is a HashMap.
The data in the Model is stored in the request scope. SringMVC uses forwarding to jump to the view by default. The data in the model is destroyed when the request ends.

package com.zep.controller;

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

/**
 * @Controller 创建处理起对象,对象放在springmvc容器中
 * 使用位置:在类的上面
 * 和Spring中讲的@Service、@Component一样
 * 能处理请求的都是控制器(处理器):MyController能够处理请求,
 *                              叫做后端控制器(back controller)
 */
@Controller
public class MyController {
    
    
    /**
     * 处理用户提交的请求:springmvc中是使用方法来处理的。
     * 方法是自定义的,可以有多种返回值,多种参数,方法名称自定义
     * 准备使用doSome方法处理some.do请求
     * @RequestMapping:请求映射,作用时把一个请求地址和一个方法绑定在一起,一个请求指定一个方法处理
     *              属性:
     *                  1.value是一个String,表示请求的uri地址的(some.do),value的值必须是唯一的,不能重复。在使用时推荐地址以“/”开头
     *              使用的位置:
     *                  1.在方法的上面,常用的
     *                  2.在类的上面
     *              说明:使用@RequestMapping修饰的方法叫做处理器方法或者控制器方法
     *                      使用@RequestMapping修饰的方法是可以处理请求的,类似Servlet中的doGet()、doPost()
     *              返回值:ModelAndView
     *                  Model:数据,请求处理完成后,要显示给用户的数据
     *                  View:视图,比如jsp等待
     */
    @RequestMapping(value = "/some.do")
    public ModelAndView doSome() {
    
      // doGet()--service请求处理
        // 可以处理some.do请求了,相当于service调用处理完成了
        ModelAndView mv = new ModelAndView();
        // 添加数据,框架在请求的最后部分把数据放入到request作用域
        // 相当于是执行了request.setAttribute("msg","欢迎使用springmvc做web开发")
        mv.addObject("msg","欢迎使用springmvc做web开发");
        mv.addObject("fun","执行的是doSome方法");
        // 指定视图,指定视图的完整路径
        // 框架对视图执行的是forward操作,类似于request.getRequestDispatcher("/show.jsp").forward(request,response);
//        mv.setViewName("/show.jsp");
//        mv.setViewName("/WEB-INF/view/show.jsp");
//        mv.setViewName("/WEB-INF/view/other.jsp");

        // 当配置了视图解析器后,可以使用逻辑名称(文件名),指定视图
        // 框架会使用视图解析器的前缀 + 逻辑名称 + 后缀 组成完整路径,这里就是字符串拼接操作
        // /WEB-INF/view/ + show + .jsp
        mv.setViewName("show");

        // 返回mv
        return mv;
    }
}


Insert picture description here

3. spring-mvc.xml file:
declare component scanner

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--声明组件扫描器-->
    <context:component-scan base-package="com.zep.controller"/>
</beans>

4. Modify the registration of the view resolver.
In order to avoid redundancy in the request resource path and extension, the view resolver
InternalResouceViewResolver introduced the prefix and suffix of the request . In ModelAndView, you only need to give the file name of the page you want to jump to. For the specific file path and file extension, the view parser will automatically complete the splicing.
Insert picture description here
index.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Zep
  Date: 2020/12/2
  Time: 0:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index</title>
</head>
<body>

    <p>第一个springmvc项目</p>
    <p><a href="some.do">发起some.do的请求</a></p>

</body>
</html>

show.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Zep
  Date: 2020/12/2
  Time: 1:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <h3>show.jsp从request作用域获取数据</h3>
    <h3>msg数据:${msg}</h3>
    <h3>fun数据:${fun}</h3>

</body>
</html>

The result:
Insert picture description here
Click initiated the request of some.do
Insert picture description here

to sum up:

Insert picture description here

* The processing flow of SpringMVC request:
1) Visit the index.jsp page, click on the hyperlink, and initiate some.do request
2) Tomcat (read web.xml file-know through url-pattern that the request of .do is for DispatcherServlet)
3 ) DispatcherServlet (according to the springmvc.xml configuration file, we know that some.do request corresponds to the doSome() method)
4) DispatcherServlet forwards some.do to MyController.doSome() method
5) The framework executes doSome() to process the resulting ModelAndView , Forwarded to show.jsp

The above process is simplified:
some.do ——> DispatcherServlet ——> MyController
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44827418/article/details/110458390