1.使用ServletAPI实现转发
| |
| |
| |
| @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.使用视图解析器实现请求转发
| |
| |
| @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里面的数据
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
2022-01-14 13.排序链表