英文:
What is the actual implementation of Model in Spring MVC
问题
很多人已经回答了 Model
是一个接口,而 ModelView
则是实现了 Map
接口的类。
-
我的疑惑是,实际上是什么实现了接口
Model
呢?(我是 Spring MVC 的初学者,请耐心点。您可以参考这个帖子的代码提示:链接) -
更有趣的是,我看到有人只是使用了
Map<>
接口:
// 当路径被路由到 '/new' 时,将调用以下方法并返回视图 newPokemon
@RequestMapping(method = RequestMethod.GET, value = "/new")
public String newPokemonForm(Map<String, Object> model) {
Pokemon Pokemon = new Pokemon();
model.put("pokemon", Pokemon);
return "newPokemon";
}
所以我在思考这个模型 参数,Map<>
应该是声明的类型,而 modelmap
则是实际的类型?有人可以和我澄清一下吗?非常感谢。
----------------------------------更新----------------------------
对于第一个问题,实际上在 IntelliJ 中很容易检查。只需打开源代码包,感谢 Elmar Brauch。我将呈现这张图片:
英文:
A lot of people have answered that Model
is an interface while ModelView
is a class implementing Map
interface.
-
My confusion is what actually implements interface
Model
then? (I am a beginner of Spring MVC so please be patient
You can refer to this thread for code hint.
https://stackoverflow.com/questions/18486660/what-are-the-differences-between-model-modelmap-and-modelandview -
More interestingly, I saw someone just use
Map<>
interface:
// When the path is routed to '/new' below method to be called and view //returned is newPokemon
@RequestMapping(method = RequestMethod.GET, value ="/new")
public String newPokemonForm(Map<String, Object> model) {
Pokemon Pokemon = new Pokemon();
model.put("pokemon", Pokemon);
return "newPokemon";
}
So I am thinking for this model parameter, Map<>
should be declared type while modelmap is the actual type?
Can anyone clarify with me?
Thanks a lot
----------------------------------Update----------------------------
For the first question, actually it is easy to check in Intellij. Just open the source code package thanks to Elmar Brauch. I will present the picture:
答案1
得分: 1
如果您配置构建工具以下载源代码,您可以直接打开Spring的Model接口。类型层次结构(您可以在Eclipse中打开它)向您展示以下实现了Model接口的类:
- public class ConcurrentModel extends ConcurrentHashMap<String,Object> implements Model
- public class ExtendedModelMap extends ModelMap implements Model
在这两个类中,您可以看到它们都扩展了某种Map类,该类在超类中实现了Map接口。
在运行时,您可以确定用作Map接口实现的类型。设置断点并在调试模式下检查,或者执行类似于此的操作:
@PostMapping
public void postMap(@RequestBody Map map) {
System.out.println(map.getClass());
}
对于我使用@RestController注释的类,这将打印以下内容:
class java.util.LinkedHashMap
英文:
If you configure your build tool to download sources, you can just open Spring's Model interface. The type hierarchy (you can open it in Eclipse) shows you, that the following classes implement Model interface:
- public class ConcurrentModel extends ConcurrentHashMap<String, Object> implements Model
- public class ExtendedModelMap extends ModelMap implements Model
In both classes you can see that they extend some kind of Map-class, which implements in a super class the Map interface.
At runtime You can figure out, which type is used as implementation of Map interface. Set a breakpoint and inspect in debug mode or do something like this:
@PostMapping
public void postMap(@RequestBody Map map) {
System.out.println(map.getClass());
}
This prints for my with @RestController annotated class this type:
class java.util.LinkedHashMap
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论