入门案例制作
- 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目录中查找对应的页面并展示