SpringMVC学习笔记04--------结果跳转
1. 请求转发与请求重定向
-
请求转发
- 一次请求
- 地址栏不会改变
- 跳转后的代码不会执行
- 只能在当前项目中转发
- 可以传递request作用域的信息
-
请求重定向
- 是两次请求
- 地址栏会改变
- 跳转后的代码会执行
- 可以跳转到当前服务器之外的路径
- 不能把request作用域信息传递下去
2.SpringMVC实现请求转发
以下几种方式的测试我们都采用注解的方式实现,注解相关配置参考SpringMVC学习笔记02-------第一个SpringMVC程序
2.1 使用servletApi(不推荐使用)
-
编写controller类
package com.xdw.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller @RequestMapping("/servlet") public class ServletApiController { @RequestMapping("/forward") public void testForward(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute("msg", "I am forward!"); request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response); } @RequestMapping("/redirect") public void testRedirect(HttpServletRequest request, HttpServletResponse response) throws Exception { // 因为要跳转的jsp页面在WEB-INF目录下,请求重定向无法直接访问到, // 我们就先重定向到/servlet/forward,再请求转发至hello.jsp response.sendRedirect("/servlet/forward"); } }
-
配置Tomcat,启动并测试
该种方式采用的是原生的servlet的方式实现的,使用起来比较繁琐,不推荐使用,实际开发中,我们更多的是使用SpringMVC方式进行结果跳转。
2.2 使用SpringMVC实现结果跳转(没有视图解析器的情形下)
-
编写springMVC的配置文件
applicationContext.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使得dispatcherServlet不处理静态资源 --> <mvc:default-servlet-handler/> <!-- 配置注解驱动,即配置处理器映射器与处理器适配器 --> <mvc:annotation-driven/> <!-- 配置扫描包路径,使得指定包下的注解@Controller生效 --> <context:component-scan base-package="com.xdw.controller"/> <!-- 配置视图解析器 --> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">--> <!-- <property name="prefix" value="/"/>--> <!-- <property name="suffix" value=".jsp"/>--> <!-- </bean>--> </beans>
-
编写Controller控制器
package com.xdw.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/springMVC") public class MvcNoVrController { @RequestMapping("/forward01") public String testForward01() { // 请求转发: 方式一 return "/index.jsp"; } @RequestMapping("/forward02") public String testForward02() { // 请求转发: 方式二 return "forward:/index.jsp"; } @RequestMapping("/redirect") public String testRedirect() { // 请求重定向 return "redirect:/index.jsp"; } }
- 这里因为我们没有配置视图解析器,所以在请求转发的时候需要写全需要跳转的jsp页面(路径加上需要跳转的jsp页面)。
-
启动Tomcat测试
2.3 使用SpringMVC实现页面跳转(配置了视图解析器)
-
编写SpringMVC配置文件
applicationContext.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 使得dispatcherServlet不处理静态资源 --> <mvc:default-servlet-handler/> <!-- 配置注解驱动,即配置处理器映射器与处理器适配器 --> <mvc:annotation-driven/> <!-- 配置扫描包路径,使得指定包下的注解@Controller生效 --> <context:component-scan base-package="com.xdw.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
编写controller控制器
package com.xdw.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/springMVC02") public class MvcController { @RequestMapping("/forward01") public String testForward01() { // 请求转发: 方式一 return "index"; } @RequestMapping("/forward02") public String testForward02() { // 请求转发: 方式二 return "forward:/index.jsp"; } @RequestMapping("/redirect") public String testRedirect() { // 请求重定向 return "redirect:/index.jsp"; } }
- 因为配置了视图解析器,并设置了视图解析器的前缀与后缀,所以请求转发可以直接写成
return "index";
,视图解析器拿到这个之后会自动根据我们的配置找到对应的jsp页面! - 请求重定向本就是发起另一次请求,所以无论有没有配置视图解析器,都需要写全访问路径(访问资源在项目中的路径 + 资源全称)!
- 因为配置了视图解析器,并设置了视图解析器的前缀与后缀,所以请求转发可以直接写成
3.测试
分类:
SpringMVC框架
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?