在Spring MVC中,Model的实际实现是什么?

huangapple go评论68阅读模式
英文:

What is the actual implementation of Model in Spring MVC

问题

很多人已经回答了 Model 是一个接口,而 ModelView 则是实现了 Map 接口的类。

  1. 我的疑惑是,实际上是什么实现了接口 Model 呢?(我是 Spring MVC 的初学者,请耐心点。您可以参考这个帖子的代码提示:链接

  2. 更有趣的是,我看到有人只是使用了 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&lt;&gt; 应该是声明的类型,而 modelmap 则是实际的类型?有人可以和我澄清一下吗?非常感谢。

----------------------------------更新----------------------------

对于第一个问题,实际上在 IntelliJ 中很容易检查。只需打开源代码包,感谢 Elmar Brauch。我将呈现这张图片:
在Spring MVC中,Model的实际实现是什么?

英文:

A lot of people have answered that Model is an interface while ModelView is a class implementing Map interface.

  1. 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

  2. More interestingly, I saw someone just use Map&lt;&gt; interface:

// When the path is routed to '/new' below method to be called and view //returned is newPokemon

@RequestMapping(method = RequestMethod.GET, value =&quot;/new&quot;)

    public String newPokemonForm(Map&lt;String, Object&gt; model) {
    Pokemon Pokemon = new Pokemon();
    model.put(&quot;pokemon&quot;, Pokemon);
    return &quot;newPokemon&quot;;
}

So I am thinking for this model parameter, Map&lt;&gt; 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:
在Spring MVC中,Model的实际实现是什么?

答案1

得分: 1

如果您配置构建工具以下载源代码,您可以直接打开Spring的Model接口。类型层次结构(您可以在Eclipse中打开它)向您展示以下实现了Model接口的类:

- public class ConcurrentModel extends ConcurrentHashMap&lt;String,Object&gt; 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

huangapple
  • 本文由 发表于 2020年10月9日 13:27:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64274397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定