{"id":24549,"date":"2024-11-21T08:04:56","date_gmt":"2024-11-21T00:04:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24549\/"},"modified":"2024-11-21T08:04:56","modified_gmt":"2024-11-21T00:04:56","slug":"%e4%bd%bf%e7%94%a8webman%e8%bf%9b%e8%a1%8c%e5%8d%95%e9%a1%b5%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24549\/","title":{"rendered":"\u4f7f\u7528Webman\u8fdb\u884c\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169305113363113.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528Webman\u8fdb\u884c\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" alt=\"\u4f7f\u7528Webman\u8fdb\u884c\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\u63d2\u56fe\" \/><\/p>\n<p>\u4f7f\u7528Webman\u8fdb\u884c\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5<\/p>\n<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u901f\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5e94\u7528\u7a0b\u5e8f\u90fd\u4ee5\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\uff08Single-page Application\uff0cSPA\uff09\u7684\u5f62\u5f0f\u5448\u73b0\u3002SPA\u7684\u7279\u70b9\u662f\u5728\u52a0\u8f7d\u521d\u6b21\u8bbf\u95ee\u65f6\uff0c\u53ea\u4f1a\u52a0\u8f7d\u4e00\u6b21HTML\u3001CSS\u548c\u811a\u672c\u6587\u4ef6\uff0c\u540e\u7eed\u7684\u9875\u9762\u5207\u6362\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u6570\u636e\u5e76\u66f4\u65b0\u9875\u9762\u5185\u5bb9\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u6d41\u7545\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>Webman\u662f\u4e00\u4e2a\u57fa\u4e8eJava\u7684\u5f00\u53d1\u6846\u67b6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72SPA\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4f7f\u7528Webman\u8fdb\u884cSPA\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u9645\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<ol>\n<li>\u6784\u5efa\u9879\u76ee<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Webman\u9879\u76ee\u3002\u53ef\u4ee5\u4f7f\u7528Webman\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u5feb\u901f\u521d\u59cb\u5316\u4e00\u4e2a\u9879\u76ee\uff1a<\/p>\n<pre>webman init &lt;projectname&gt;&lt;\/projectname&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u57fa\u672c\u6587\u4ef6\u7ed3\u6784\u548c\u5fc5\u8981\u4f9d\u8d56\u7684\u9879\u76ee\u3002<\/p>\n<ol>\n<li>\u8def\u7531\u914d\u7f6e<\/li>\n<\/ol>\n<p>\u5728SPA\u4e2d\uff0c\u8def\u7531\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5b83\u5b9a\u4e49\u4e86\u4e0d\u540cURL\u4e0e\u9875\u9762\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\u3002\u5728Webman\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u9879\u76ee\u7684src\/main\/java\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aroutes\u5305\uff0c\u5e76\u5728\u8be5\u5305\u4e0b\u521b\u5efa\u4e00\u4e2aRoutes.java\u6587\u4ef6\u6765\u914d\u7f6e\u8def\u7531\u3002<\/p>\n<pre>package routes;\n\nimport io.github.webman.core.annotation.Controller;\nimport io.github.webman.core.annotation.GetMapping;\nimport io.github.webman.core.annotation.Route;\nimport io.github.webman.core.router.RouterBuilder;\n\n@Controller\npublic class Routes {\n\n    @GetMapping(\"\/\")\n    public void index() {\n        \/\/ \u8fd4\u56de\u4e3b\u9875\n        RouterBuilder.render(\"index.html\");\n    }\n    \n    @GetMapping(\"\/about\")\n    public void about() {\n        \/\/ \u8fd4\u56de\u5173\u4e8e\u9875\u9762\n        RouterBuilder.render(\"about.html\");\n    }\n    \n    \/\/ \u66f4\u591a\u8def\u7531\u914d\u7f6e...\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u8def\u7531&#8221;\/&#8221;\u548c&#8221;\/about&#8221;\uff0c\u5206\u522b\u5bf9\u5e94\u4e3b\u9875\u548c\u5173\u4e8e\u9875\u9762\u3002\u5728\u6bcf\u4e2a\u8def\u7531\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528RouterBuilder.render()\u65b9\u6cd5\u6765\u8fd4\u56de\u8981\u6e32\u67d3\u7684HTML\u9875\u9762\u3002<\/p>\n<ol>\n<li>\u9875\u9762\u6a21\u677f\u548c\u7ec4\u4ef6<\/li>\n<\/ol>\n<p>SPA\u4e2d\u7684\u9875\u9762\u901a\u5e38\u7531\u591a\u4e2a\u7ec4\u4ef6\u7ec4\u5408\u800c\u6210\u3002\u5728Webman\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u6765\u521b\u5efa\u9875\u9762\u6a21\u677f\u5e76\u6e32\u67d3\u7ec4\u4ef6\u3002<\/p>\n<p>\u5728\u9879\u76ee\u7684src\/main\/resources\/templates\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aindex.html\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Thymeleaf\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3\u7ec4\u4ef6\uff1a<\/p>\n<pre>\n\n\n    &lt;meta charset=\"UTF-8\"&gt;&lt;title&gt;SPA Example&lt;\/title&gt;&lt;header&gt;&lt;!-- \u6e32\u67d3\u9876\u90e8\u5bfc\u822a\u680f\u7ec4\u4ef6 --&gt;&lt;div th:replace=\"components\/header\"&gt;&lt;\/div&gt;\n    &lt;\/header&gt;&lt;main&gt;&lt;!-- \u6e32\u67d3\u9875\u9762\u4e3b\u5185\u5bb9\u7ec4\u4ef6 --&gt;&lt;div th:replace=\"components\/content\"&gt;&lt;\/div&gt;\n    &lt;\/main&gt;&lt;footer&gt;&lt;!-- \u6e32\u67d3\u9875\u811a\u7ec4\u4ef6 --&gt;&lt;div th:replace=\"components\/footer\"&gt;&lt;\/div&gt;\n    &lt;\/footer&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Thymeleaf\u7684th:replace\u5c5e\u6027\u6765\u5f15\u7528\u5176\u4ed6\u7ec4\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u7ec4\u4ef6\u7684\u590d\u7528\u3002<\/p>\n<ol>\n<li>\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e<\/li>\n<\/ol>\n<p>\u5728SPA\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u901a\u8fc7Ajax\u6765\u5411\u540e\u53f0\u53d1\u9001\u8bf7\u6c42\u5e76\u63a5\u6536\u6570\u636e\u3002\u5728Webman\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528webman-ajax\u5e93\u6765\u7b80\u5316\u6570\u636e\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u9879\u76ee\u7684build.gradle\u6587\u4ef6\u4e2d\u6dfb\u52a0webman-ajax\u7684\u4f9d\u8d56\uff1a<\/p>\n<pre>dependencies {\n    \/\/ ...\n    implementation 'io.github.webman:webman-ajax:1.1.0'\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u5728\u9700\u8981\u53d1\u9001\u8bf7\u6c42\u7684JavaScript\u4ee3\u7801\u4e2d\uff0c\u5f15\u5165webman-ajax\u5e76\u4f7f\u7528\u5b83\u53d1\u9001\u8bf7\u6c42\uff1a<\/p>\n<pre>import { ajax } from 'webman-ajax';\n\najax({\n    url: '\/api\/data',\n    method: 'GET',\n    success: function(response) {\n        \/\/ \u5904\u7406\u6210\u529f\u54cd\u5e94\n    },\n    error: function(error) {\n        \/\/ \u5904\u7406\u9519\u8bef\u54cd\u5e94\n    }\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ajax()\u51fd\u6570\u53d1\u9001\u4e00\u4e2aGET\u8bf7\u6c42\u5230\/api\/data\u5730\u5740\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u56de\u8c03\u51fd\u6570\u4e2d\u5904\u7406\u54cd\u5e94\u6570\u636e\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4f7f\u7528Webman\u8fdb\u884cSPA\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\u662f\uff1a\u5408\u7406\u5730\u914d\u7f6e\u8def\u7531\u3001\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u6765\u7ec4\u5408\u9875\u9762\u548c\u7ec4\u4ef6\u3001\u4f7f\u7528webman-ajax\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u5b9e\u8df5\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u9ad8\u6027\u80fd\u3001\u7528\u6237\u53cb\u597d\u7684SPA\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u6700\u4f73\u5b9e\u8df5\u5bf9\u4f7f\u7528Webman\u8fdb\u884cSPA\u5f00\u53d1\u7684\u5f00\u53d1\u4eba\u5458\u6709\u6240\u5e2e\u52a9\u3002\u5e0c\u671b\u4f60\u80fd\u5728\u5b9e\u8df5\u4e2d\u7075\u6d3b\u8fd0\u7528\uff0c\u5e76\u53d6\u5f97\u4ee4\u4eba\u6ee1\u610f\u7684\u5f00\u53d1\u6210\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Webman\u8fdb\u884c\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Webman\u8fdb\u884c\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5 \u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u901f\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5e94\u7528\u7a0b\u5e8f\u90fd\u4ee5\u5355\u9875\u5e94\u7528\u7a0b\u5e8f\uff08Single-page Application\uff0cSPA\uff09\u7684\u5f62\u5f0f\u5448\u73b0\u3002SPA\u7684\u7279\u70b9\u662f\u5728\u52a0\u8f7d\u521d\u6b21\u8bbf\u95ee\u65f6\uff0c\u53ea\u4f1a\u52a0\u8f7d\u4e00\u6b21HTML\u3001CSS\u548c\u811a\u672c\u6587\u4ef6\uff0c\u540e\u7eed\u7684\u9875\u9762\u5207\u6362\u901a\u8fc7JavaScript\u52a8\u6001\u52a0\u8f7d\u6570\u636e\u5e76\u66f4\u65b0\u9875\u9762\u5185\u5bb9\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u6d41\u7545\u7684\u7528\u6237\u4f53\u9a8c\u3002 Webman\u662f\u4e00\u4e2a\u57fa\u4e8eJava\u7684\u5f00\u53d1\u6846\u67b6\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u6784\u5efa\u548c\u90e8\u7f72SPA\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4f7f\u7528Webman\u8fdb\u884cSPA\u5f00\u53d1\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u9645\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u6784\u5efa\u9879\u76ee \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Webman\u9879\u76ee\u3002\u53ef\u4ee5\u4f7f\u7528Webman\u63d0\u4f9b\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u5feb\u901f\u521d\u59cb\u5316\u4e00\u4e2a\u9879\u76ee\uff1a webman init &lt;projectname&gt;&lt;\/projectname&gt; \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u57fa\u672c\u6587\u4ef6\u7ed3\u6784\u548c\u5fc5\u8981\u4f9d\u8d56\u7684\u9879\u76ee\u3002 \u8def\u7531\u914d\u7f6e \u5728SPA\u4e2d\uff0c\u8def\u7531\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5b83\u5b9a\u4e49\u4e86\u4e0d\u540cURL\u4e0e\u9875\u9762\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\u3002\u5728Webman\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u9879\u76ee\u7684src\/main\/java\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aroutes\u5305\uff0c\u5e76\u5728\u8be5\u5305\u4e0b\u521b\u5efa\u4e00\u4e2aRoutes.java\u6587\u4ef6\u6765\u914d\u7f6e\u8def\u7531\u3002 package routes; import io.github.webman.core.annotation.Controller; import io.github.webman.core.annotation.GetMapping; import io.github.webman.core.annotation.Route; import io.github.webman.core.router.RouterBuilder; @Controller public class Routes { @GetMapping(&#8220;\/&#8221;) public void index() { \/\/ \u8fd4\u56de\u4e3b\u9875 RouterBuilder.render(&#8220;index.html&#8221;); } @GetMapping(&#8220;\/about&#8221;) public void about() { \/\/ \u8fd4\u56de\u5173\u4e8e\u9875\u9762 RouterBuilder.render(&#8220;about.html&#8221;); } \/\/ \u66f4\u591a\u8def\u7531\u914d\u7f6e&#8230; } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u8def\u7531&#8221;\/&#8221;\u548c&#8221;\/about&#8221;\uff0c\u5206\u522b\u5bf9\u5e94\u4e3b\u9875\u548c\u5173\u4e8e\u9875\u9762\u3002\u5728\u6bcf\u4e2a\u8def\u7531\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528RouterBuilder.render()\u65b9\u6cd5\u6765\u8fd4\u56de\u8981\u6e32\u67d3\u7684HTML\u9875\u9762\u3002 \u9875\u9762\u6a21\u677f\u548c\u7ec4\u4ef6 SPA\u4e2d\u7684\u9875\u9762\u901a\u5e38\u7531\u591a\u4e2a\u7ec4\u4ef6\u7ec4\u5408\u800c\u6210\u3002\u5728Webman\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\u6765\u521b\u5efa\u9875\u9762\u6a21\u677f\u5e76\u6e32\u67d3\u7ec4\u4ef6\u3002 \u5728\u9879\u76ee\u7684src\/main\/resources\/templates\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aindex.html\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528Thymeleaf\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3\u7ec4\u4ef6\uff1a &lt;meta [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24549","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24549"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24549\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}