Spring MVC工作原理 及注解说明

<div id="article_details" class="details">
    <div class="article_title">   
         <span class="ico ico_type_Repost"></span>

    <h1>
        <span class="link_title"><a href="/u012191627/article/details/41943393">
        Spring MVC工作原理 及注解说明        
           
        </a>
        </span>

         
    </h1>
</div>

   

        <div class="article_manage clearfix">
        <div class="article_r">
            <span class="link_postdate">2014-12-15 14:31</span>
            <span class="link_view" title="阅读次数">8866人阅读</span>
            <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
            <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('Spring+MVC%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86+%e5%8f%8a%e6%b3%a8%e8%a7%a3%e8%af%b4%e6%98%8e','41943393');return false;" title="收藏" target="_blank">收藏</a></span>
             <span class="link_report"> <a href="#report" onclick="javascript:report(41943393,2);return false;" title="举报">举报</a></span>

        </div>
    </div>    <style type="text/css">        
            .embody{
                padding:10px 10px 10px;
                margin:0 -20px;
                border-bottom:solid 1px #ededed;                
            }
            .embody_b{
                margin:0 ;
                padding:10px 0;
            }
            .embody .embody_t,.embody .embody_c{
                display: inline-block;
                margin-right:10px;
            }
            .embody_t{
                font-size: 12px;
                color:#999;
            }
            .embody_c{
                font-size: 12px;
            }
            .embody_c img,.embody_c em{
                display: inline-block;
                vertical-align: middle;               
            }
             .embody_c img{               
                width:30px;
                height:30px;
            }
            .embody_c em{
                margin: 0 20px 0 10px;
                color:#333;
                font-style: normal;
            }
    </style>
    <script type="text/javascript">
        $(function () {
            try
            {
                var lib = eval("("+$("#lib").attr("value")+")");
                var html = "";
                if (lib.err == 0) {
                    $.each(lib.data, function (i) {
                        var obj = lib.data[i];
                        //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                        html += ' <a href="' + obj.url + '" target="_blank">';
                        html += ' <img src="' + obj.logo + '">';
                        html += ' <em><b>' + obj.name + '</b></em>';
                        html += ' </a>';
                    });
                    if (html != "") {
                        setTimeout(function () {
                            $("#lib").html(html);                      
                            $("#embody").show();
                        }, 100);
                    }
                }      
            } catch (err)
            { }
            
        });
    </script>
      <div class="category clearfix">
        <div class="category_l">
           <img src="http://static.blog.csdn.net/images/category_icon.jpg">
            <span>分类:</span>
        </div>
        <div class="category_r">
                    <label onclick="GetCategoryArticles('2710905','u012191627','top','41943393');">
                        <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">经典转载<em>(17)</em></span>
                      <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                      <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                        <div class="subItem">
                            <div class="subItem_t"><a href="http://blog.csdn.net/u012191627/article/category/2710905" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                            <ul class="subItem_l" id="top_2710905">                            
                            </ul>
                        </div>
                    </label>                    
        </div>
    </div>

 

 
 
     


<div style="clear:both"></div><div style="border:solid 1px #ccc; background:#eee; float:left; min-width:200px;padding:4px 10px;"><p style="text-align:right;margin:0;"><span style="float:left;">目录<a href="#" title="系统根据文章中H1到H6标签自动生成文章目录">(?)</a></span><a href="#" onclick="javascript:return openct(this);" title="收起">[-]</a></p><ol style="margin-left: 14px; padding-left: 14px; line-height: 160%; display: block;"><li><a href="#t0">
SpringMVC框架介绍</a></li><li><a href="#t1">
SpringMVC原理图</a></li><li><a href="#t2">
SpringMVC接口解释
</a></li><li><a href="#t3">
SpringMVC运行原理</a></li></ol></div><div style="clear:both"></div><div id="article_content" class="article_content tracking-ad" data-mod="popu_307" data-dsm="post">

<h1 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px; background-color:rgb(255,255,255)"><a name="t0" target="_blank"></a>
<span style="font-size:24px"><span style="font-family:'Microsoft YaHei'">SpringMVC框架介绍</span></span></h1>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'"><span style="font-size:18px">&nbsp;&nbsp;&nbsp; 1) <a href="http://lib.csdn.net/base/javaee" class="replace_word" title="Java EE知识库" target="_blank" style="color:#df3434; font-weight:bold;">spring</a> MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。</span></span></p>
<blockquote style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); border:medium none; padding:0px; margin:0px 0px 0px 40px">
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC <a href="http://lib.csdn.net/base/architecture" class="replace_word" title="大型网站架构知识库" target="_blank" style="color:#df3434; font-weight:bold;">架构</a>,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架。通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages(JSP)技术、Velocity、Tiles、iText 和 POI。Spring
 MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术。</span></p>
