You don't need @ModelAttribute
(parameter) just to use a Bean as a parameter
For example, these handler methods work fine with these requests:
@RequestMapping("/a")
void pathA(SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
GET /a?name=neil
@RequestMapping(value="/a", method=RequestMethod.POST)
void pathAPost(SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
POST /a
name=neil
Use @ModelAttribute
(method) to load default data into your model on every request - for example from a database, especially when using @SessionAttributes
. This can be done in a Controller
or in a ControllerAdvice
:
@Controller
@RequestMapping("/foos")
public class FooController {
@ModelAttribute("foo")
String getFoo() {
return "bar"; // set modelMap["foo"] = "bar" on every request
}
}
Any JSP forwarded to by FooController
:
${foo} //=bar
or
@ControllerAdvice
public class MyGlobalData {
@ModelAttribute("foo")
String getFoo() {
return "bar"; // set modelMap["foo"] = "bar" on every request
}
}
Any JSP:
${foo} //=bar
Use @ModelAttribute
(parameter) if you want to use the result of @ModelAttribute
(method) as a default:
@ModelAttribute("attrib1")
SomeBean getSomeBean() {
return new SomeBean("neil"); // set modelMap["attrib1"] = SomeBean("neil") on every request
}
@RequestMapping("/a")
void pathA(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
GET /a
Use @ModelAttribute
(parameter) to get an object stored in a flash attribute:
@RequestMapping("/a")
String pathA(RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("attrib1", new SomeBean("from flash"));
return "redirect:/b";
}
@RequestMapping("/b")
void pathB(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("from flash", someBean.getName());
}
GET /a
Use @ModelAttribute
(parameter) to get an object stored by @SessionAttributes
@Controller
@SessionAttributes("attrib1")
public class Controller1 {
@RequestMapping("/a")
void pathA(Model model) {
model.addAttribute("attrib1", new SomeBean("neil")); //this ends up in session due to @SessionAttributes on controller
}
@RequestMapping("/b")
void pathB(@ModelAttribute("attrib1") SomeBean someBean) {
assertEquals("neil", someBean.getName());
}
}
GET /a
GET /b
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…