SpringMVC中的@ModelAttribute和@SessionAttribute注解以及RedirectAttributes总结

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:8081/model2?person1=nihao&person2=world,并且在页面中利用jstl表达式可以在model.jsp中获取的${person1}、${person2}、${person3}的值分别为nihao "" lang(因在执行model2方法前会首先执行before方法,故person1和person3放在model里面,因进行了页面跳转,所以person2不能利用jstl在page页面中取出,可以简单把model的传值范围看做是request域)

上述方法执行localhost:8080/model时,如返回值为"forward:/model2"时会请求转发到http://localhost:8081/model,并且在页面中利用jstl表达式可以在model.jsp中获取的${person1}、${person2}、${person3}的值分别为nihao world lang
  • 使用@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 "forward:/model2";
    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:8081/model2?person1=nihao&person4=yue&person2=world,并且在页面中利用jstl表达式可以在model.jsp中获取的${person1}、${person2}、${person3}、${person4}的值分别为nihao "" lang yue.

上述方法执行localhost:8080/model时,如返回值为"forward:/model2"时会请求转发到http://localhost:8081/model,并且在页面中利用jstl表达式可以在model.jsp中获取的${person1}、${person2}、${person3}、${person4}的值分别为nihao world lang yue

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、SpringMVC@SessionAttributes注解仅能放置在类上(@SessionAttributes详解@SessionAttributes源码分析@SessionAttributes使用

2.1@SessionAttributes作用

  • 若在类上使用@SessionAttributes("attributeName")注解,并且在类中存在attributeName,则会将attributeName放入session作用域.
  • 若在类上使用@SessionAttributes("attributeName")注解,SpringMVC会在方法执行前自动从session中读取keyattributeName的值,并且将其注入到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 利用RedirectAttributesaddAttribute(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);    //Tom----->25
    return "login";
}

说明:输入http://localhost:8080/redirectAttributes时,页面重定向到http://localhost:8080/redirectAttributes2?name=Tom&age=25,并且请求参数在redirectAttributes2()方法中可以直接获取.这种方法直接将参数暴露在链接地址上,请谨慎使用!!!
注意:此种情况下达到的效果和将name和age放在Model中效果一样(在链接地址上直接暴露参数),不能体现RedirectAttributes的高级特性!
  • 3.2 利用RedirectAttributesaddFlashAttribute(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://localhost:8080/redirectAttributes3后重定向到http://localhost:8080/redirectAttributes4,然后重定向到http://localhost:8080/redirectAttributes5,若没有在redirectAttributes4()中使用map.clear(),则地址栏出现的链接为http://localhost:8080/redirectAttributes5?email=fenglang2016@qq.com&gender=Male.
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值