</blockquote>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'; font-size:18px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。</span></p>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'"><span style="font-size:18px">&nbsp;&nbsp;&nbsp;&nbsp;2) Spring的MVC框架主要由DispatcherServlet、处理器映射、处理器(控制器)、视图解析器、视图组成。</span></span></p>
<h1 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px; background-color:rgb(255,255,255)"><a name="t1" target="_blank"></a>
<a target="_blank" name="t1"></a><a target="_blank" name="t1" style="color:rgb(51,102,153)"></a><span style="font-family:'Microsoft YaHei'; font-size:24px">SpringMVC原理图</span></h1>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<strong><span style="font-family:'Microsoft YaHei'; font-size:24px"><img alt="" src="https://img-my.csdn.net/uploads/201211/16/1353059506_5137.jpg" style="border:none" width="712" height="427" align="middle"></span></strong></p>
<h1 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px; background-color:rgb(255,255,255)"><a name="t2" target="_blank"></a>
<a target="_blank" name="t2"></a><a target="_blank" name="t2" style="color:rgb(51,102,153)"></a><span style="font-family:'Microsoft YaHei'; font-size:24px">SpringMVC接口解释</span><br>
</h1>
<blockquote style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); border:medium none; padding:0px; margin:0px 0px 0px 40px">
</blockquote>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">DispatcherServlet接口:</span></span></p>
<blockquote style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); border:medium none; padding:0px; margin:0px 0px 0px 40px">
</blockquote>
<blockquote dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); margin-right:0px">
<p><span style="font-family:'Microsoft YaHei'"><span style="font-size:18px">Spring提供的前端控制器,所有的请求都有经过它来统一分发。在DispatcherServlet将请求分发给Spring Controller之前,需要借助于Spring提供的HandlerMapping定位到具体的Controller。</span></span></p>
</blockquote>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">HandlerMapping接口:</span></span></p>
<blockquote dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); margin-right:0px">
<p><span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">能够完成客户请求到Controller映射。</span></span></p>
</blockquote>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">Controller接口:</span></span></p>
<blockquote dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); margin-right:0px">
<p dir="ltr"><span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">需要为并发用户处理上述请求,因此实现Controller接口时,必须保证线程安全并且可重用。</span></span></p>
<p dir="ltr"><span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">Controller将处理用户请求,这和Struts Action扮演的角色是一致的。一旦Controller处理完用户请求,则返回ModelAndView对象给DispatcherServlet前端控制器,ModelAndView中包含了模型(Model)和视图(View)。</span></span></p>
<p dir="ltr"><span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">从宏观角度考虑,DispatcherServlet是整个Web应用的控制器;从微观考虑,Controller是单个Http请求处理过程中的控制器,而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)。</span></span></p>
</blockquote>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'; font-size:18px">ViewResolver接口:</span></p>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); border:medium none; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; margin-left:40px">
<span style="font-family:'Microsoft YaHei'; font-size:18px"></span></p>
<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); border:medium none; padding-top:0px; padding-bottom:0px; margin-top:0px; margin-bottom:0px; margin-left:40px">
<span style="font-size:18px"><span style="font-family:'Microsoft YaHei'">Spring提供的视图解析器(ViewResolver)在Web应用中查找View对象,从而将相应结果渲染给客户。</span></span></p>
<h1 dir="ltr" style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px; background-color:rgb(255,255,255)"><a name="t3" target="_blank"></a>
<a target="_blank" name="t3"></a><a target="_blank" name="t3" style="color:rgb(51,102,153)"></a><span style="font-family:'Microsoft YaHei'; font-size:24px">SpringMVC运行原理</span></h1>
<blockquote dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255); margin-right:0px">
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">1. 客户端请求提交到DispatcherServlet</span></p>
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">2. 由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller</span></p>
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">3. DispatcherServlet将请求提交到Controller</span></p>
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">4. Controller调用业务逻辑处理后,返回ModelAndView</span></p>
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图</span></p>
<p><span style="font-family:'Microsoft YaHei'; font-size:18px">6. 视图负责将结果显示到客户端</span></p>
</blockquote>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'; font-size:18px">DispatcherServlet是整个Spring MVC的核心。它负责接收HTTP请求组织协调Spring MVC的各个组成部分。其主要工作有以下三项:</span></p>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'; font-size:18px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1. 截获符合特定格式的URL请求。<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2. 初始化DispatcherServlet上下文对应的WebApplicationContext,并将其与业务层、持久化层的WebApplicationContext建立关联。<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3. 初始化Spring MVC的各个组成组件,并装配到DispatcherServlet中。</span></p>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<br>
</p>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<br>
</p>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<br>
</p>
<p dir="ltr" style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px; background-color:rgb(255,255,255)">
<span style="font-family:'Microsoft YaHei'; font-size:18px">&lt;mvc:annotation-driven /&gt; 说明:</span></p>
<div>&nbsp;</div>
<div>是一种简写形式,可以让初学者快速成应用默认的配置方案,会默认注册 DefaultAnnotationHandleMapping以及AnnotionMethodHandleAdapter 这两个 Bean, 这两个 Bean ,前者对应类级别, 后者对应到方法级别;</div>
<div>&nbsp;</div>
<div>上在面的 DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter 是 Spring 为 @Controller 分发请求所必需的。</div>
<div>&nbsp;</div>
<div>annotation-driven 扫描指定包中类上的注解,常用的注解有:</div>
<div>&nbsp;</div>
<div>复制代码</div>
<div>@Controller 声明Action组件</div>
<div>@Service 声明Service组件 @Service("myMovieLister")</div>
<div>@Repository 声明Dao组件</div>
<div>@Component 泛指组件, 当不好归类时.</div>
<div>@RequestMapping("/menu") 请求映射</div>
<div>@Resource 用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName")</div>
<div>@Autowired 用于注入,(srping提供的) 默认按类型装配</div>
<div>@Transactional( rollbackFor={Exception.class}) 事务管理</div>
<div>@ResponseBody</div>
<div>@Scope("prototype") 设定bean的作用域</div>
<br>
<p></p>
   
