简体   繁体   English

与Spring MVC模型对象有关的一些疑问

[英]Some doubts related to Spring MVC model object

I am studying Spring MVC and I have some doubts related the concept of model . 我正在研究Spring MVC,并且对模型的概念有些疑问。

So if I have a controller like this: 因此,如果我有一个像这样的控制器

@Controller
public class RewardController {

    private RewardLookupService lookupService;

    @Autowired
    public RewardController(RewardLookupService svc) {
        this.lookupService = svc;
    }

    @RequestMapping("/reward/show")
    public String show(@RequestParam("id") long id, Model model) {
        Reward reward = lookupService.lookupReward(id);
        model.addAttribute(“reward”, reward);
        return “rewardView”;
    }
}

So, into this controller is definied the show() method that handle HttpRequest toward the /reward/show path and take 2 input parameters: 因此,在该控制器中定义了show()方法,该方法将HttpRequest朝向/ reward / show路径处理并采用2个输入参数:

  • long id : that is extract from the URL of the request, something like /reward/show?id=1 长id :从请求的网址中提取,类似于/ reward / show?id = 1

  • The Model model object: I think that it contains the values to sharw with the view. Model模型对象:我认为它包含要与视图共享的值。

So this controller method perform a query on the DB and obtain a Reward object that put into the Model object. 因此,此控制器方法对数据库执行查询,并获得放入模型对象的Reward对象。

So I can have this simple view named for example rewardView.jsp (the name and the path is automatically build by the Spring view resolveréé) that show the content of the **Model object: 因此,我可以将这个简单的视图命名为例如rewardView.jsp (名称和路径由Spring 视图resolveréé自动构建),该视图显示** Model对象的内容

<html>
    <head><title>Your Reward</title></head>

    <body>
        Amount=${reward.amount} <br/>
        Date=${reward.date} <br/>
        Account Number=${reward.account} <br/>
        Merchant Number=${reward.merchant}
    </body>
</html>

So my doubts are: 所以我的疑问是:

  1. Is the Model object a specific implementation of a Java Map or is an object that wrap a Map? Model对象是Java Map的特定实现还是包装Map的对象? I think so because, as in a Map, I have a couple 我认为是因为,例如在地图中,我有几个 where the KEY is the field name and the VALUE is its specific value to show in the view. 其中KEY是字段名称,而VALUE是要在视图中显示的特定值。

  2. If my previous reasoning is correct the addAttribute() is a specific Spring method to put an element into this Map? 如果我以前的推理是正确的,则addAttribute()是将元素放入此Map的特定Spring方法? why is not used directly the Map put() method? 为什么不直接使用Map put()方法?

Tnx 特纳克斯

Model is actually an interface, which declares the addAttribute method you mention. 模型实际上是一个接口,它声明您提到的addAttribute方法。 In your Show method you are effectively using this interface to access a Map. 在您的Show方法中,您可以有效地使用此界面访问地图。

The "put" methods are not available through the Model interface, so you instead need to use addAttribute (which performs basic sanity checks on your key/value arguments and then "puts" them into the underlying map for you. “放置”方法无法通过模型接口使用,因此您需要使用addAttribute(它对键/值参数执行基本的健全性检查,然后为您“放入”基础图。

ExtendedModelMap is the implementation of Model object. ExtendedModelMapModel对象的实现。

This class is extending ModelMap which itself is extending HashMap. 此类扩展了ModelMap,而ModelMap本身也在扩展HashMap。

And addAttribute name is more meaningful as we are using it in servlet/web environment. 当在Servlet / Web环境中使用它时, addAttribute名称更有意义。

The model (the M in MVC) is a Map interface , which allows for the complete abstraction of the view technology. model (MVC中的M)是Map interface ,它允许对视图技术进行完全抽象。 You can integrate directly with template based rendering technologies such as JSP, Velocity and Freemarker, or directly generate XML, JSON, Atom, and many other types of content. 您可以直接与基于模板的渲染技术(例如JSP,Velocity和Freemarker)集成,也可以直接生成XML,JSON,Atom和许多其他类型的内容。 The model Map is simply transformed into an appropriate format, such as JSP request attributes, a Velocity template model. 模型Map可以简单地转换为适当的格式,例如JSP请求属性,Velocity模板模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM