SpringMvc redirect
SpringMVC redirect
核心
首先网上百度到的资源基本已经够用, 留作记录.
虽然这哥们也是转的, 但又没有留源地址. 因此 ...
个人补充
同时如果懒得去原链接看, 我在这里也写出来, 同时文档略做更改.
同样仅仅考虑 SpringMVC 中的跳转问题:
这种是最为常见的情况:
@RequestMapping("/t1.html")
public String who2(String password, Model model) { return "test.jsp";
}
在这种拼接中, 会拼接上:
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
对应的 suffix, 至于ModelAndView那种方式也不再多说.
采用redirect的方式
@RequestMapping("/t1.html")
public String who2(String password, Model model) { return "redirect:/index.jsp";
}
至于返回的路径加/, 这里就是指项目根目录, 而更有趣的作用应该是重定向至另一个 Controller, 因为在这里重定向的时候, 默认以当前 class 的 RequestMapping 为前缀,进行拼接. 但不会拼接对应的后缀.
@RequestMapping("/test")
@Controller
public class TestController { @RequestMapping("/t1.html")
public String who2(String password, Model model) { return "redirect: t2";
}
}
404响应, HTTP Status 404 - /test/t2, 所以更适合用在 Controller上, 但存在同样的问题, 会将参数拼接在 Url上.
所以如果不在意参数显示在前台的情况下, 这种方式已经能满足要求了. 如果需要访问的并非本类的 路径, 还可以自行拼接路径.
RedirectAttributes
在这种方式中, 返回值必须为 redirect 才会有起到存储数据的作用.
@RequestMapping("/t1.html")
public String who2(String password, RedirectAttributes model) {
model.addAttribute("password", password);
return "redirect: /index.jsp";
}
但和上述方式, 直接采用 Model 所带来的问题是相同的
有以下测试:
@RequestMapping("/t1.html")
public String who2(String password, RedirectAttributes model) {
model.addFlashAttribute("password", password);
return "redirect: t3";
} @RequestMapping("t3")
public String who4(ModelMap map, HttpServletRequest request, String password, String username) {
System.out.println(map.get("password"));
System.out.println(request.getParameter("password"));
System.out.println(request.getAttribute("password"));
System.out.println(request.getSession().getAttribute("password"));
System.out.println("password:" + password);
Map map1 = RequestContextUtils.getInputFlashMap(request);
System.out.println(map1.get("password"));
return "myname";
} <script type="text/javascript">
alert('${password}');
</script>
输出结果:
123
null
null
null
password:null
123
也就不难发现, 这种addFlashAttribute的方式其实仅仅只支持重定向至另一个 method, 而非页面, 因为在页面无法取出对应的参数.
所以网上的几种方法, EL表达式, Request, Session, 这些方式都是取不出来对应的值得, 特别地, 在看源码之前, 对原理我也不敢多说什么,只知道是这样用的.
另外一种获取数据的方法是:
@ModelAttribute("password") String pwd //其他几种
Map map1 = RequestContextUtils.getInputFlashMap(request); ModelMap map
以及在本次被重定向之后, 转发的界面,用EL表达式可以取到.
以及一种比较特殊的写法(addFlashAttribute, addAttribute 的返回值均为RedirectAttributes):
model.addAttribute("username", "zzzzz").addFlashAttribute("password", password);
可以通过多种方式设定想要的参数, 但需要注意的是, 一旦使用了 addAttribute, 参数就会被拼接在url上.
RedirectView
也是一种用法, 在 RedirectAttributes 用到的场合中, 除了采用 redirect: 的方式, 也可以采用 RedirectView的方式.
这点类似于,常规使用中的, ModelAndView 和 return new String;
SpringMvc redirect的更多相关文章
- SpringMVC redirect乱码问题
转:http://blog.csdn.net/xubo_zhang/article/details/8239725 spring redirect 用spring redirect中文会乱码:如下示例 ...
- SpringMVC redirect中文乱码问题
在使用"redirect:xxx.do?param=中文"时会出现乱码问题,解决方案如下: 使用model.addAttribute来替代直接拼接参数.如下: @RequestMa ...
- SpringMVC——redirect重定向跳转传值
原文:https://my.oschina.net/u/2273085/blog/398374 spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url ...
- SSM-SpringMVC-22:SpringMVC中转发(forward)和重定向(redirect)
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 转发和重定向大家都熟悉,都学到框架了,怎么能不了解转发和重定向呢? 如果有不熟悉的,可以去百度搜几篇博客去看看 ...
- jsp取addFlashAttribute值深入理解即springMVC发redirect传隐藏参数
结论:两种方式 a.如果没有进行action转发,在页面中el需要${sessionScope['org.springframework.web.servlet.support.SessionFlas ...
- SpringMVC中使用forward和redirect进行转发和重定向以及重定向时如何传参详解
转自:http://blog.51cto.com/983836259/1877188 2016-11-28 09:45:59 如题所示,在SpringMVC中可以使用forward和redirec ...
- springmvc中controller内方法跳转forward?redirect?
使用springmvc的controller的时候,碰到controller内方法的跳转的问题,记录下问题以及自己测试的过程. 场景: 业务执行更新操作之后返回列表页面,列表页面需默认展示查询的列表数 ...
- SpringMVC重定向(redirect)传参数,前端EL表达式接受值
由于重定向相当于2次请求,所以无法把参数加在model中传过去.在上面例子中,页面获取不到msg参数.要想获取参数,可以手动拼url,把参数带在后面.Spring 3.1 提供了一个很好用的类:Red ...
- SpringMVC视图解析中的 forward: 与 redirect: 前缀
在 SpringMVC 中,可以指定画面的跳转方式.使用 forward: 前缀实现请求转发跳转,使用 redirect: 前缀实现重定向跳转.有前缀的转发和重定向操作和配置的视图解析器没有关系,视图 ...
随机推荐
- a=a++问题引发的思考
使用javap命令解析出来的a=a++过程的字节码(int a=1; a=a++): 0: iconst_1 将数值1压入栈顶 1: istore_1 栈顶值1存入局部变量表的第一个slot中 2 ...
- 递归中traverse小人 & dc女王的区别
TRAVERSE 是一个小人, 拿着一个记事本, 顺着二叉树走, 走过一个, 在本子上面记下来 DIVIDE & CONQUER 是女王接到这个任务, 找两个小弟A和B, 让A和B先去收集, ...
- python参数Sample Code
import time import datetime import getopt import sys try: opts, args = getopt.getopt(sys.argv[1:], & ...
- asp.net 在自己指定的文件夹下面弄个App.config来读取配置
.注意首先你要在你的应用程序的根目录下面新建一个Config的文件夹,然后在你新建的Config文件夹下面建立一个MyApp.config文件,根据标准的App.config复制一个过来稍作修改就好, ...
- 进入docker的4种方式
在使用Docker创建了容器之后,大家比较关心的就是如何进入该容器了,其实进入Docker容器有好几多种方式,这里我们就讲一下常用的几种进入Docker容器的方法. 进入Docker容器比较常见的几种 ...
- metasploit-数据库支持
db_status db_disconnect db_connect 用户名:口令@服务器地址:端口/数据库名称 createdb msf4 -E UTF8 -T template0 -o msf3 ...
- win7更改路由器wifi 密码
1.有线.无线都能进入192.168.1.1路由设置界面 (也可能是192.168.0.1看路由底面IP) ps: 无线(笔记本与路由没使用网线相连)情况下必须开启wifi连接上该路由才能进入. 无法 ...
- 用Swift实现一款天气预报APP(三)
这个系列的目录: 用Swift实现一款天气预报APP(一) 用Swift实现一款天气预报APP(二) 用Swift实现一款天气预报APP(三) 通过前面的学习,一个天气预报的APP已经基本可用了.至少 ...
- python使用smtplib和email发送腾讯企业邮箱邮件
公司每天要发送日报,最近没事搞了一下如何自动发邮件,用的是腾讯企业邮箱,跟大家分享一下我的研究过程吧. 以前弄的发邮件的是用qq邮箱发的,当时在网上查资料最后达到了能发图片,网页,自定义收件人展示,主 ...
- Modelsim设置数据以模拟波形显示
选中希望以模拟波形显示的信号,右击选择format—>Analog(automatic) 如果你的数据是用无符号数表示一个完整的波形的,那么可能显示出来的波形样子是下面的样子,不过不要紧,这是因 ...