要点:
model是一个map结构的数据模型,能重定向时传递数据(拼接url),但不安全,主要用于渲染前端页面,配合thymeleaf填充html里面里设置好的参数。
@requestparam用来获取查询字符串的参数值。
httpservletrequest也可以获取查询字符串的参数值。
redirect: 用于重定向到新的url。
@modelattribute:运用在参数上,会将客户端传递过来的参数按名称注入到指定对象中,并且会将这个对象自动加入modelmap中,便于view层使用。
@modelattribute:运用在方法上,会在每一个@requestmapping标注的方法前执行,如果有返回值,则自动将该返回值加入到modelmap中。
redirectattribute.addattribute实现url字符串拼接,类似于model.addattribute,但是它并不把数据添加到model里。
redirectattribute.addflashattribute是安全的传参方法。
下面是以上几点的实例:
1 package com.example.demo.controller;
2
3
4
5 import javax.servlet.http.httpservletrequest;
6
7 import org.springframework.stereotype.controller;
8 import org.springframework.ui.model;
9 import org.springframework.web.bind.annotation.modelattribute;
10 import org.springframework.web.bind.annotation.requestmapping;
11 import org.springframework.web.bind.annotation.requestparam;
12 import org.springframework.web.bind.annotation.responsebody;
13
14
15 @controller
16 public class modelconclusion {
17
18 /*
19 *从查询字符串里获取参数username的值
20 * 把name添加到model模型里
21 * 重定向到新的页面
22 */
23 @requestmapping("user")
24 public string setattribute(@requestparam(value="username",defaultvalue="leo") string name,model model) {
25 model.addattribute("name",name);
26 return "redirect:user/1";
27 }
28
29 /*
30 * 再次绑定name到model32 * 查看req请求参数,发现添加到model里的属性也可以在请求参数中获得
33 */
34 @requestmapping("user/1")
35 @responsebody()
36 public string getattribute(@modelattribute("name") string name,model model,httpservletrequest req) {
37
38 string modelname="model->name:"+name;
39 string modelstring = "model:"+model.tostring();
40 string reqname = "req->name:"+req.getparameter("name");
41
42 return modelname+"
"+modelstring+"
"+reqname;
43 }
44
45 }
页面输出结果:
发现,model里的数据添加到了url里,从这一特点可以知道model传递数据是不安全的。所以我们使用model主要是因为java request没有与视图技术绑定,而非作为重定向时暂存一些重要数据,如密码。
另外,在两个方法参数里,系统都自动创建了新的model,所以重定向后的model不在被保留,但是通过@modelattribute再次将数据绑定在model里。
重新写一下setattribute方法,以体现modelattibute绑定功能:
1 @requestmapping("user")
2 public string setattribute(@modelattribute("name") string name,model model) {
3 return "redirect:user/1";
4 }
@modelattribute用于方法前面时,先于所在controller下的requestmapping标注的所有方法执行,实例如下:
user:
1 package com.example.demo.service;
2
3 public class user {
4
5 private string name;
6 private integer age;
7 public string getname() {
8 return name;
9 }
10 public void setname(string name) {
11 this.name = name;
12 }
13 public integer getage() {
14 return age;
15 }
16 public void setage(integer age) {
17 this.age = age;
18 }
19
20 }
controller:
1 package com.example.demo.controller;
2
3 import org.springframework.stereotype.controller;
4 import org.springframework.web.bind.annotation.modelattribute;
5 import org.springframework.web.bind.annotation.requestmapping;
6 import org.springframework.web.bind.annotation.responsebody;
7
8 import com.example.demo.service.user;
9
10 @controller
11 public class modelconclusion3 {
12
13 /*
14 * @modelattribute注解在方法前,在所有mapping前执行
15 * 可以实现统一配置
16 */
17 //绑定参数到对象属性
18 @modelattribute
19 public user create(user newuser) {
20 return newuser;
21 }
22
23 //获取名字
24 @requestmapping("user/getname")
25 @responsebody()
26 public string getname(user newuser) {
27 return newuser.getname();
28 }
29
30 //获取年龄
31 @requestmapping("user/getage")
32 @responsebody()
33 public integer getage(user newuser) {
34 return newuser.getage();
35 }
36
37
38 }
在来看另一种重定向传参技术:
1 package com.example.demo.controller;
2
3 import org.springframework.stereotype.controller;
4 import org.springframework.ui.model;
5 import org.springframework.web.bind.annotation.requestmapping;
6 import org.springframework.web.bind.annotation.responsebody;
7 import org.springframework.web.servlet.mvc.support.redirectattributes;
8
9 @controller
10 public class modelconclusion2 {
11
12 /*
13 * redirectattribute.addattribute功能时字符串拼接,类似于model.addattribute,但是它并不把数据添加到model里
14 * redirectattribute.addflashattribute时安全的传递参数方法,原理是将数据添加到session里,等页面渲染好后从session里移除,最后加入到model模型里
15 */
16
17 @requestmapping("user2")
18 public string setattribute(model model,redirectattributes redirectattribute) {
19
20 redirectattribute.addattribute("name","jack");
21 redirectattribute.addflashattribute("age",15);
22 system.out.println(model);
23
24 return "redirect:user2/1";
25 }
26
27 @requestmapping("user2/1")
28 @responsebody()
29 public string getattribute(string name,integer age,model model) {
30
31 system.out.println(age);
32 system.out.println(name);
33 system.out.println(model);
34
35 return "hello";
36 }
37
38
39 }
控制台结果:
可以发现,addflash后model里是没数据的,而且由于它不是url拼接,所以age也没有捕获到,但最后我们还是在model里找到它,所以addflash非常适合密码等信息的传递。
上述如有错误,望请指正!
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!