\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u76f4\u63a5\u8c03\u7528\u91cd\u5b9a\u5411\u7684\u65b9\u6cd5\uff1a<\/p>\n
\r\n@RequestMapping(\"\/test1\")\r\n@ResponseBody\r\npublic void test1(HttpServletResponse resp){\r\n resp.sendRedirect(\"\/aa\/index\");\r\n}\r\n<\/pre>\n\u5f53\u7136\uff0c\u91cd\u5b9a\u5411\u65e0\u8bba\u4f60\u600e\u4e48\u5199\uff0c\u90fd\u662f Servlet\/Jsp \u4e2d\u7684\u77e5\u8bc6\u70b9\uff0c\u4e0a\u9762\u4e24\u79cd\u5199\u6cd5\u90fd\u76f8\u5f53\u4e8e\u662f\u91cd\u56de\u8fdc\u53e4\u65f6\u4ee3\u3002<\/p>\n
2.3 \u670d\u52a1\u7aef\u8df3\u8f6c
\n\u65e2\u7136\u53ef\u4ee5\u91cd\u5b9a\u5411\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u670d\u52a1\u7aef\u8df3\u8f6c\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n
\r\n@GetMapping(\"\/test5\")\r\npublic void test5(HttpServletRequest req,HttpServletResponse resp) {\r\n req.getRequestDispatcher(\"\/WEB-INF\/jsp\/index.jsp\").forward(req,resp);\r\n}\r\n<\/pre>\n2.4 \u8fd4\u56de\u5b57\u7b26\u4e32
\n\u5f53\u7136\u4e5f\u53ef\u4ee5\u5229\u7528 HttpServletResponse \u8fd4\u56de\u5176\u4ed6\u5b57\u7b26\u4e32\u6570\u636e\uff0c\u5305\u62ec\u4f46\u4e0d\u5c40\u9650\u4e8e JSON\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n
\r\n@RequestMapping(\"\/test2\")\r\n@ResponseBody\r\npublic void test2(HttpServletResponse resp) throws IOException {\r\n resp.setContentType(\"application\/json;charset=utf-8\");\r\n PrintWriter out = resp.getWriter();\r\n List books = new ArrayList<>();\r\n Book b1 = new Book();\r\n b1.setId(1);\r\n b1.setName(\"\u4e09\u56fd\u6f14\u4e49\");\r\n b1.setAuthor(\"\u7f57\u8d2f\u4e2d\");\r\n books.add(b1);\r\n Book b2 = new Book();\r\n b2.setId(2);\r\n b2.setName(\"\u7ea2\u697c\u68a6\");\r\n b2.setAuthor(\"\u66f9\u96ea\u82b9\");\r\n books.add(b2);\r\n String s = new Gson().toJson(books);\r\n out.write(s);\r\n out.flush();\r\n out.close();\r\n}\r\n<\/book><\/pre>\n\u8fd9\u662f\u8fd4\u56de\u503c\u4e3a void \u65f6\u5019\u7684\u60c5\u51b5\uff0c\u65b9\u6cd5\u8fd4\u56de\u503c\u4e3a void \uff0c\u4e0d\u4e00\u5b9a\u5c31\u771f\u7684\u4e0d\u8fd4\u56de\u4e86\uff0c\u53ef\u80fd\u8fd8\u6709\u5176\u4ed6\u7684\u65b9\u5f0f\u7ed9\u524d\u7aef\u6570\u636e\u3002<\/p>\n
3. String<\/strong><\/div>\n\u5f53 SpringMVC \u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4e3a String \u7c7b\u578b\u65f6\uff0c\u4e5f\u6709\u51e0\u79cd\u4e0d\u540c\u60c5\u51b5\u3002<\/p>\n
3.1 \u903b\u8f91\u89c6\u56fe\u540d<\/strong><\/span><\/div>\n\u8fd4\u56de String \u6700\u5e38\u89c1\u7684\u662f\u903b\u8f91\u89c6\u56fe\u540d\uff0c\u8fd9\u79cd\u65f6\u5019\u4e00\u822c\u5229\u7528\u9ed8\u8ba4\u7684\u53c2\u6570 Model \u6765\u4f20\u9012\u6570\u636e\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837 \uff1a<\/p>\n
\r\n@RequestMapping(\"\/hello\")\r\npublic String aaa(Model model) {\r\n model.addAttribute(\"username\", \"\u5f20\u4e09\");\r\n return \"hello\";\r\n}\r\n<\/pre>\n\u6b64\u65f6\u8fd4\u56de\u7684 hello \u5c31\u662f\u903b\u8f91\u89c6\u56fe\u540d\uff0c\u9700\u8981\u643a\u5e26\u7684\u6570\u636e\u653e\u5728 model \u4e2d\u3002<\/p>\n
3.2 \u91cd\u5b9a\u5411<\/strong><\/span><\/div>\n\u4e5f\u53ef\u4ee5\u91cd\u5b9a\u5411\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u5728 SpringMVC \u4e2d\u6709\u91cd\u5b9a\u5411\u7684\u9700\u6c42\uff0c\u4e00\u822c\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\uff1a<\/p>\n
\r\n@RequestMapping(\"\/test4\")\r\npublic String test4() {\r\n return \"redirect:\/aa\/index\";\r\n}\r\n<\/pre>\n3.3 forward \u8f6c\u53d1<\/strong><\/span><\/div>\n\u4e5f\u53ef\u4ee5 forward \u8f6c\u53d1\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u5982\u679c\u5728 SpringMVC \u4e2d\u6709 forward \u8f6c\u53d1\u7684\u9700\u6c42\uff0c\u4e00\u822c\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\uff1a<\/p>\n
\r\n@RequestMapping(\"\/test3\")\r\npublic String test3() {\r\n return \"forward:\/WEB-INF\/jsp\/order.jsp\";\r\n}\r\n<\/pre>\n3.4 \u771f\u7684\u662f String<\/strong><\/span><\/div>\n\u5f53\u7136\uff0c\u4e5f\u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u5c31\u662f\u4f60\u771f\u7684\u60f3\u8fd4\u56de\u4e00\u4e2a String \uff0c\u6b64\u65f6\uff0c\u53ea\u8981\u5728\u65b9\u6cd5\u4e0a\u52a0\u4e0a @ResponseBody \u6ce8\u89e3\u5373\u53ef\uff0c\u6216\u8005 Controller \u4e0a\u672c\u8eab\u6dfb\u52a0\u7684\u662f\u7ec4\u5408\u6ce8\u89e3 @RestController\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n
\r\n@RestController\r\npublic class HelloController {\r\n @GetMapping(\"\/hello\")\r\n public String hello() {\r\n return \"hello provider!\";\r\n }\r\n}\r\n<\/pre>\n\u4e5f\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837 \uff1a<\/p>\n
\r\n@Controller\r\npublic class HelloController {\r\n @GetMapping(\"\/hello\")\r\n @ResponseBody\r\n public String hello() {\r\n return \"hello provider!\";\r\n }\r\n}\r\n<\/pre>\n\u8fd9\u662f\u8fd4\u56de\u503c\u4e3a String \u7684\u51e0\u79cd\u60c5\u51b5\u3002<\/p>\n
4. JSON<\/strong><\/div>\n\u8fd4\u56de JSON \u7b97\u662f\u6700\u6700\u5e38\u89c1\u7684\u4e86\uff0c\u73b0\u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u8d8b\u52bf\u4e0b\uff0c\u5927\u90e8\u5206\u540e\u7aef\u53ea\u9700\u8981\u8fd4\u56de JSON \u5373\u53ef\uff0c\u90a3\u4e48\u5e38\u89c1\u7684 List \u96c6\u5408\u3001Map\uff0c\u5b9e\u4f53\u7c7b\u7b49\u90fd\u53ef\u4ee5\u8fd4\u56de\uff0c\u8fd9\u4e9b\u6570\u636e\u7531 HttpMessageConverter \u81ea\u52a8\u8f6c\u4e3a JSON \uff0c\u5982\u679c\u5927\u5bb6\u7528\u4e86 Jackson \u6216\u8005 Gson \uff0c\u4e0d\u9700\u8981\u989d\u5916\u914d\u7f6e\u5c31\u53ef\u4ee5\u81ea\u52a8\u8fd4\u56de JSON \u4e86\uff0c\u56e0\u4e3a\u6846\u67b6\u5e2e\u6211\u4eec\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684 HttpMessageConverter \uff0c\u5982\u679c\u5927\u5bb6\u4f7f\u7528\u4e86 Alibaba \u7684 Fastjson \u7684\u8bdd\uff0c\u5219\u9700\u8981\u81ea\u5df1\u624b\u52a8\u63d0\u4f9b\u4e00\u4e2a\u76f8\u5e94\u7684 HttpMessageConverter \u7684\u5b9e\u4f8b\uff0c\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n
\r\n@GetMapping(\"\/user\")\r\n@ResponseBody\r\npublic User getUser() {\r\n User user = new User();\r\n List favorites = new ArrayList<>();\r\n favorites.add(\"\u8db3\u7403\");\r\n favorites.add(\"\u7bee\u7403\");\r\n user.setFavorites(favorites);\r\n user.setUsername(\"zhagnsan\");\r\n user.setPassword(\"123\");\r\n return user;\r\n}\r\n@GetMapping(\"\/users\")\r\n@ResponseBody\r\npublic List getALlUser() {\r\n List<\/user> users = new ArrayList<>();\r\n for (int i = 0; i < 10; i++) {\r\n User e = new User();\r\n e.setUsername(\"zhangsan:\" + i);\r\n e.setPassword(\"pwd:\" + i);\r\n users.add(e);\r\n }\r\n return users;\r\n}\r\n<\/user><\/string><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4ee5\u524d\u524d\u540e\u7aef\u4e0d\u5206\u7684\u60c5\u51b5\u4e0b\uff0cModelAndView \u5e94\u8be5\u662f\u6700\u6700\u5e38\u89c1\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u4e86\uff0c\u73b0\u5728\u524d\u540e\u7aef\u5206\u79bb\u540e\uff0c\u540e\u7aef\u90fd\u662f\u4ee5\u8fd4 […]<\/p>\n","protected":false},"author":1903,"featured_media":218242,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=218232"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218232\/revisions"}],"predecessor-version":[{"id":218244,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218232\/revisions\/218244"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/218242"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=218232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=218232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=218232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}