1.在方法上传入Map/Model/ModelMap
在方法上传入Map/Model/ModelMap后,这些参数里面保存的数据都会放在request域中,可以在页面获取。
<a href="hello01">hello01</a><br>
<a href="hello02">hello02</a><br>
<a href="hello03">hello03</a><br>
@Controller
public class HelloController {
@RequestMapping("/hello01")
public String test1(Map<String,Object> map){
map.put("username","张三");
return "success";
}
@RequestMapping("/hello02")
public String test2(Model model){
model.addAttribute("username","李四");
return "success";
}
@RequestMapping("/hello03")
public String test3(ModelMap modelMap){
modelMap.addAttribute("username","赵丽");
return "success";
}
}
请求转发页面:
${username}
2.方法返回ModelAndView对象
@Controller
public class HelloController {
@RequestMapping("/hello04")
public ModelAndView test4(){
ModelAndView modelAndView= new ModelAndView("success");
modelAndView.addObject("username","郑爽");
return modelAndView;
}
}
${username}
既包含视图信息(页面地址),也包含模型数据(给页面带的数据),数据保存在request域中