SpringMVC第一篇_SpringMVC入门案例

SpringMVC入门案例

1. 需求分析

在这里插入图片描述

  1. 需要搭建开发的环境
  2. 编写入门程序

2. 搭建开发环境

2.1 将Tomcat服务器和IDEA集成

教程: https://blog.csdn.net/weixin_44086832/article/details/105712283

2.2 创建文件夹 springmvc

并用IDEA打开该文件夹

2.3 创建Module springmvc_day01_01_start

在这里插入图片描述

在这里插入图片描述

2.4 将 springmvc_day01_01_start添加到Tomcat中

完成此步骤之前先把Tomcat和IDEA集成

在这里插入图片描述

在这里插入图片描述

2.5 在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>cn.itcast</groupId>
  <artifactId>springmvc_day01_01_start</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>springmvc_day01_01_start 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>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.0.2.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>

  </dependencies>

  <build>
    <finalName>springmvc_day01_01_start</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>

2.6 在main目录下创建java目录,并将其设置为源码根目录

选中java -> 右键 -> Make Directory as -> Sources Root

2.7在main目录下创建resources目录,并将其设置为资源根目录

选中resources -> 右键 -> Make Directory as -> Resources Root

2.8 在resources目录下创建配置文件springmvc.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

2.9 在web.xml中完成Servlet相关配置

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

2.10 目录结构

在这里插入图片描述

3. 代码编写

3.1 完善 web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

说明: web.xml为全局配置文件,服务器开启时加载, 需要在web.xml中声明以下三个部分

  1. 前端控制器 Servlet

     <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        
      </servlet>
    
  2. 在Servlet中声明 SpringMVC配置文件所在路径并设置优先级,保证开启服务加载Servlet的时候同时加载该配置文件

     <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--声明SpringMVC配置文件所在路径-->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    	<!--设置优先级-->
        <load-on-startup>1</load-on-startup>
      </servlet>
    
  3. 在web.xml中声明受到Servlet控制的请求

 <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

3.2 创建控制器类HelloController

/**
 * 控制器类
 */
@Controller
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello");
        return "success";
    }

}

3.3 在webapp目录下创建pages/success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>成功</h3>
</body>
</html>

3.4 完善springmvc.xml

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

    <!--开启注解扫描-->
    <context:component-scan base-package="cn.itcast.controller"></context:component-scan>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--开启SpringMVC框架注解的支持-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

3.5 完善index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门程序</h3>
    <a href="hello">入门程序</a>
</body>
</html>

3.6 开启服务器运行

可正常运行

3.7 目录结构

在这里插入图片描述

4. 运行流程

4.1 启动服务器,加载配置文件

  1. 加载web.xml, 创建DispatcherServlet对象
  2. 之后加载springmvc.xml
  3. 将HelloController注入IOC容器中,并创建HelloController对象

4.2 发送请求,后台处理请求

在这里插入图片描述

5. 组件介绍

5.1 处理流程

在这里插入图片描述

5.2 组件介绍

  1. DispatcherServlet: 前端控制器

    • 用户请求到达前端控制器,它就相当于 mvc 模式中的 controller(控制中心)

    • dispatcherServlet 是整个流程控制的中心,由它调用其它组件处理用户的请求

    • dispatcherServlet 的存在降低了组件之间的耦合性

  2. HandlerMapping:处理器映射器

    • HandlerMapping 负责根据用户请求找到 Handler 即处理器
    • SpringMVC 提供了不同的映射器实现不同的映射方式,如:配置文件方式,实现接口方式,注解方式等
  3. Handler:处理器

    • 它就是我们开发中要编写的具体业务控制器

    • 由 DispatcherServlet 把用户请求转发到 Handler, 由 Handler 对具体的用户请求进行处理

  4. HandlAdapter:处理器适配器

    • 通过 HandlerAdapter 对处理器进行执行,这是适配器模式的应用
    • 通过扩展适配器可以对更多类型的处理器进行执行
  5. View Resolver:视图解析器

    View Resolver 负责将处理结果生成 View 视图,View Resolver 首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成 View 视图对象,最后对 View 进行渲染将处理结果通过页面展示给用户

  6. View:视图

    • SpringMVC 框架提供了很多的 View 视图类型的支持,包括:jstlView、freemarkerView、pdfView 等。我们最常用的视图就是 jsp
    • 一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面

补充: <mvc:annotation-driven>说明

  • 在 SpringMVC 的各个组件中,处理器映射器、处理器适配器、视图解析器称为 SpringMVC 的三大组件
  • 使用<mvc:annotation-driven>相当于 自动加载 RequestMappingHandlerMapping (处理映射器)和 RequestMappingHandlerAdapter (处 理 适 配 器 )
  • 在 SpringMVC.xml 配 置 文 件 中可以 使 用<mvc:annotation-driven>替代处理器映射器和处理器适配器的配置

