SpringMVC 重定向和请求转发(转载)
了解servlet的页面跳转工作情况之后,下面看看spring mvc如何实现controller之间跳转,与重定向的原理相同。
请求转发只需要请求一次服务器,可以提高访问速度。
重定向可以跳转到任意服务器,可以用在系统间的跳转。
- @RequestMapping(“/forward1”)
- public String forward1(HttpServletRequest request){
- request.setAttribute(”forward”, “请求转发”);
- return “forward:forward2.html”;
- }
- @RequestMapping(“/forward2”)
- public String forward2(HttpServletRequest request){
- System.out.println(request.getAttribute(”forward”));
- return “forward:index.html”;
- }
@RequestMapping("/forward1")
public String forward1(HttpServletRequest request){
request.setAttribute("forward", "请求转发");
return "forward:forward2.html";
}
@RequestMapping("/forward2")
public String forward2(HttpServletRequest request){
System.out.println(request.getAttribute("forward"));
return "forward:index.html";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- @RequestMapping(“/redirect1”)
- public String index2(RedirectAttributes attr) throws UnsupportedEncodingException{
- attr.addAttribute(”ds”, URLEncoder.encode(“重定向”, “utf-8”));
- return “redirect:redirect2.html”;
- }
- @RequestMapping(“/redirect2”)
- public String index22(String ds) throws UnsupportedEncodingException{
- System.out.println(URLDecoder.decode(ds, ”utf-8”));
- return “index”;
- }
@RequestMapping("/redirect1")
public String index2(RedirectAttributes attr) throws UnsupportedEncodingException{
attr.addAttribute("ds", URLEncoder.encode("重定向", "utf-8"));
return "redirect:redirect2.html";
}
@RequestMapping("/redirect2")
public String index22(String ds) throws UnsupportedEncodingException{
System.out.println(URLDecoder.decode(ds, "utf-8"));
return "index";
}
SpringMVC 重定向和请求转发(转载)的更多相关文章
- web初学之重定向与请求转发
重定向与请求转发的问题 (1)RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法. (2 ...
- 请求转发:MVC设计模式、细节、请求域属性的编程实例、请求重定向和请求转发的区别
请求转发:MVC设计模式.细节.请求域属性的编程实例.请求重定向和请求转发的区别 MVC设计模式将一次请求的响应过程分成三个功能模块(一般称之为层)来协同完成,这三个模块分别是Model(模型层) ...
- 深入浅出Java 重定向和请求转发的区别
深入浅出Java 重定向和请求转发的区别 <span style="font-family:FangSong_GB2312;font-size:18px;">impor ...
- java重定向与请求转发的区别
最近工作不算太忙,今天在这里对java中的重定向和请求转发稍作总结,希望能帮助到大家. 请求转发: request.getRequestDispatcher().forward(); 重定向: res ...
- 【Servlet】深入浅出JavaServlet重定向和请求转发
import java.text.*; import java.util.*; import java.io.*; import javax.servlet.http.*; import javax. ...
- Struts2中重定向和请求转发配置
struts2中默认跳转为dispatcher请求转发 只能往jsp转发,跳转action报404 重定向 设置为redirect ,可以是jsp也可以是action <!--同一个包下的act ...
- jsp之 ---- 页面重定向和请求转发(笔记之深度说明)
1. HttpServletResponse对象的sendRedirect(String location)方法称作重定向. 如果location地址前面加上“/”,则表示 相对于Servlet容 ...
- HTTP中的重定向和请求转发的区别
原文出处:http://blog.csdn.net/meiyalei/article/details/2129120 一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: reque ...
- javaweb中重定向和请求转发(response.sendRedirect()和request.getRequestDispatcher(rul).forward(request,response)))的区别
先来两张图,方便理解: 可以看出,重定向时,是服务器向游览器重新发送了一个response命令,让游览器再次向url2发送请求,以获取url2的资源 而请求转发时,类似于是服务器自己向自己发了一个跳转 ...
随机推荐
- linux局域网内挂载其它操作系统目录
一.linux挂载windows 1.windows目录打开共享: 2.mount -t cifs -o username=admin***tor,password=abc //192.168.*** ...
- asp.net IE11 dopostback is null or undefined 为空或未定义
个人认为这个问题一般是未按规范书写导致,但懒人嘛就有懒办法. 页面重构一般可以解决此问题,但是对于不便重写的情形,这里仍然有一个解决办法. 解决方法: 1.将文件ie11.browser拷贝到C:\W ...
- PythonStudy——可变与不可变 Variable and immutable
ls = [10, 20, 30] print(id(ls), ls) ls[0] = 100 print(id(ls), ls) print(id(10)) print(id(20)) print( ...
- flex 布局压缩问题
在 flex 布局中,当有一个元素宽度过长时,另一个元素宽度会被压缩, 如下图: 解决办法:在不想被压缩的元素上加上样式 flex-shrink: 0; 效果图:
- windows下matplotlib编译安装备忘
windows下,codeblocks,mingw安装matplotlib. python下一些源码的编译安装,备忘. matplotlib官网编译好的版本只支持到3.3.我不慎刚下了python3. ...
- 如何在同一主机中添加多个homestead并行开发
参考源 https://blog.csdn.net/smm188/article/details/79356150 1,在项目目录 git clone homestead 后(见上面流程中的第四步), ...
- linux--切换ipython解释器到python3
Ipython修改为python3解释器: which ipython --得到路径 cat 路径--查看执行的解释器版本 sudo gedit 路径--修改解释器版本为python3 保存即可,保存 ...
- django的forms认证组件
django的forms认证组件 每个网站的注册界面都需要有相应的"认证"功能,比如说认证注册页面的用户名是否已被注册,二次输入的密码是否一致以及认证用户输入的用户名.邮箱.手机号 ...
- Zabbix监控进程(进程消失后钉钉报警)
用于python报警的脚本如下:(钉钉机器人的连接需要修改) #!/usr/bin/python3# -*- coding: utf-8 -*-# Author: aiker@gdedu.ml# My ...
- 个人测试SQL学习
--普通方法赋值 declare @a int print @a --Update 方法赋值 ) ' print @name --Select 语句赋值 ) ' print @name ) selec ...