个人笔记-SpringMVC-SpringMVC入门案例制作

入门案例制作

  • XML版
  • XML+注解版(主体)
  • 纯注解版(变形)

新建项目
pom文件
在这里插入图片描述
在这里插入图片描述
web.xml
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       version="3.0">
</web-app>

success.jsp文件
在这里插入图片描述
UserServlet文件
在这里插入图片描述
这些是准备工作,然后开始我们的SpringMVC配置
先在pom文件添加两个依赖,spring-webmvc与spring-web
在这里插入图片描述
新建spring-mvc.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: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">
</beans>

在UserController里添加@Controller注解
在这里插入图片描述
修改web.xml如图所示
在这里插入图片描述
再在UserController添加@RequestMapping注解
在这里插入图片描述
添加跳转页面结果
在这里插入图片描述
文件结构图
在这里插入图片描述
在pom新添在这里插入图片描述
配置启动项
在这里插入图片描述
在这里插入图片描述
访问成功
在这里插入图片描述


入门案例工作流程分析

  • 服务器启动
    • 加载web.xml中的DispatcherServlet
    • 读取spring-mvc.xml中的配置,加载所有com.thenema.my_springmvc包中的所有标记为bean的类
    • 读取bean中方法上方标注@RequestMapping的内容
  • 处理请求
    • DispatcherServlet配置拦截所有请求
    • 使用请求路径与所有加载的@RequestMapping的内容进行比对
    • 执行对应的方法
    • 根据方法的返回值在webapp目录中查找对应的页面并展示
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值