SpringMVC归纳-1(model数据模型与重定向传参技术)
要点:
- model是一个Map结构的数据模型,能重定向时传递数据(拼接URL),但不安全,主要用于渲染前端页面,配合Thymeleaf填充html里面里设置好的参数。
- @RequestParam用来获取查询字符串的参数值。
- HttpServletRequest也可以获取查询字符串的参数值。
- redirect: 用于重定向到新的url。
- @ModelAttribute:运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入ModelMap中,便于View层使用。
- @ModelAttribute:运用在方法上,会在每一个@RequestMapping标注的方法前执行,如果有返回值,则自动将该返回值加入到ModelMap中。
- redirectAttribute.addAttribute实现URL字符串拼接,类似于model.addAttribute,但是它并不把数据添加到model里。
- redirectAttribute.addFlashAttribute是安全的传参方法。
下面是以上几点的实例:
package com.example.demo.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class ModelConclusion { /*
*从查询字符串里获取参数username的值
* 把name添加到model模型里
* 重定向到新的页面
*/
@RequestMapping("user")
public String setAttribute(@RequestParam(value="username",defaultValue="Leo") String name,Model model) {
model.addAttribute("name",name);
return "redirect:user/1";
} /*
* 再次绑定name到model * 查看req请求参数,发现添加到model里的属性也可以在请求参数中获得
*/
@RequestMapping("user/1")
@ResponseBody()
public String getAttribute(@ModelAttribute("name") String name,Model model,HttpServletRequest req) { String modelName="model->name:"+name;
String modelString = "model:"+model.toString();
String reqName = "req->name:"+req.getParameter("name"); return modelName+"<br>"+modelString+"<br>"+reqName;
} }
页面输出结果:
发现,model里的数据添加到了URL里,从这一特点可以知道model传递数据是不安全的。所以我们使用model主要是因为java request没有与视图技术绑定,而非作为重定向时暂存一些重要数据,如密码。
另外,在两个方法参数里,系统都自动创建了新的model,所以重定向后的model不在被保留,但是通过@ModelAttribute再次将数据绑定在model里。
重新写一下setAttribute方法,以体现modelattibute绑定功能:
@RequestMapping("user")
public String setAttribute(@ModelAttribute("name") String name,Model model) {
return "redirect:user/1";
}
@ModelAttribute用于方法前面时,先于所在Controller下的RequestMapping标注的所有方法执行,实例如下:
User:
package com.example.demo.service; public class User { private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} }
Controller:
package com.example.demo.controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.example.demo.service.User; @Controller
public class ModelConclusion3 { /*
* @ModelAttribute注解在方法前,在所有mapping前执行
* 可以实现统一配置
*/
//绑定参数到对象属性
@ModelAttribute
public User create(User newUser) {
return newUser;
} //获取名字
@RequestMapping("user/getname")
@ResponseBody()
public String getName(User newUser) {
return newUser.getName();
} //获取年龄
@RequestMapping("user/getage")
@ResponseBody()
public Integer getAge(User newUser) {
return newUser.getAge();
} }
在来看另一种重定向传参技术:
package com.example.demo.controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller
public class ModelConclusion2 { /*
* redirectAttribute.addAttribute功能时字符串拼接,类似于model.addAttribute,但是它并不把数据添加到model里
* redirectAttribute.addFlashAttribute时安全的传递参数方法,原理是将数据添加到session里,等页面渲染好后从session里移除,最后加入到model模型里
*/ @RequestMapping("user2")
public String setAttribute(Model model,RedirectAttributes redirectAttribute) { redirectAttribute.addAttribute("name","Jack");
redirectAttribute.addFlashAttribute("age",15);
System.out.println(model); return "redirect:user2/1";
} @RequestMapping("user2/1")
@ResponseBody()
public String getAttribute(String name,Integer age,Model model) { System.out.println(age);
System.out.println(name);
System.out.println(model); return "hello";
} }
控制台结果:
可以发现,addflash后model里是没数据的,而且由于它不是URL拼接,所以age也没有捕获到,但最后我们还是在model里找到它,所以addflash非常适合密码等信息的传递。
上述如有错误,望请指正!
SpringMVC归纳-1(model数据模型与重定向传参技术)的更多相关文章
- spring mvc controller间跳转 重定向 传参(转)
spring mvc controller间跳转 重定向 传参 url:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ ...
- jsp内部传参与重定向传参
1 重定向地址栏会发生改变,因为它会发送两次请求,内部转发,地址栏不会发生改变,因为它只有一个请求2 重定向不能获取上一次请求中的参数,而内部转换可以3 内部转发可以访问WEB-INF下的资源,重定向 ...
- SpringMVC中使用RedirectAttributes重定向传参,防止暴露参数
RedirectAttributes是SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的. 当我从jsp页面函数中带参数到controller层方法,方法执行完毕后返回 ...
- spring mvc controller间跳转 重定向 传参 (转)
转自:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架contr ...
- Spring Mvc Controller间跳转 重定向 传参 (转)
原文链接:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景 需求:spring MVC框架con ...
- SpringMVC之接收请求参数和页面传参
1.Spring接收请求参数 1>.使用HttpServletRequest获取 @RequestMapping("/login.do") public String log ...
- ModelAttribute注解使用与spring重定向传参
@ModelAttribute可以用于修饰controller里的方法和参数,将被修饰的对象的值绑定到指定名称的属性里.当修饰方法时,方法返回的值会在该controller里每个访问处理前绑定一次.修 ...
- spring mvc controller间跳转 重定向 传参
http://blog.csdn.net/jackpk/article/details/19121777/
- spring mvc 重定向传参
参考链接如下: http://bbs.csdn.net/topics/391034118?page=1 自己的示例程序: 详细页面提交一个修改动作,修改完成后跳转到检索页面,把检索条件重新赋值给检索页 ...
随机推荐
- 浅谈MySQL架构体系
一 数据库和数据库实例 在MySQL的学习研究中,存在两个非常容易混淆的概念,即数据库和数据库实例.在MySQL中,数据库和数据库实例定义如下: 数据库:存储数据的集合: 数据库实例:操作数据库的集 ...
- cesium 之图层管理器篇(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- cesium 之地图贴地量算工具效果篇(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...
- mysql7笔记----遍历节点所有子节点
mysql遍历节点的所有子节点 DELIMITER // CREATE FUNCTION `getChildrenList`(rootId INT) ) BEGIN ); ); SET sTemp = ...
- Linux 桌面玩家指南:14. 数值计算和符号计算
特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...
- vue2.0 日历日程表 ,可进行二次开发.
由于工作业务需求,要写一个日程表,日程表写之前 要先生成日历,废话不多说,直接 上代码: <!DOCTYPE html> <html lang="zh-CN"&g ...
- 知名区块链人脸识别公司iFace Chain [爱妃链] 支招,如何防止钱包数字币被盗...
最近众多钱包发行方跑路频发,让非常多的用户蒙受巨大经济损失,知名区块链人脸识别公司iFace Chain [爱妃链] 前日做客某区块链媒体为网友支招,如何防止钱包数字币被盗. 那么,用户怎么降低Tok ...
- redis 初识
架构 sharding redis 集群是主从式架构,数据分片是根据hash slot(哈希槽来分布) 总共有16384个哈希槽,所以理论上来说,集群的最大节点(master) 数量是16384个.一 ...
- 使用 Moq 测试.NET Core 应用 -- Mock 方法
第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 本文介绍使用Moq来Mock方法. 使用的代码: https://git ...
- [Abp vNext 源码分析] - 1. 框架启动流程分析
一.简要说明 本篇文章主要剖析与讲解 Abp vNext 在 Web API 项目下的启动流程,让大家了解整个 Abp vNext 框架是如何运作的.总的来说 ,Abp vNext 比起 ABP 框架 ...