{"id":1047,"date":"2021-10-23T10:42:42","date_gmt":"2021-10-23T02:42:42","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1047"},"modified":"2021-10-23T10:44:47","modified_gmt":"2021-10-23T02:44:47","slug":"spring-controller","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2021\/10\/23\/spring-controller\/","title":{"rendered":"Spring Controller"},"content":{"rendered":"\n<p>\u57fa\u672c\u4e0a\u6240\u4ee5\u7684\u7f51\u9875\u52a0\u8f7d\u90fd\u662f\u8fd9\u6837\u7684\u4e00\u4e2a\u8fc7\u7a0b\u3002\u5728Spring Boot\u65b9\u6848\u91cc\uff0c\u4e00\u4e2a\u7f51\u9875\u8bf7\u6c42\u5230\u4e86\u670d\u52a1\u5668\u540e\uff0c\u9996\u5148\u6211\u4eec\u8fdb\u5165\u7684\u662fJava Web\u670d\u52a1\u5668\uff0c\u7136\u540e\u8fdb\u5165Spring Boot\u5e94\u7528\uff0c\u6700\u540e\u5339\u914d\u5230\u67d0\u4e00\u4e2aSpring Controller \uff0c\u7136\u540e\u8def\u7531\u5230\u5177\u4f53\u67d0\u4e00\u4e2aBean\u7684\u65b9\u6cd5\uff0c\u6267\u884c\u5b8c\u540e\u8fd4\u56de\u7ed3\u679c\uff0c\u8f93\u51fa\u5230\u5ba2\u6237\u7aef\u6765\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Spring Controller \u6280\u672f\u6709\u4e09\u4e2a\u6838\u5fc3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Bean\u7684\u914d\u7f6e\uff1aController\u6ce8\u89e3\u8fd0\u7528<\/li><li>\u7f51\u7edc\u8d44\u6e90\u7684\u52a0\u8f7d\uff1a\u52a0\u8f7d\u7f51\u9875<\/li><li>\u7f51\u5740\u8def\u7531\u7684\u914d\u7f6e\uff1aRequestMapping\u6ce8\u89e3\u8fd0\u7528<\/li><\/ul>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u9996\u5148Controller\u672c\u8eab\u4e5f\u662f\u4e00\u4e2aSpring Bean\uff0c\u9700\u8981\u5728\u7c7b\u4e0a\u63d0\u4f9b\u4e00\u4e2a@Controller\u6ce8\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>@Controller<\/strong>\n<strong>public<\/strong> <strong>class<\/strong> <strong>HelloControl<\/strong> {\n\n\n}<\/code><\/pre>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Spring Boot\u4e2d\u6211\u4eec\u4e00\u822c\u628a\u7f51\u9875\u5b58\u653e\u5728 <strong>src\/main\/resources\/static<\/strong> \u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>@Controller<\/strong>\n<strong>public<\/strong> <strong>class<\/strong> <strong>HelloControl<\/strong> {\n\n    <strong>public<\/strong> String <strong>say<\/strong>(){\n        <strong>return<\/strong> \"hello.html\";\n    }\n\n}<\/code><\/pre>\n\n\n\n<p>\u7531\u4e8eresouces\u5c5e\u4e8eclasspath\u7c7b\u578b\u6587\u4ef6\uff0cSpring\u4f1a\u81ea\u52a8\u52a0\u8f7d\uff0c\u6240\u4ee5\u8fd4\u56de\u503c\u53ea\u9700\u8981\u5199hello.html\u5c31\u53ef\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\u5982\u679c\u5b58\u653e\u5728 <strong>\/resources\/static\/html<\/strong> \u4e0b\uff0c\u5219\u9700\u8981 <code> <strong>return<\/strong> \"html\/hello.html\"; <\/code><\/p>\n\n\n\n<p>\u540c\u65f6Spring MVC\u7b80\u5316\u4e86\u8def\u7531\u914d\u7f6e\u53ea\u9700\u8981\u6dfb\u52a0 @RequestMapping \u5c31\u53ef\u4ee5\u5b8c\u6210\u8def\u7531<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>@RequestMapping(\"\/hello\")<\/strong>\n    <strong>public<\/strong> String <strong>say<\/strong>(){\n        <strong>return<\/strong> \"html\/hello.html\";\n    }<\/code><\/pre>\n\n\n\n<p>\u540c\u65f6\uff0c\u5982\u679c\u60f3\u8981\u5982\u4e0b\u5e26\u53c2\u6570\u7684\u5730\u5740\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">https:\/\/\u57df\u540d\/songlist?id=xxxx<\/code><\/pre>\n\n\n\n<p>\u5e76\u4f7f\u5176\u53c2\u6570\u4f20\u5165\u53ef\u4ee5\u5728\u65b9\u6cd5\u53c2\u6570\u4e0a\u6dfb\u52a0 <strong>@RequestParam(&#8220;xx&#8221;) <\/strong>\u6765\u8868\u793a\u53c2\u6570\u4f20\u5165 \u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"> <strong>@RequestMapping(\"\/songlist\")<\/strong>\n    <strong>public<\/strong> String <strong>index<\/strong>( <strong>@RequestParam(\"id\")<\/strong> String id){\n        <strong>return<\/strong> \"html\/songList.html\";\n    }<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u6dfb\u52a0<strong>@RequestBody<\/strong>\u8868\u793a\u524d\u7aef\u4f20\u9012\u6765\u7684JSON\u6570\u636e<\/p><\/blockquote>\n\n\n\n<p>\u5982\u679c\u8981\u8f93\u51faJSON\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u6dfb\u52a0 <strong>@ResponseBody<\/strong> \u6ce8\u89e3\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\"><strong>@GetMapping(\"\/api\/user\")<\/strong>\n  <strong>@ResponseBody<\/strong>\n  <strong>public<\/strong> User <strong>getUser<\/strong>(<strong>@RequestParam(\"id\")<\/strong> String id) {\n    <strong>return<\/strong> users.get(id);\n  }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u4e0a\u6240\u4ee5\u7684\u7f51\u9875\u52a0\u8f7d\u90fd\u662f\u8fd9\u6837\u7684\u4e00\u4e2a\u8fc7\u7a0b\u3002\u5728Spring Boot\u65b9\u6848\u91cc\uff0c\u4e00\u4e2a\u7f51\u9875\u8bf7\u6c42\u5230\u4e86\u670d\u52a1\u5668\u540e\uff0c\u9996\u5148\u6211\u4eec\u8fdb\u5165 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-1047","post","type-post","status-publish","format-standard","hentry","category-6"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1047"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1047"}],"version-history":[{"count":2,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1047\/revisions"}],"predecessor-version":[{"id":1049,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1047\/revisions\/1049"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}