Spring中Model,ModelMap以及ModelAndView之间的区别

2018/03/06 18:20
阅读数 29

<div id="article_content" class="article_content csdn-tracking-statistics tracking-click" data-mod="popu_519" data-dsm="post" style="overflow: hidden;"> <div class="htmledit_views">

<h2><a name="t0"></a>1.场景分析</h2> <p>&nbsp; &nbsp; 在许多实际项目需求中,后台要从控制层直接返回前端所需的数据,这时Model大家族就派上用场了。</p> <h2><a name="t1"></a>2.三者区别</h2> <p>①Model</p> <p>Model是一个接口,它的实现类为ExtendedModelMap,继承ModelMap类</p> <p></p><pre class="prettyprint" style="white-space:nowrap;margin-top:0px;margin-bottom:1.1em;font-family:'Source Code Pro', monospace;padding:5px 5px 5px 60px;font-size:14px;line-height:1.45;color:rgb(51,51,51);background-color:rgba(128,128,128,.05);border:0px solid rgb(136,136,136);" name="code"><code class="language-java hljs has-numbering" style="display:block;padding:0px;background:transparent;color:inherit;font-family:'Source Code Pro', monospace;white-space:pre;"><span class="hljs-keyword" style="color:rgb(0,0,136);">public</span> <span class="hljs-class"><span class="hljs-keyword" style="color:rgb(0,0,136);">class</span> <span class="hljs-title" style="color:rgb(102,0,102);">ExtendedModelMap</span> <span class="hljs-keyword" style="color:rgb(0,0,136);">extends</span> <span class="hljs-title" style="color:rgb(102,0,102);">ModelMap</span> <span class="hljs-keyword" style="color:rgb(0,0,136);">implements</span> <span class="hljs-title" style="color:rgb(102,0,102);">Model</span></span></code></pre><ul class="pre-numbering" style="background-color:rgb(238,238,238);margin:0px;padding:6px 0px 40px;border-right:1px solid rgb(221,221,221);list-style:none;text-align:right;opacity:0;"><li style="padding:0px 5px;">1</li></ul><div class="save_code tracking-ad" style="color:rgb(255,255,255);z-index:2;"><a style="color:rgb(79,161,219);" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets_01.png" style="border:none;" alt=""></a></div>②ModelMap <p>ModelMap继承LinkedHashMap,spring框架自动创建实例并作为controller的入参,用户无需自己创建</p> <p></p><pre class="prettyprint" style="margin-top:0px;margin-bottom:1.1em;background-color:rgba(128,128,128,.05);white-space:nowrap;font-family:'Source Code Pro', monospace;padding:5px 5px 5px 60px;font-size:14px;line-height:1.45;border:0px solid rgb(136,136,136);" name="code"><code class="language-java hljs has-numbering" style="display:block;padding:0px;background:transparent;font-family:'Source Code Pro', monospace;white-space:pre;"><span class="hljs-keyword" style="color:rgb(0,0,136);">public</span><span style="color:rgba(0,0,0,0);"> </span><span class="hljs-class"><span class="hljs-keyword" style="color:rgb(0,0,136);">class</span><span style="color:rgba(0,0,0,0);"> </span><span style="color:#660066;">ModelMap extends LinkedHashMap&lt;String,Object&gt;</span></span></code></pre><ul class="pre-numbering" style="color:rgb(51,51,51);background-color:rgb(238,238,238);margin:0px;padding:6px 0px 40px;border-right:1px solid rgb(221,221,221);list-style:none;text-align:right;opacity:0;"><li style="padding:0px 5px;">1</li></ul><div class="save_code tracking-ad" style="color:rgb(255,255,255);z-index:2;"><a style="color:rgb(79,161,219);" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets_01.png" style="border:none;" alt=""></a></div><div style="color:rgb(51,51,51);"><a style="color:rgb(79,161,219);" target="_blank"> </a></div>③ModelAndView <p>顾名思义,ModelAndView指模型和视图的集合,既包含模型 又包含视图;ModelAndView的实例是开发者自己手动创建的,这也是和ModelMap主要不同点之一;</p> <h2><a name="t2"></a>3.实例说明</h2> <p>准备工作:springboot工程集成了velocity模版引擎,暂不会的伙伴看看笔者velocity集成的博客:<a href="http://blog.csdn.net/zhangxing52077/article/details/73194948" target="_blank">http://blog.csdn.net/zhangxing52077/article/details/73194948</a>,恕不赘述。</p> <p>①Model的用法</p> <p>spring自动为Model创建实例,并作为controller的入参</p> <p></p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:Consolas;font-size:12pt;"><span style="color:#bbb529;">@RequestMapping</span>(<span style="color:#6a8759;">"hello"</span>)</br> <span style="color:#cc7832;">public </span>String <span style="color:#ffc66d;">testVelocity</span>(Model model<span style="color:#cc7832;">,</span>String name){</br> model.addAttribute(<span style="color:#6a8759;">"name"</span><span style="color:#cc7832;">,</span>name)<span style="color:#cc7832;">;</br> </span><span style="color:#cc7832;"> return </span><span style="color:#6a8759;">"hello"</span><span style="color:#cc7832;">;</br> </span>}</pre> <p></p> <p>测试效果:</p> <p><img src="http://img.blog.csdn.net/20170715212403379?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmd4aW5nNTIwNzc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br></p> <p>②Model与model用法差不多</p> <p></p> <pre style="background-color:rgb(43,43,43);color:rgb(169,183,198);font-family:Consolas;font-size:12pt;"><span style="color:rgb(187,181,41);">@RequestMapping</span>(<span style="color:rgb(106,135,89);">"hello"</span>)</br> <span style="color:rgb(204,120,50);">public </span>String <span style="color:rgb(255,198,109);">testVelocity</span>(ModelMap model<span style="color:rgb(204,120,50);">,</span>String name){</br> model.addAttribute(<span style="color:rgb(106,135,89);">"name"</span><span style="color:rgb(204,120,50);">,</span>name)<span style="color:rgb(204,120,50);">;</br> </span><span style="color:rgb(204,120,50);"> return </span><span style="color:rgb(106,135,89);">"hello"</span><span style="color:rgb(204,120,50);">;</br> </span>}</pre> 这里效果跟Model运行效果一样 <p></p> <p>③ModelAndView的用法</p> <p></p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:Consolas;font-size:12pt;"><span style="color:#bbb529;">@RequestMapping</span>(<span style="color:#6a8759;">"model"</span>)</br> <span style="color:#cc7832;">public </span>ModelAndView <span style="color:#ffc66d;">testModel</span>(String name) {</br> <span style="color:#808080;">//</span><span style="color:#808080;font-family:'Courier New';">构建</span><span style="color:#808080;">ModelAndView</span><span style="color:#808080;font-family:'Courier New';">实例,并设置跳转地址</br> </span><span style="color:#808080;font-family:'Courier New';"> </span>ModelAndView view = <span style="color:#cc7832;">new </span>ModelAndView(<span style="color:#6a8759;">"test"</span>)<span style="color:#cc7832;">;</br> </span><span style="color:#cc7832;"> </span><span style="color:#808080;">//</span><span style="color:#808080;font-family:'Courier New';">将数据放置到</span><span style="color:#808080;">ModelAndView</span><span style="color:#808080;font-family:'Courier New';">对象</span><span style="color:#808080;">view</span><span style="color:#808080;font-family:'Courier New';">中</span><span style="color:#808080;">,</span><span style="color:#808080;font-family:'Courier New';">第二个参数可以是任何</span><span style="color:#808080;">java</span><span style="color:#808080;font-family:'Courier New';">类型</br> </span><span style="color:#808080;font-family:'Courier New';"> </span>view.addObject(<span style="color:#6a8759;">"name"</span><span style="color:#cc7832;">,</span>name)<span style="color:#cc7832;">;</br> </span><span style="color:#cc7832;"> </span><span style="color:#808080;">//</span><span style="color:#808080;font-family:'Courier New';">返回</span><span style="color:#808080;">ModelAndView</span><span style="color:#808080;font-family:'Courier New';">对象</span><span style="color:#808080;">view</br> </span><span style="color:#808080;"> </span><span style="color:#cc7832;">return </span>view<span style="color:#cc7832;">;</br> </span>}</pre> <p></p> <p>设置view跳转地址</p> <p></p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:Consolas;font-size:12pt;">ModelAndView view = <span style="color:#cc7832;">new </span>ModelAndView(<span style="color:#6a8759;">"test"</span>)<span style="color:#cc7832;">;</span></pre> <p></p> <p>当然还可以这样设置跳转地址</p> <p></p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:Consolas;font-size:12pt;">ModelAndView view = <span style="color:#cc7832;">new </span>ModelAndView()<span style="color:#cc7832;">; </span>view.setViewName(<span style="color:#6a8759;">"test"</span>)<span style="color:#cc7832;">;</span></pre> <p></p> <p>其中的test表示templates中的test.html,springboot默认的模版文件一般都在resources/templates下</p> <p><img src="http://img.blog.csdn.net/20170715213336355?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmd4aW5nNTIwNzc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br></p> 设置view的model <p></p> <pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:Consolas;font-size:12pt;">view.addObject(<span style="color:#6a8759;">"name"</span><span style="color:#cc7832;">,</span><span style="color:#6a8759;">"zhangxing,hello"</span>)<span style="color:#cc7832;">;</span></pre> 运行效果: <p></p> <p><img src="http://img.blog.csdn.net/20170715213623209?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmd4aW5nNTIwNzc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br></p> <p>这样既跳转了前端页面,又把值传过来了,一石二鸟!</p> <p><br></p> <p><br></p> </div> </div>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部