一文学会SpringMVC,SpringMVC最简单的入门教程(万字好文)

第一章 SpringMVC概述

1.1 SpringMVC基本说明

SpringMVC是基于spring的,是spring中的一个模块,做web开发用的。springmvc叫做spring web mvc,说明他是spring的核心技术,做web开发,springmvc内部是使用mvc框架模式。

SpringMVC是一个容器,管理对象的,使用IoC核心技术。

SpringMVC管理界面层中的控制器对象。

SpringMVC底层也是Servlet,以Servlet为核心,接收请求,处理请求。显示处理结果给用户。

处理用户的请求:

用户发起请求—SpringMVC—Spring—MyBatis—Mysql数据库

1.2 SpringMVC中的核心Servlet – DispatcherServlet

DispatcherServlet是框架中的一个Servlet对象。负责接收请求,响应处理结果。

DispatcherServlet他的父类是HttpServlet

DispatcherServlet也叫做前端控制器(front controller)

SpringMVC是管理控制器对象,原来没有SpringMVC之前使用Servlet作为控制器对象使用。现在通过SpringMVC容器创建一种叫做控制器的对象,代替Servlet行使控制器的功能。

例子:

需求:用户发起一个请求,springmvc接收请求,显示请求的处理结果

servlet的实现方式:
    jsp发起请求---servlet---jsp显示结果

步骤:
1.新建web应用
2.加入web依赖
    spring-webmvc依赖(springmvc框架依赖),servlet依赖

3.声明springmvc核心对象DispatcherServlet
    1) DispatcherServlet是一个Servlet对象
    2) DispatcherServlet叫做前端控制器(front controller)
    3) DispatcherServlet作用:
        1.在servlet的init()方法中,创建springmvc中的容器对象
        2.作为servlet,接收请求

4.创建一个jsp,发起请求

5.创建一个普通的类,作为控制器使用(代替之前的servlet)
    1) 在类的上面加入@Controller注解
    2) 在类中定义方法,方法的上面加入@RequestMapping注解
       方法处理请求的,相当于servlet的doGet,doPost

6.创建作为结果的jsp页面

7.创建springmvc的配置文件(spring的配置文件一样)
    1) 声明组件扫描器,指定@Controller注解所在的包
    2) 声明视图解析器对象

pom.xml文件:

<?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>SpringMVC-01</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>SpringMVC-01 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.3.4</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>SpringMVC-01</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

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

  <!--声明springmvc的核心对象
      访问mymvc地址后,报错 文件没有找到。找的文件 /WEB-INF/springmvc-servlet.xml
                                           /WEB-INF/myweb-servlet.xml
      错误原因:
        在Servlet的init()方法中,创建springmvc使用的容器对象WebApplicationContext
        WebApplicationContext ctx = new ClassPathXmlApplicationContext(配置文件).

        配置文件的默认枯井:/WEB-INF/<servlet-name>-servlet.xml
  -->

  <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>
    <!--
      表示服务器tomcat创建对象的顺序,是个整数值,大于等于0
      数值越小,创建对象的时间越早
    -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--
      url-pattern 作用:把一些请求交给指定的servlet处理
      使用中央调度器DispatcherServlet
      1. 使用扩展名方式,格式:*.xxx,xxx是自定义的扩展名,
         例如 *.do,*.action,*.mvc 等等,不能使用*.jsp
         http://locahost:8080/myweb/some.do
         http://locahost:8080/myweb/user/list/queryUser.do
         http://locahost:8080/myweb/user/list/list.action

      2. 使用斜杠 "/"
    -->
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

springmvc-servlet.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"
       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.lu.controller"></context:component-scan>

    <!--声明视图解析器:帮助处理视图-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--前缀:指定视图文件的路径-->
        <property name="prefix" va
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

再让我学一会吧!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值