SpringMVC中重定向model值的获取方式
作者:Minecr001 时间:2022-08-25 11:22:09
SpringMVC重定向model值的获取
1、步骤一:在控制器中编写
/*重定向测试*/
@RequestMapping("/m1/t2")
public String test1(Model model, String message) {
message = "222";
model.addAttribute("msg", message);
return "redirect:/r1";
}
2、编写对应跳转
@RequestMapping("/r1")
public String test2(Model model, String msg) {
model.addAttribute("msg", msg);
return "success";
}
这里的return “success”;是跳转到自己编写的视图解析器规则下的success.jsp页面
3、测试:在浏览器访问自己项目下的对应控制器
http://localhost:8080/mvc_04/m1/t2
mvc_04是我自己的项目模块名,m1/t2是上面创建的拦截
4、然后浏览器跳转
http://localhost:8080/mvc_04/r1?msg=222
成功获取到重定向传入的参数,
问题思考:
如果跳转到主页,那model的参数怎么获取呢,如上我跳转的是ViewResolver拼接而成的success.jsp页面,
这时候需要使用RedirectAttributes参数
/*重定向测试2*/
@RequestMapping("/r2")
public String test3(Model model, RedirectAttributes rettr) {
rettr.addAttribute("msg", "redirect");
return "redirect:/index.jsp";
}
注意:index.jsp中参数的获取不能直接 ${msg}
而是得使用 ${param.msg}
SpringMVC中model的理解
spring的Model相当于前端的一个数据库,就好比后端中的user实体类所对应的数据库User,从Model中获取数据比从后端的User实体类中获取数据更加方便。
如下图是一个简单的实例,简单展示一下Model是怎么存储数据然后展示到前段页面的。
1、首先将数据添加到Model中
2、然后再将其在页面上展示出来
来源:https://blog.csdn.net/Minecr001/article/details/109260985
标签:SpringMVC,重定向,model
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用C#修改本地Windows系统时间
2022-05-06 08:23:00
![](https://img.aspxhome.com/file/2023/6/77276_0s.png)
java 获取当前路径下的所有xml文档的方法
2021-08-08 13:54:29
jtds1.1连接sqlserver2000测试示例
2021-06-13 04:57:28
unity里获取text中文字宽度并截断省略的操作
2023-11-02 07:48:52
![](https://img.aspxhome.com/file/2023/0/88410_0s.jpg)
C#访问SqlServer设置链接超时的方法
2022-11-19 17:43:21
C# 中对象序列化XML的方法
2021-11-12 13:51:55
![](https://img.aspxhome.com/file/2023/5/82215_0s.png)
通过WIFI(不用数据线)连接Android手机调试
2023-01-05 18:20:45
![](https://img.aspxhome.com/file/2023/1/97881_0s.png)
SpringBoot如何使用ApplicationContext获取bean对象
2023-06-28 20:36:55
![](https://img.aspxhome.com/file/2023/1/67101_0s.jpg)
C#微信开发之启用开发者模式
2022-07-07 11:24:54
![](https://img.aspxhome.com/file/2023/9/91369_0s.png)
Spring Boot集成Ehcache缓存解决方式
2023-05-13 08:27:27
C#开发Android百度地图手机应用程序(多地图展示)
2022-01-06 00:47:51
![](https://img.aspxhome.com/file/2023/6/77306_0s.png)
Mybatis的动态Sql组合模式详情
2021-12-26 03:26:35
使用SpringBoot获取所有接口的路由
2021-08-29 22:28:34
Java 数据结构与算法系列精讲之哈希算法实现
2022-01-10 23:56:07
![](https://img.aspxhome.com/file/2023/3/71683_0s.gif)
springboot配置文件绑定实现解析
2022-06-07 23:32:38
![](https://img.aspxhome.com/file/2023/7/61917_0s.png)
Java判断用户名和密码是否符合要求过程详解
2023-09-24 08:08:36
springboot 自定义权限标签(tld),在freemarker引用操作
2023-11-23 06:20:15
![](https://img.aspxhome.com/file/2023/9/59979_0s.jpg)
你真的了解Java的多线程方法吗
2022-02-01 22:55:40
![](https://img.aspxhome.com/file/2023/4/69344_0s.png)
JAVA面试题之Forward与Redirect的区别详解
2023-11-25 02:27:15
![](https://img.aspxhome.com/file/2023/2/59912_0s.png)
SpringBoot解决Required String parameter xxx is not present问题
2021-08-08 12:53:15