</div>




<!-- Baidu Button BEGIN -->




<div class="bdsharebuttonbox tracking-ad bdshare-button-style0-16" style="float: right;" data-mod="popu_172" data-bd-bind="1498779645089">
<a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important" target="_blank"></a>
<a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间" style="background-position:0 -52px !important" target="_blank"></a>
<a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博" style="background-position:0 -104px !important" target="_blank"></a>
<a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博" style="background-position:0 -260px !important" target="_blank"></a>
<a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网" style="background-position:0 -208px !important" target="_blank"></a>
<a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信" style="background-position:0 -1612px !important" target="_blank"></a>
</div>
<script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script>
<!-- Baidu Button END -->

   

<!--172.16.140.15-->

<!-- Baidu Button BEGIN -->
<script type="text/javascript" id="bdshare_js" data="type=tools&amp;uid=1536434" src="http://bdimg.share.baidu.com/static/js/bds_s_v2.js?cdnversion=416328"></script>

<script type="text/javascript">
    document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->



 


        <div id="digg" articleid="41943393">
            <dl id="btnDigg" class="digg digg_disable" onclick="btndigga();">
               
                 <dt>顶</dt>
                <dd>3</dd>
            </dl>
           
              
            <dl id="btnBury" class="digg digg_disable" onclick="btnburya();">
              
                  <dt>踩</dt>
                <dd>0</dd>               
            </dl>
            
        </div>
     <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank">&nbsp;</a>   </div>
    <div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank">&nbsp;</a></div>
    <script type="text/javascript">
                function btndigga() {
                    $(".tracking-ad[data-mod='popu_222'] a").click();
                }
                function btnburya() {
                    $(".tracking-ad[data-mod='popu_223'] a").click();
                }
            </script>

   <ul class="article_next_prev">
                <li class="prev_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='/u012191627/article/details/41895663';">上一篇</span><a href="/u012191627/article/details/41895663" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">Mybatis 中在传参时,$ 和# 的区别</a></li>
                <li class="next_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='/u012191627/article/details/41961713';">下一篇</span><a href="/u012191627/article/details/41961713" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">&lt;mvc:annotation-driven /&gt;注解意义</a></li>
    </ul>

    <div style="clear:both; height:10px;"></div>


            <div class="similar_article">
                    <h4></h4>
                    <div class="similar_c" style="margin:20px 0px 0px 0px">
                        <div class="similar_c_t">
                          &nbsp;&nbsp;相关文章推荐
                        </div>
                   
                        <div class="similar_wrap tracking-ad" data-mod="popu_36">                       
                            <ul class="similar_list fl">    
                            </ul>
                              <ul class="similar_list fr">      
                            </ul>
                        </div>
                    </div>
                </div>   
      
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值