版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Mythology_px/article/details/82912489
Spring MVC的请求流程:
第一步:发起请求到前端控制器(DispatcherServlet)
第二步:前端控制器请求HandlerMapping查找Handler可以根据xml配置、注解进行查找
第三步:处理器映射器HandlerMapping向前端控制器返回Handler
第四步:前端控制器调用处理器适配器去执行Handler
第五步:处理器适配器去执行Handler
第六步:Handler执行完成给适配器返回ModelAndView
第七步:处理器适配器向前端控制器返回ModelAndView。ModelAndView是springmvc框架的一个底层对象,包括 Model和view
第八步:前端控制器请求视图解析器去进行视图解析,根据逻辑视图名解析成真正的视图(jsp)
第九步:视图解析器向前端控制器返回View
第十步:前端控制器进行视图渲染。视图渲染将模型数据(在ModelAndView对象中)填充到request域
第十一步:前端控制器向用户响应结果
项目实例
1. 创建项目,引入SpringMVC的jar包
2. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载前端控制器 -->
<servlet>
<!-- 指定路径下的请求由DispatcherServlet统一管理分发 -->
<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>
</servlet>
<!-- 指定访问路径及映射关系 -->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- 访问路径后缀为.do时,由分发器类统一分发请求 -->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3. 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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<!-- ↑ 添加context约束 -->
<!-- 指定分发器在该包下检索控制类 -->
<context:component-scan base-package="com.controller"></context:component-scan>
</beans>
4. 声明控制类
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
// @Controller声明类为控制类
@Controller
// @RequestMapping(访问路径):指定该类处理
@RequestMapping("/hello")
public class HelloWorld {
// 指定访问该路径时,由该方法响应操作
@RequestMapping("/now.do")
public String now(){
System.out.println("墨渐生微:SpringMVC加载成功");
return null;
}
}
5. 访问路径: http://localhost:8080/SpringMVC/hello/now.do
SpringMVC页面跳转
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 请求转发和重定向:通过方法返回值跳转页面
*/
@Controller
@RequestMapping("/hello")
public class HelloWorld {
// 指定访问该路径时,由该方法响应操作
@RequestMapping("/now.do")
public String now(){
System.out.println("默认请求转发");
// 默认:请求转发
return "/index.jsp";
}
@RequestMapping("/future.do")
public String future(){
System.out.println("请求转发");
// 请求转发
return "forward:/index.jsp";
}
@RequestMapping("/past.do")
public String past(){
System.out.println("重定向");
// 重定向
return "redirect:/index.jsp";
}
}
Servlet页面跳转
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/*
* 请求转发和重定向:通过Servelt页面跳转
*/
@Controller
@RequestMapping("/hello")
public class HelloWorld {
@RequestMapping("/future.do")
public void future(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
System.out.println("请求转发");
// 请求转发
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
@RequestMapping("/past.do")
public void past(HttpServletRequest request,HttpServletResponse response) throws IOException{
System.out.println("重定向");
// 重定向
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
}