我在 stackoverflow、spring 规范、google 上找了好久还是没有找到答案。
例如,
--------------------示例方法#1------------------------
@RequestMapping(value = "/login")
public String handleLogin(ModelMap model, @RequestParam String name){
model.addAttribute("name", name);
return "login";
}
在上面的方法中,
- 谁在什么情况下调用此方法?
- 调用者在调用此方法之前对模型做了什么?
- 此 ModelMap 对象位于哪个范围?是否在请求范围内?
- 该类方法的签名有什么要求? 我可以任意创建如下方法:
------------------示例方法#2-------------------------
@RequestMapping(value = "/login")
public String handleLogin(String s1, ModelMap model_1, int i, ModelMap model_2 @RequestParam String name){
model_1.addAttribute("name", name);
model_2.addAttribute("company", "good");//what are the initial status of model_1 and model_2?
return "login";
}
在这个方法中,我放置了2个ModelMap参数,它们的位置是任意的。 spring 框架是否可以调用此方法,以便可以使用 modelmap 将数据传输到 View ,就像示例方法 #1 中的常规情况一样?
一般来说,有人可以在将 ModelMap 传递到方法之前说明其生命周期的详细信息吗?
最佳答案
请在下面找到答案:
(1) 谁在什么情况下调用此方法?
This controller method will be invoked by Spring Dispatcher Servlet (& handler methods) when "/login" request comes to the server
(2) 调用者在调用此方法之前对模型做了什么?
Spring Dispatcher Servlet provides this model object for use with UI tools.
(3) 这个 ModelMap 对象在哪个范围内?是否在请求范围内?
No, Modelmap object will be used internally by spring handler methods to add the user defined objects (to be presented in UI) into the http response.
(4)该类方法的签名有什么要求?可以
model_1 和 model_2 都将引用相同的模型对象,因为每个请求都会维护一个对象。 Controller 方法签名很灵活,您可以在下面的链接中找到更多信息: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-methods
有关 Modelmap 的更多详细信息,请参阅以下内容:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/ui/ModelMap.html
另外,请引用下面的 spring 调度程序 servlet 生命周期: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html
关于java - 谁将 Spring ModelMap 作为参数传递给 Controller 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38473058/