复习第7点-7.SpringMVC 的响应方式
2024-09-08 18:40:51
1.使用ServletAPI实现转发
/*
使用HttpServletRequest对象实现请求转发
*/
@RequestMapping("/httpServletRequest")
public void method1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("msg", "hello--httpServletRequestForward");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
2.使用 Forward 关键字实现请求转发跳转
@RequestMapping("/forward")
public String method2(HttpServletRequest request) {
request.setAttribute("msg", "hello--forward");
return "forward:/index.jsp";
}
3.使用视图解析器实现请求转发
// 访问路径之后,没有出现结果
// redirect:不能共享request范围的数据
@RequestMapping("/redirect1")
public String redirectLogin1(HttpServletRequest request) {
request.setAttribute("msg", "hello--redirectLogin1");
return "redirect:/index.jsp";
}
// 访问成功
@RequestMapping("/redirect2")
public String redirectLogin2(HttpSession session) {
session.setAttribute("msg", "hello--redirectLogin2");
return "redirect:/index.jsp";
}
redirect和forward的区别
- 地址栏的区别
forward:地址栏不发生变化
redirect:地址栏显示的是新的url - 数据共享的角度
forward:转发页面和转发到的页面可以共享request里面的数据
redirect:转发页面和转发到的页面可以共享session里面的数据
最新文章
- SharePoint 2013 JavaScript API 记录
- Ubuntu下matlab快捷键设置
- 你的联动够快够小吗——基于Telerik(ASP.NET平台)
- JAVA之关于super的用法
- 使用powerdesigner 画图的详细说明
- vs默认的app.config是哪个?
- php文件格式数组
- php入门微理解
- Jasmine基础语法
- Bean复制
- 手机自动化测试:Appium源码分析之跟踪代码分析七
- objc[20556]:Class JavaLaunchHelper is implemented in both xxx 警告处理
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
- (三)surging 微服务框架使用系列之我的第一个服务(审计日志)
- 看eShopOnContainers学一个EventBus
- [BZOJ1061] [Noi2008] 志愿者招募 (费用流)
- 剑指offer前6题
- 基于Centos搭建Python Web 环境搭建教程
- MATLAB 的输入输出命令
- asp.net 浏览器下载文件的四种方式