6. RequestMapping中属性介绍

6.1 path和value

path: 指定请求路径的url , value作用个path相同

6.2 method

指定该方法的请求方式

案例:

修改HelloController.java

/**
 * 控制器类
 */
@Controller
//@RequestMapping("user")
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello");
        return "success";
    }


     //测试method属性
    @RequestMapping(value = "testRequestMapping",method = {RequestMethod.POST})
    public String testRequestMapping(){
        System.out.println("RequestMapping");
        return "success";
    }


}

说明: method后可以接数组

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门程序</h3>
    <%--<a href="hello">入门程序</a>--%>

    <%--测试RequestMapping可以写在类上--%>
    <%--<a href="user/testRequestMapping">RequestMapping</a>--%>

    <%--测试RequestMapping中的 method属性--%>
    <a href="testRequestMapping">RequestMapping</a>

</body>
</html>

说明: a标签中 href属性的路径前边不能加/不能, 即<a href="user/testRequestMapping">RequestMapping</a>会报错

原因: href属性的路径为相对路径

6.3 params

指定限制请求参数的条件

HelloController.java

/**
 * 控制器类
 */
@Controller
//@RequestMapping("user")
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello");
        return "success";
    }

//    @RequestMapping(value = "testRequestMapping")
//    public String testRequestMapping(){
//        System.out.println("RequestMapping");
//        return "success";
//    }
//     //测试method属性
//    @RequestMapping(value = "testRequestMapping",method = {RequestMethod.POST})
//    public String testRequestMapping(){
//        System.out.println("RequestMapping");
//        return "success";
//    }

    //测试param属性
    @RequestMapping(value = "/testRequestMapping",params = {"username"})
    public String testRequestMapping(){
        System.out.println("RequestMapping");
        return "success";
    }

}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门程序</h3>
    <%--<a href="hello">入门程序</a>--%>

    <%--测试RequestMapping可以写在类上--%>
    <%--<a href="user/testRequestMapping">RequestMapping</a>--%>

    <%--测试RequestMapping中的 method属性--%>
    <%--<a href="testRequestMapping">RequestMapping</a>--%>

    <%--测试RequestMapping中的params属性--%>
    <a href="testRequestMapping?username=ok">RequestMapping</a>
</body>
</html>

此时运行的时候会报 405的错误,显示请求方法错误

原因: <a></a>标签默认的methodGET

6.4 headers

发送的请求中必须包含的请求头

HelloController.java

/**
 * 控制器类
 */
@Controller
//@RequestMapping("user")
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello");
        return "success";
    }

//    @RequestMapping(value = "testRequestMapping")
//    public String testRequestMapping(){
//        System.out.println("RequestMapping");
//        return "success";
//    }
//     //测试method属性
//    @RequestMapping(value = "testRequestMapping",method = {RequestMethod.POST})
//    public String testRequestMapping(){
//        System.out.println("RequestMapping");
//        return "success";
//    }

    //测试param属性
//    @RequestMapping(value = "/testRequestMapping",params = {"username"})
//    public String testRequestMapping(){
//        System.out.println("RequestMapping");
//        return "success";
//    }

//    测试header属性
    @RequestMapping(value = "/testRequestMapping",headers = {"Accept"})
    public String testRequestMapping(){
        System.out.println("RequestMapping");
        return "success";
    }
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门程序</h3>
    <%--<a href="hello">入门程序</a>--%>

    <%--测试RequestMapping可以写在类上--%>
    <%--<a href="user/testRequestMapping">RequestMapping</a>--%>

    <%--测试RequestMapping中的 method属性--%>
    <%--<a href="testRequestMapping">RequestMapping</a>--%>

    <%--测试RequestMapping中的params属性--%>
    <%--<a href="testRequestMapping?username=ok">RequestMapping</a>--%>

    <%--测试RequestMapping中的header属性--%>
    <a href="testRequestMapping">RequestMapping</a>

</body>
</html>

说明: 浏览器会自动添加header,所以还会正常运行

6.5 RequestMapping可写在类上

HelloController.java

/**
 * 控制器类
 */
@Controller
@RequestMapping("user")
public class HelloController {

    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello");
        return "success";
    }

    @RequestMapping(value = "testRequestMapping")
    public String testRequestMapping(){
        System.out.println("RequestMapping");
        return "success";
    }

}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

    <h3>入门程序</h3>
    <%--<a href="hello">入门程序</a>--%>

    <%--测试RequestMapping可以写在类上--%>
    <a href="user/testRequestMapping">RequestMapping</a>

</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值