SpringMvc
Model、ModelMap、@ModelAttribute
@ModelAttribute :被该注解修饰的方法,会在每一次请求时优先执行,用于接收前台jsp页面传入的参数
优先级:ModelAndView > Model/ModelMap(按书写顺序) > request.setAttribute
@RequestMapping("/test5")
public String test5(Model mm ,ModelMap mmp,HttpServletRequest request){
request.setAttribute("key1", "123456");
mm.addAttribute("key1", "12345");
mmp.addAttribute("key1", "1234567");
return "mytest";
}//前端为 1234567 1234567
@ModelAttribute
spring学习之@ModelAttribute运用详解
例如从表单获取:
// task-list.jsp、task-page.jsp
<input type="hidden" name="account" value='${param["account"]}'/>
<input type="text" name="pageNo" id="pageNo">
// TaskController
public String load(@PathVariable String type, HttpServletRequest request, ModelMap modelMap,
@ModelAttribute TaskSetting taskSetting) {
taskSetting.getAccount();
taskSetting.getPageNo();
}
@RequestAttribute、@SessionAttribute
从原理层面掌握@RequestAttribute、@SessionAttribute的使用
@RequestAttribute作为参数获取,或HttpServletRequest::getAttribute
@GetMapping("/success")
public void success(@RequestAttribute String msg, @RequestAttribute Integer code, HttpServletRequest request) {
request.getAttribute("msg");
}