{"id":683,"date":"2023-02-26T14:40:47","date_gmt":"2023-02-26T06:40:47","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=683"},"modified":"2023-04-29T17:58:22","modified_gmt":"2023-04-29T09:58:22","slug":"several-ways-to-obtain-parameters-in-spring-boot","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/several-ways-to-obtain-parameters-in-spring-boot\/","title":{"rendered":"Spring Boot\u83b7\u53d6\u53c2\u6570\u7684\u51e0\u79cd\u65b9\u5f0f"},"content":{"rendered":"<h3>\u76f4\u63a5\u628a\u8868\u5355\u7684\u53c2\u6570\u5199\u5728Controller\u76f8\u5e94\u7684\u65b9\u6cd5\u7684\u5f62\u53c2\u4e2d<\/h3>\n<p>\u76f4\u63a5\u628a\u8868\u5355\u7684\u53c2\u6570\u5199\u5728Controller\u76f8\u5e94\u7684\u65b9\u6cd5\u7684\u5f62\u53c2\u4e2d\uff0c\u9002\u7528\u4e8eget\u65b9\u5f0f\u63d0\u4ea4\uff0c\u4e0d\u9002\u7528\u4e8epost\u65b9\u5f0f\u63d0\u4ea4<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/**\n * \u76f4\u63a5\u628a\u8868\u5355\u7684\u53c2\u6570\u5199\u5728Controller\u76f8\u5e94\u7684\u65b9\u6cd5\u7684\u5f62\u53c2\u4e2d\n * @param username\n * @param password\n * @return\n *\/\n@RequestMapping(&quot;\/addUser1&quot;)\npublic String addUser1(String username, String password) {\n    System.out.println(&quot;username is: &quot; + username);\n    System.out.println(&quot;password is: &quot; + password);\n    return &quot;demo\/index&quot;;\n}<\/code><\/pre>\n<p>Url\u5f62\u5f0f\uff1a<code>http:\/\/localhost\/AppBlog\/demo\/addUser1?username=appblog&amp;password=888888<\/code>\uff0c\u63d0\u4ea4\u7684\u53c2\u6570\u9700\u8981\u548cController\u65b9\u6cd5\u4e2d\u7684\u5165\u53c2\u540d\u79f0\u4e00\u81f4<\/p>\n<h3>\u901a\u8fc7HttpServletRequest\u63a5\u6536<\/h3>\n<p>\u901a\u8fc7<code>HttpServletRequest<\/code>\u63a5\u6536\uff0cpost\u65b9\u5f0f\u548cget\u65b9\u5f0f\u90fd\u53ef\u4ee5<\/p>\n<pre><code class=\"language-java\">\/**\n * \u901a\u8fc7HttpServletRequest\u63a5\u6536\n * @param request\n * @return\n *\/\n@RequestMapping(&quot;\/addUser2&quot;)\npublic String addUser2(HttpServletRequest request) {\n    String username=request.getParameter(&quot;username&quot;);\n    String password=request.getParameter(&quot;password&quot;);\n    System.out.println(&quot;username is: &quot; + username);\n    System.out.println(&quot;password is: &quot; + password);\n    return &quot;demo\/index&quot;;\n}<\/code><\/pre>\n<h3>\u901a\u8fc7\u4e00\u4e2abean\u6765\u63a5\u6536<\/h3>\n<p>\u901a\u8fc7\u4e00\u4e2abean\u6765\u63a5\u6536\uff0cpost\u65b9\u5f0f\u548cget\u65b9\u5f0f\u90fd\u53ef\u4ee5<\/p>\n<pre><code class=\"language-java\">\/**\n * \u901a\u8fc7\u4e00\u4e2abean\u6765\u63a5\u6536\n * @param user\n * @return\n *\/\n@RequestMapping(&quot;\/addUser3&quot;)\npublic String addUser3(UserModel user) {\n    System.out.println(&quot;username is: &quot; + user.getUsername());\n    System.out.println(&quot;password is: &quot; + user.getPassword());\n    return &quot;demo\/index&quot;;\n}<\/code><\/pre>\n<h3>\u901a\u8fc7<code>@PathVariable<\/code>\u83b7\u53d6\u8def\u5f84\u4e2d\u7684\u53c2\u6570<\/h3>\n<pre><code class=\"language-java\">\/**\n * \u901a\u8fc7@PathVariable\u83b7\u53d6\u8def\u5f84\u4e2d\u7684\u53c2\u6570\n * @param username\n * @param password\n * @return\n *\/\n@RequestMapping(value=&quot;\/addUser4\/{username}\/{password}&quot;, method=RequestMethod.GET)\npublic String addUser4(@PathVariable String username, @PathVariable String password) {\n    System.out.println(&quot;username is: &quot; + username);\n    System.out.println(&quot;password is: &quot; + password);\n    return &quot;demo\/index&quot;;\n}<\/code><\/pre>\n<p>\u8bbf\u95ee\uff1a<code>http:\/\/localhost\/AppBlog\/demo\/addUser4\/appblog\/888888<\/code>\u8def\u5f84\u65f6\uff0c\u5219\u81ea\u52a8\u5c06URL\u4e2d\u6a21\u677f\u53d8\u91cf<code>{username}<\/code>\u548c<code>{password}<\/code>\u7ed1\u5b9a\u5230\u901a\u8fc7<code>@PathVariable<\/code>\u6ce8\u89e3\u7684\u540c\u540d\u53c2\u6570\u4e0a\uff0c\u5373\u5165\u53c2\u540eusername=appblog\u3001password=888888<\/p>\n<h3>\u4f7f\u7528<code>@ModelAttribute<\/code>\u6ce8\u89e3\u83b7\u53d6POST\u8bf7\u6c42\u7684FORM\u8868\u5355\u6570\u636e<\/h3>\n<pre><code class=\"language-html\">&lt;!--jsp\u9875\u9762--&gt;\n&lt;form action =&quot;&lt;%=request.getContextPath()%&gt;\/demo\/addUser5&quot; method=&quot;post&quot;&gt; \n     \u7528\u6237\u540d:&nbsp;&lt;input type=&quot;text&quot; name=&quot;username&quot;\/&gt;&lt;br\/&gt;\n     \u5bc6&nbsp;&nbsp;\u7801:&nbsp;&lt;input type=&quot;password&quot; name=&quot;password&quot;\/&gt;&lt;br\/&gt;\n     &lt;input type=&quot;submit&quot; value=&quot;\u63d0\u4ea4&quot;\/&gt; \n     &lt;input type=&quot;reset&quot; value=&quot;\u91cd\u7f6e&quot;\/&gt; \n&lt;\/form&gt; <\/code><\/pre>\n<pre><code class=\"language-java\">\/**\n * \u4f7f\u7528@ModelAttribute\u6ce8\u89e3\u83b7\u53d6POST\u8bf7\u6c42\u7684FORM\u8868\u5355\u6570\u636e\n * @param user\n * @return\n *\/\n@RequestMapping(value=&quot;\/addUser5&quot;, method=RequestMethod.POST)\npublic String addUser5(@ModelAttribute(&quot;user&quot;) UserModel user) {\n    System.out.println(&quot;username is: &quot; + user.getUsername());\n    System.out.println(&quot;password is: &quot; + user.getPassword());\n    return &quot;demo\/index&quot;;\n}<\/code><\/pre>\n<h3>\u7528\u6ce8\u89e3<code>@RequestParam<\/code>\u7ed1\u5b9a\u8bf7\u6c42\u53c2\u6570\u5230\u65b9\u6cd5\u5165\u53c2<\/h3>\n<p>\u5f53\u8bf7\u6c42\u53c2\u6570username\u4e0d\u5b58\u5728\u65f6\u4f1a\u6709\u5f02\u5e38\u53d1\u751f\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5c5e\u6027<code>required=false<\/code>\u89e3\u51b3<\/p>\n<p>\u4f8b\u5982: <code>@RequestParam(value=&quot;username&quot;, required=false)<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76f4\u63a5\u628a\u8868\u5355\u7684\u53c2\u6570\u5199\u5728Controller\u76f8\u5e94\u7684\u65b9\u6cd5\u7684\u5f62\u53c2\u4e2d \u76f4\u63a5\u628a\u8868\u5355\u7684\u53c2\u6570\u5199\u5728Controller\u76f8\u5e94\u7684\u65b9\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-683","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=683"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/683\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}