最近的工作中需要使用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