springmvc重定向传递参数

最近的工作中需要使用springmvc进行重定向并且传递参数,其实以前也有相对的困惑,故在此进行记录。

一、使用model进行传递参数

  1、举个栗子:重定向的参数为基本类型(包括string)

   /**
   * 使用Model来存放参数,在重定向时该参数也会被带过去
   * @param model
   * @return
   */
  @RequestMapping("redirectParamsByModel.shtml")
  public String redirectParamsByModel(Model model){
  String strParams = "测试一";
  
  model.addAttribute("strParams", strParams);
  return "redirect:/test/test1.shtml";
  }
  

  //重定的方法,同时上面的Model中存放的参数也被传递过来了
  @RequestMapping("test1.shtml")
  public String test1(Model model,String strParams){
  System.out.println("通过model传递的参数: "+strParams);
  return "aaa";
  }  

测试结果在名为test1的方法中输出 :“通过model传递的参数: 测试一“;


    2、传递参数为集合类型list

  /**
   * 使用Model来存放参数,在重定向时该参数也会被带过去
   * @param model
   * @return
   */
  @RequestMapping("redirectParamsByModel.shtml")
  public String redirectParamsByModel(Model model){
  
  List<String> strParams = new ArrayList<String>();  
  strParams.add("测试一");
  strParams.add("测试二");
  strParams.add("测试三");
  model.addAttribute("strParams", strParams);
  return "redirect:/test/test1.shtml";
  }
  
  //重定的方法,同时上面的Model中存放的参数也被传递过来了
  @RequestMapping("test1.shtml")
  public String test1(Model model,List<String> st

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值