1、SpringMVC
中使用@ModelAttribute
时,注解可放置在方法、方法的参数或者@ModelAttribute
与@RequestMapping
同时放置在方法上(@ModelAttribute详解)
1.1使用@ModelAttribute
注解在方法上
- 使用
@ModelAttribute
注解在方法上且无返回值
@ModelAttribute
public void before(Model model){
System.out.println("--->before");
model.addAttribute("person1","nihao");
}
@GetMapping("/model")
public String model1(Model model){
model.addAttribute("person2","world");
System.out.println("------>model");
return "redirect:/model2";
}
@GetMapping("/model2")
public String model2(Model model){
System.out.println("---->model2");
model.addAttribute("person3","lang");
return "model";
}
--->before
------>model
--->before
---->model2
说明:@ModelAttribute注解的方法会在Controller每个方法执行之前都执行,故对于一个Controller中包含多个URL的时候,需谨慎!!
上述方法执行localhost:8080/model时,如返回值为"redirect:/model2"时会重定向到http:
上述方法执行localhost:8080/model时,如返回值为"forward:/model2"时会请求转发到http:
- 使用
@ModelAttribute
注解在方法上且带有返回值
@ModelAttribute
public void before(Model model){
System.out.println("--->before");
model.addAttribute("person1","nihao");
}
/**
* 此种情况相当于返回值对象会被放到隐含的Model中,
* 定义value属性后相当于Model中的key为"value属性的值(本例中为person4)",value为返回值.
* 注意:返回值可为基本类型(八种基本类型)或者引用类型,并不局限于仅仅为String.
* @return :Model中的键值对中的key所对应的value值;
*/
@ModelAttribute(value="person4")
public String before2(){
System.out.println("----->before2");
return "yue";
}
@GetMapping("/model")
public String model1(Model model){
model.addAttribute("person2","world");
System.out.println("------>model");
return "redirect:/model2";
}
@GetMapping("/model2")
public String model2(Model model){
System.out.println("---->model2");
model.addAttribute("person3","lang");
return "model";
}
--->before
----->before2
------>model
--->before
----->before2
---->model2
上述方法执行localhost:8080/model时,如返回值为"redirect:/model2"时会重定向到http:
上述方法执行localhost:8080/model时,如返回值为"forward:/model2"时会请求转发到http:
1.2使用@ModelAttribute
注解在方法的参数
@ModelAttribute
public void before(Model model){
model.addAttribute("k1","zhengyanling");
System.out.println("------>before1");
}
@ModelAttribute(value="k2")
public String before2(){
System.out.println("------>before2");
return "zhenglingling";
}
@GetMapping("/modelparam")
public String model(@ModelAttribute("k1")String k1,@ModelAttribute("k2")String k2,Model model){
model.addAttribute("k3","zhurui");
System.out.println(k1+"---->"+k2);
return "model2";
}
------>before1
------>before2
zhengyanling---->zhenglingling
说明:@ModelAttribute注解在方法的参数上,相当于从前面的Model中提取对应名称的属性.
1.3 @ModelAttribute
注解和@RequestMapping
同时注解在方法上
@ModelAttribute(value="name")
@GetMapping("/model3")
public String model(){
return "lingling";
}
说明:此种情况下返回值String(或者其他对象)不再是视图,而是放入Model中的键为name所对应的值(即Model.addAttribute("name","lingling"));此时对应的页面即为@GetMapping的值"model3".
2.1@SessionAttributes
作用
- 若在类上使用
@SessionAttributes("attributeName")
注解,并且在类中存在attributeName
,则会将attributeName放入session
作用域. - 若在类上使用
@SessionAttributes("attributeName")
注解,SpringMVC会在方法执行前自动从session
中读取key
为attributeName
的值,并且将其注入到Map
中。若要获取attributeName
的值有两种方式:①利用在方法的参数中使用@ModelAttribute("attributeName")
,从Model
中读取这个值;②利用方法参数中的ModelMap
,利用modelMap.get("attributeName")
获取. - 使用了
@SessionAttributes
注解后,若要清除掉@SessionAttributes)
保存的数据,需要在方法中传入SessionStatus
参数,然后调用status.setComplete()
.
2.2@SessionAttributes
参数说明
names
:字符串数组,存储到session
中的数据的名称。type
:根据指定参数的类型,将模型中对应类型的参数存储到session
中value
: 同names
.
2.3@SessionAttributes
具体使用
@Controller
@SessionAttributes(value={"name"})
public class SessionAttributesController {
@GetMapping("/sessionattributes")
public String sessionView(Model model){
model.addAttribute("name","zhengyanling");
return "redirect:/ss";
}
@GetMapping("/ss")
public String sessionView2(@ModelAttribute("name")String name){
System.out.println("---->>>>>"+name);
return "session";
}
}
说明:当执行localhost:8080/sessionattributes时,在Model中诸如name属性,页面重定向到localhost:8080/ss?name=zhengyanling,并且在该方法中可取出Model中的值(在正常情况下因为Model的作用域相当于request,不能在重定向情况下传值,利用的是@SessionAttributes的特性)
3、RedirectAttributes
3.1RedirectAttributes
使用场景:典型场景如重定向(POST-REDIRECT-GET模式,1、POST时将下一次需要的数据放在FlashMap;2、重定向;3、通过GET访问重定向的地址,此时FlashMap会把1放到FlashMap的数据取出放到请求中,并从FlashMap中删除;从而支持在两次请求之间保存数据并防止了重复表单提交)
3.2RedirectAttributes
介绍:RedirectAttributes
专门用于重定向后能携带参数的工具类,主要有两种带参的方式
- 3.1 利用
RedirectAttributes
的addAttribute(String attributeName,Object attributeValue)
方法
@GetMapping("/redirectAttributes")
public String redirectAttributes1(RedirectAttributes redirectAttributes){
redirectAttributes.addAttribute("name","Tom");
redirectAttributes.addAttribute("age","25");
return "redirect:/redirectAttributes2";
}
@GetMapping("/redirectAttributes2")
public String redirectAttributes2(String name,String age){
System.out.println(name+"----->"+age);
return "login";
}
说明:输入http:
注意:此种情况下达到的效果和将name和age放在Model中效果一样(在链接地址上直接暴露参数),不能体现RedirectAttributes的高级特性!
- 3.2 利用
RedirectAttributes
的addFlashAttribute(String attributeName,Object attributeValue)
方法
@GetMapping("/redirectAttributes3")
public String redirectAttributes3(RedirectAttributes redirectAttributes){
redirectAttributes.addFlashAttribute("email","fenglang2016@qq.com");
redirectAttributes.addFlashAttribute("gender","Male");
return "redirect:/redirectAttributes4";
}
@GetMapping("/redirectAttributes4")
public String redirectAttributes4(ModelMap map,@ModelAttribute("email")String email,@ModelAttribute("gender")String gender){
System.out.println(email+"------>"+gender);
map.clear();
return "redirect:/redirectAttributes5";
}
@GetMapping("/redirectAttributes5")
public String redirectAttributes5(SessionStatus status){
System.out.println("redirectAttributes5----->"+status.isComplete());
return "redirectattributes";
}
说明:redirectAttributes.addFlashAttributie("prama",value)的优点是在发送链接请求时隐藏了请求参数,若跳转至页面,可以在页面中利用jstl表达式取出对应的值,若在方法中要获取param的属性值,需要借助@ModelAttribute注解.
注意:若用户请求http: