{"id":1632,"date":"2017-07-11T17:12:33","date_gmt":"2017-07-11T09:12:33","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1632"},"modified":"2017-07-11T17:12:33","modified_gmt":"2017-07-11T09:12:33","slug":"%e8%af%a6%e8%a7%a3spring-mvc%e4%b8%ad%e7%9a%84controller","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1632","title":{"rendered":"\u8be6\u89e3Spring MVC\u4e2d\u7684Controller"},"content":{"rendered":"<p>\n\t\u8fd9\u7bc7\u6587\u7ae0\u672c\u6765\u662f\u5df2\u7ecf\u5199\u597d\u4e86\uff0c\u4f46\u662f\u5728\u4fdd\u5b58\u53d1\u5e03\u4e4b\u524d\u6536\u5230\u4e86\u6765\u81ea\u7f8e\u56fd\u7684\u653b\u51fb\u3002\u5bfc\u81f4\u6574\u4e2a\u670d\u52a1\u5668\u65e0\u6cd5\u8bbf\u95ee\u4e86\uff0c\u8fd9\u91cc\u53ea\u80fd\u91cd\u65b0\u5199\u4e0a\u4e00\u7bc7\u3002\n<\/p>\n<p>\n\t\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u5b66\u4e60\u4e86<a href=\"http:\/\/www.xttblog.com\/?p=1629\" name=\"\u8be6\u89e3Spring MVC\u4e2dDispatcherServlet\u7684\u4f5c\u7528\" target=\"_blank\" title=\"\u8be6\u89e3Spring MVC\u4e2dDispatcherServlet\u7684\u4f5c\u7528\">DispatcherServlet<\/a>\u7684\u5185\u5bb9\uff0c<a href=\"http:\/\/www.xttblog.com\/?tag=dispatcherservlet\" name=\"dispatcherservlet\" target=\"_blank\" title=\"dispatcherservlet\">DispatcherServlet<\/a>\u4f1a\u5c06\u6536\u5230\u7684\u8bf7\u6c42\u5206\u914d\u7ed9\u5404\u4e2a <a href=\"http:\/\/www.xttblog.com\/?tag=controller\" name=\"controller\" target=\"_blank\" title=\"controller\">Controller<\/a>\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecdController \u5728<a href=\"http:\/\/www.xttblog.com\/?tag=spring-mvc\" name=\"spring mvc\" target=\"_blank\" title=\"spring mvc\">Spring MVC<\/a>\u4e2d\u7684\u4f5c\u7528\u3002\n<\/p>\n<p>\n\tController\u63a7\u5236\u5668\uff0c\u662fMVC\u4e2d\u7684\u90e8\u5206C\uff0c\u4e3a\u4ec0\u4e48\u662f\u90e8\u5206\u5462\uff1f\u56e0\u4e3a\u6b64\u5904\u7684\u63a7\u5236\u5668\u4e3b\u8981\u8d1f\u8d23\u529f\u80fd\u5904\u7406\u90e8\u5206\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u6536\u96c6\u3001\u9a8c\u8bc1\u8bf7\u6c42\u53c2\u6570\u5e76\u7ed1\u5b9a\u5230\u547d\u4ee4\u5bf9\u8c61\uff1b\n\t<\/li>\n<li>\n\t\t\u5c06\u547d\u4ee4\u5bf9\u8c61\u4ea4\u7ed9\u4e1a\u52a1\u5bf9\u8c61\uff0c\u7531\u4e1a\u52a1\u5bf9\u8c61\u5904\u7406\u5e76\u8fd4\u56de\u6a21\u578b\u6570\u636e\uff1b\n\t<\/li>\n<li>\n\t\t\u8fd4\u56deModelAndView\uff08Model\u90e8\u5206\u662f\u4e1a\u52a1\u5bf9\u8c61\u8fd4\u56de\u7684\u6a21\u578b\u6570\u636e\uff0c\u89c6\u56fe\u90e8\u5206\u4e3a\u903b\u8f91\u89c6\u56fe\u540d\uff09\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u8fd8\u8bb0\u5f97DispatcherServlet\u5417\uff1f\u4e3b\u8981\u8d1f\u8d23\u6574\u4f53\u7684\u63a7\u5236\u6d41\u7a0b\u7684\u8c03\u5ea6\u90e8\u5206\uff1a\n<\/p>\n<ol>\n<li>\n\t\t\u8d1f\u8d23\u5c06\u8bf7\u6c42\u59d4\u6258\u7ed9\u63a7\u5236\u5668\u8fdb\u884c\u5904\u7406\uff1b\n\t<\/li>\n<li>\n\t\t\u6839\u636e\u63a7\u5236\u5668\u8fd4\u56de\u7684\u903b\u8f91\u89c6\u56fe\u540d\u9009\u62e9\u5177\u4f53\u7684\u89c6\u56fe\u8fdb\u884c\u6e32\u67d3\uff08\u5e76\u628a\u6a21\u578b\u6570\u636e\u4f20\u5165\uff09\u3002\n\t<\/li>\n<\/ol>\n<p>\n\t\u56e0\u6b64MVC\u4e2d\u5b8c\u6574\u7684C\uff08\u5305\u542b\u63a7\u5236\u903b\u8f91+\u529f\u80fd\u5904\u7406\uff09\u7531\uff08DispatcherServlet + Controller\uff09\u7ec4\u6210\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u56e0\u6b64\u6b64\u5904\u7684\u63a7\u5236\u5668\u662fWeb MVC\u4e2d\u90e8\u5206\uff0c\u4e5f\u53ef\u4ee5\u79f0\u4e3a\u9875\u9762\u63a7\u5236\u5668\u3001\u52a8\u4f5c\u3001\u5904\u7406\u5668\u3002<br \/>\n\t&nbsp;<br \/>\n\tSpring Web MVC\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u63a7\u5236\u5668\uff0c\u6bd4\u5982\u5b9e\u73b0Controller\u63a5\u53e3\uff0c\u4eceSpring2.5\u5f00\u59cb\u652f\u6301\u6ce8\u89e3\u65b9\u5f0f\u7684\u63a7\u5236\u5668\uff08\u5982@Controller\u3001@RequestMapping\u3001@RequestParam\u3001@ModelAttribute\u7b49\uff09\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u76f8\u5e94\u7684\u63a7\u5236\u5668\uff08\u53ea\u9700\u8981\u5b9a\u4e49\u76f8\u5e94\u7684HandlerMapping\u548cHandlerAdapter\u5373\u53ef\uff09\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u56e0\u4e3a\u8003\u8651\u5230\u8fd8\u6709\u90e8\u5206\u516c\u53f8\u4f7f\u7528\u7ee7\u627fController\u63a5\u53e3\u5b9e\u73b0\u65b9\u5f0f\uff0c\u56e0\u6b64\u6211\u4eec\u4e5f\u5b66\u4e60\u4e00\u4e0b\uff0c\u867d\u7136\u5df2\u7ecf\u4e0d\u63a8\u8350\u4f7f\u7528\u4e86\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u5bf9\u4e8e\u6ce8\u89e3\u65b9\u5f0f\u7684\u63a7\u5236\u5668\uff0c\u540e\u8fb9\u4f1a\u8be6\u7ec6\u8bb2\uff0c\u5728\u6b64\u6211\u4eec\u5148\u5b66\u4e60Spring2.5\u4ee5\u524d\u7684Controller\u63a5\u53e3\u5b9e\u73b0\u65b9\u5f0f\u3002\n<\/p>\n<h2>\n\tController\u63a5\u53e3<br \/>\n<\/h2>\n<pre class=\"brush:java;\">\r\npackage org.springframework.web.servlet.mvc;\r\npublic interface Controller {\r\n       ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;\r\n}<\/pre>\n<p>\n\t\u8fd9\u662f\u63a7\u5236\u5668\u63a5\u53e3\uff0c\u6b64\u5904\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5handleRequest\uff0c\u7528\u4e8e\u8fdb\u884c\u8bf7\u6c42\u7684\u529f\u80fd\u5904\u7406\uff0c\u5904\u7406\u5b8c\u8bf7\u6c42\u540e\u8fd4\u56deModelAndView\uff08Model\u6a21\u578b\u6570\u636e\u90e8\u5206 \u548c View\u89c6\u56fe\u90e8\u5206\uff09\u3002<br \/>\n\t&nbsp;<br \/>\n\t\u8fd8\u8bb0\u5f97\u7b2c\u4e8c\u7ae0\u7684HelloWorld\u5417\uff1f\u6211\u4eec\u7684HelloWorldController\u5b9e\u73b0Controller\u63a5\u53e3\uff0cSpring\u9ed8\u8ba4\u63d0\u4f9b\u4e86\u4e00\u4e9bController\u63a5\u53e3\u7684\u5b9e\u73b0\u4ee5\u65b9\u4fbf\u6211\u4eec\u4f7f\u7528\uff0c\u5177\u4f53\u7ee7\u627f\u4f53\u7cfb\u5982\u4e0b\u56fe\u6240\u793a\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Spring MVC Controller \u7c7b\u56fe\" src=\"http:\/\/b.hiphotos.baidu.com\/image\/pic\/item\/b21bb051f8198618c7d6cb1540ed2e738ad4e68a.jpg\" style=\"width: 860px;height: 502px\" \/>\n<\/p>\n<h2>\n\tWebContentGenerator<br \/>\n<\/h2>\n<p>\n\t\u4ece\u4e0a\u9762\u7684\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u4e0eController\u5e73\u884c\u7684\u8fd8\u6709\u4e00\u4e2aWebContentGenerator\u7c7b\u3002\u8be5\u7c7b\u7528\u4e8e\u63d0\u4f9b\u5982\u6d4f\u89c8\u5668\u7f13\u5b58\u63a7\u5236\u3001\u662f\u5426\u5fc5\u987b\u6709session\u5f00\u542f\u3001\u652f\u6301\u7684\u8bf7\u6c42\u65b9\u6cd5\u7c7b\u578b\uff08GET\u3001POST\u7b49\uff09\u7b49\uff0c\u8be5\u7c7b\u4e3b\u8981\u6709\u5982\u4e0b\u5c5e\u6027\uff1a\n<\/p>\n<ul>\n<li>\n\t\tSet&lt;String&gt; &nbsp; supportedMethods\uff1a\u8bbe\u7f6e\u652f\u6301\u7684\u8bf7\u6c42\u65b9\u6cd5\u7c7b\u578b\uff0c\u9ed8\u8ba4\u652f\u6301&ldquo;GET&rdquo;\u3001&ldquo;POST&rdquo;\u3001&ldquo;HEAD&rdquo;\uff0c\u5982\u679c\u6211\u4eec\u60f3\u652f\u6301&ldquo;PUT&rdquo;\uff0c\u5219\u53ef\u4ee5\u52a0\u5165\u8be5\u96c6\u5408&ldquo;PUT&rdquo;\u3002\n\t<\/li>\n<li>\n\t\tboolean requireSession = false\uff1a\u662f\u5426\u5f53\u524d\u8bf7\u6c42\u5fc5\u987b\u6709session\uff0c\u5982\u679c\u6b64\u5c5e\u6027\u4e3atrue\uff0c\u4f46\u5f53\u524d\u8bf7\u6c42\u6ca1\u6709\u6253\u5f00session\u5c06\u629b\u51faHttpSessionRequiredException\u5f02\u5e38\uff1b\n\t<\/li>\n<li>\n\t\tboolean useExpiresHeader = true\uff1a\u662f\u5426\u4f7f\u7528HTTP1.0\u534f\u8bae\u8fc7\u671f\u54cd\u5e94\u5934\uff1a\u5982\u679ctrue\u5219\u4f1a\u5728\u54cd\u5e94\u5934\u6dfb\u52a0\uff1a&ldquo;Expires\uff1a&rdquo;\uff1b\u9700\u8981\u914d\u5408cacheSeconds\u4f7f\u7528\uff1b\n\t<\/li>\n<li>\n\t\tboolean useCacheControlHeader = true\uff1a\u662f\u5426\u4f7f\u7528HTTP1.1\u534f\u8bae\u7684\u7f13\u5b58\u63a7\u5236\u54cd\u5e94\u5934\uff0c\u5982\u679ctrue\u5219\u4f1a\u5728\u54cd\u5e94\u5934\u6dfb\u52a0\uff1b\u9700\u8981\u914d\u5408cacheSeconds\u4f7f\u7528\uff1b\n\t<\/li>\n<li>\n\t\tboolean useCacheControlNoStore = true\uff1a\u662f\u5426\u4f7f\u7528HTTP 1.1\u534f\u8bae\u7684\u7f13\u5b58\u63a7\u5236\u54cd\u5e94\u5934\uff0c\u5982\u679ctrue\u5219\u4f1a\u5728\u54cd\u5e94\u5934\u6dfb\u52a0\uff1b\u9700\u8981\u914d\u5408cacheSeconds\u4f7f\u7528\uff1b\n\t<\/li>\n<li>\n\t\tprivate int cacheSeconds = -1\uff1a\u7f13\u5b58\u8fc7\u671f\u65f6\u95f4\uff0c\u6b63\u6570\u8868\u793a\u9700\u8981\u7f13\u5b58\uff0c\u8d1f\u6570\u8868\u793a\u4e0d\u505a\u4efb\u4f55\u4e8b\u60c5\uff08\u4e5f\u5c31\u662f\u8bf4\u4fdd\u7559\u4e0a\u6b21\u7684\u7f13\u5b58\u8bbe\u7f6e\uff09\n\t<\/li>\n<\/ul>\n<p>\n\t\u8be5\u62bd\u8c61\u7c7b\u9ed8\u8ba4\u88abAbstractController\u548cWebContentInterceptor\u7ee7\u627f\u3002\n<\/p>\n<h2>\n\tcacheSeconds<br \/>\n<\/h2>\n<p>\n\tcacheSeconds =0\u65f6\uff0c\u5219\u5c06\u8bbe\u7f6e\u5982\u4e0b\u54cd\u5e94\u5934\u6570\u636e\uff1a\n<\/p>\n<ul>\n<li>\n\t\tPragma\uff1ano-cache &nbsp;\/\/HTTP 1.0\u7684\u4e0d\u7f13\u5b58\u54cd\u5e94\u5934\n\t<\/li>\n<li>\n\t\tExpires\uff1a1L &nbsp;\/\/useExpiresHeader=true\u65f6\uff0cHTTP 1.0\n\t<\/li>\n<li>\n\t\tCache-Control \uff1ano-cache \/\/useCacheControlHeader=true\u65f6\uff0cHTTP 1.1\n\t<\/li>\n<li>\n\t\tCache-Control \uff1ano-store \/\/useCacheControlNoStore=true\u65f6\uff0c\u8be5\u8bbe\u7f6e\u662f\u9632\u6b62Firefox\u7f13\u5b58\n\t<\/li>\n<\/ul>\n<p>\n\tcacheSeconds&gt;0\u65f6\uff0c\u5219\u5c06\u8bbe\u7f6e\u5982\u4e0b\u54cd\u5e94\u5934\u6570\u636e\uff1a\n<\/p>\n<ul>\n<li>\n\t\tExpires\uff1aSystem.currentTimeMillis() + cacheSeconds * 1000L &nbsp;\/\/ useExpiresHeader=true\u65f6\uff0cHTTP 1.0\n\t<\/li>\n<li>\n\t\tCache-Control \uff1amax-age=cacheSeconds \/\/ useCacheControlHeader=true\u65f6\uff0cHTTP 1.1\n\t<\/li>\n<\/ul>\n<p>\n\tcacheSeconds&lt;0\u65f6\uff0c\u5219\u4ec0\u4e48\u90fd\u4e0d\u8bbe\u7f6e\uff0c\u5373\u4fdd\u7559\u4e0a\u6b21\u7684\u7f13\u5b58\u8bbe\u7f6e\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1632\">\u8be6\u89e3Spring MVC\u4e2d\u7684Controller<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u672c\u6765\u662f\u5df2\u7ecf\u5199\u597d\u4e86\uff0c\u4f46\u662f\u5728\u4fdd\u5b58\u53d1\u5e03\u4e4b\u524d\u6536\u5230\u4e86\u6765\u81ea\u7f8e\u56fd\u7684\u653b\u51fb\u3002\u5bfc\u81f4\u6574\u4e2a\u670d\u52a1\u5668\u65e0\u6cd5\u8bbf\u95ee\u4e86\uff0c\u8fd9\u91cc\u53ea\u80fd\u91cd\u65b0\u5199\u4e0a\u4e00 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1369,1368,1350,1367,1359],"class_list":["post-1632","post","type-post","status-publish","format-standard","hentry","category-java","tag-cacheseconds","tag-controller","tag-spring-mvc","tag-spring-mvc-"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1632"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1632\/revisions"}],"predecessor-version":[{"id":1634,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1632\/revisions\/1634"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}