ps:当model中存入了数据,如果使用重定向跳转方式,那么SpringMVC会自动将model中的数据存放在地址栏中传递
-
向session作用域中存入数据,需要在类上加一个注解:
@SessionAttributes({"属性名..."})
通过这个注解,可以指定将model中的那些命名属性存入session作用域中一份
@Controller
@RequestMapping("/model")
public class ModelController {
@RequestMapping("/modelTest1")
public String modelTest1(Model model, HttpServletRequest request){
//向request作用域存入数据
request.setAttribute("rname","蓝银皇!");
//使用model向request作用域中存数据
model.addAttribute("mname","昊天锤!");
return "index";
}
@RequestMapping("/modelMap")
public String modelMap(Model model){
//向request作用域存数据
Map<String, String> map = new HashMap<>();
map.put("dadada","大大大怪兽");
map.put("shudaixiong","树袋熊");
model.addAllAttributes(map);
return "success";
}
/**
* 使用modelAndView进行数据和视图的处理
* @return
*/
@RequestMapping("/mv")
public ModelAndView modelAndView(){
ModelAndView mv = new ModelAndView();
//可以向作用域存入数据
mv.addObject("name","大大大怪兽");
//可以设置跳转的视图:参数书写的方式与方法的返回值为String书写的方式一样
mv.setViewName("forward:/success.jsp");
return mv;
}