{"id":273,"date":"2021-01-10T22:53:00","date_gmt":"2021-01-10T14:53:00","guid":{"rendered":"https:\/\/cn1.club\/?p=273"},"modified":"2025-03-15T22:47:10","modified_gmt":"2025-03-15T14:47:10","slug":"springmvc%e4%b8%adget%e6%96%b9%e5%bc%8f%e4%bc%a0%e9%80%92%e4%b8%ad%e6%96%87%e5%8f%82%e6%95%b0%e4%b9%b1%e7%a0%81%e7%9a%84%e8%a7%a3%e5%86%b3%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/cn1.club\/?p=273","title":{"rendered":"SpringMVC\u4e2dGET\u65b9\u5f0f\u4f20\u9012\u4e2d\u6587\u53c2\u6570\u4e71\u7801\u7684\u89e3\u51b3\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u95ee\u9898\u63cf\u8ff0:<\/p>\n\n\n\n<p>\u4f7f\u7528SpringMVC\u53c2\u6570\u4f20\u9012\u65f6, \u9047\u5230get\u8bf7\u6c42\u4e2d\u6587\u4fe1\u606f\u65f6,\u9875\u9762\u5e94\u7b54\u4f1a\u663e\u793a\u4e2d\u6587\u4e71\u7801.<\/p>\n\n\n\n<p>\u89e3\u51b3\u529e\u6cd5:<\/p>\n\n\n\n<p>\u4e00, \u6211\u4eec\u9700\u8981\u628arequest.getParameter(\u201c\u53c2\u6570\u540d\u201d)\u83b7\u53d6\u5230\u7684\u5b57\u7b26\u4e32\u5148\u7528ISO-8859-1\u7f16\u7801\u6210\u5b57\u8282\u6d41\uff0c\u7136\u540e\u518d\u5c06\u5176\u7528utf-8\u89e3\u7801\u6210\u5b57\u7b26\u6d41,\u4ee3\u7801\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >String str = new String(request.getParameter(&quot;\u53c2\u6570\u540d&quot;).getBytes(&quot;iso-8859-1&quot;), &quot;utf-8&quot;);<\/code><\/pre>\n\n\n\n<p>\u4e8c,&nbsp;\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7Tomcat\u914d\u7f6e\u6587\u4ef6\uff0c\u8bbe\u7f6eURL\u7f16\u7801\u96c6\uff08URIEncoding\uff09\u8bbe\u7f6e\u7f16\u7801.<\/p>\n\n\n\n<p>\u5728tomcat\u7684server.xml\u914d\u7f6e\u6587\u4ef6\u4e2d,\u6dfb\u52a0&nbsp;URIEncoding=\"UTF-8\"&nbsp;<\/p>\n\n\n\n<p><strong>\u5728\u8fd9\u91cc\u8981\u7279\u522b\u6ce8\u610f:\u5f53\u4f60\u7528eclipse\u8fd0\u884c\u9879\u76ee\u6587\u4ef6\u65f6,\u5982\u679c\u6539\u4e86tomcat\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684server.xml\u6587\u4ef6\u4f9d\u65e7\u4e0d\u8d77\u4f5c\u7528,\u5c31\u8981\u6539eclipse\u81ea\u8eab\u914d\u7f6e\u7684servers\u5e95\u4e0b\u7684server.xml\u6587\u4ef6.\u56e0\u4e3aeclipse\u8fd0\u884c\u9879\u76ee\u65f6\u7528\u7684\u662f\u8fd9\u4e2aserver.<\/strong><\/p>\n\n\n\n<p>\u5982\u4e0b\u56fe\u6240\u793a:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/images2015.cnblogs.com\/blog\/858807\/201607\/858807-20160701152347124-1301961471.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u6dfb\u52a0&nbsp;URIEncoding=\"UTF-8\"&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;Connector connectionTimeout=&quot;20000&quot; port=&quot;8080&quot; protocol=&quot;HTTP\/1.1&quot; redirectPort=&quot;8443&quot;\/&gt;<\/code><\/pre>\n\n\n\n<p>\u4fee\u6539\u4e3a:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;Connector URIEncoding=&quot;UTF-8&quot; connectionTimeout=&quot;20000&quot; port=&quot;8080&quot; protocol=&quot;HTTP\/1.1&quot; redirectPort=&quot;8443&quot;\/&gt; <\/code><\/pre>\n\n\n\n<p>\u95ee\u9898\u63cf\u8ff0:<\/p>\n\n\n\n<p>\u4f7f\u7528SpringMVC\u53c2\u6570\u4f20\u9012\u65f6, \u9047\u5230get\u8bf7\u6c42\u4e2d\u6587\u4fe1\u606f\u65f6,\u9875\u9762\u5e94\u7b54\u4f1a\u663e\u793a\u4e2d\u6587\u4e71\u7801.<\/p>\n\n\n\n<p>\u89e3\u51b3\u529e\u6cd5:<\/p>\n\n\n\n<p>\u4e00, \u6211\u4eec\u9700\u8981\u628arequest.getParameter(\u201c\u53c2\u6570\u540d\u201d)\u83b7\u53d6\u5230\u7684\u5b57\u7b26\u4e32\u5148\u7528ISO-8859-1\u7f16\u7801\u6210\u5b57\u8282\u6d41\uff0c\u7136\u540e\u518d\u5c06\u5176\u7528utf-8\u89e3\u7801\u6210\u5b57\u7b26\u6d41,\u4ee3\u7801\u5982\u4e0b:<\/p>\n\n\n\n<p>String str = new String(request.getParameter(\"\u53c2\u6570\u540d\").getBytes(\"iso-8859-1\"), \"utf-8\");<br>\u4e8c, \u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7Tomcat\u914d\u7f6e\u6587\u4ef6\uff0c\u8bbe\u7f6eURL\u7f16\u7801\u96c6\uff08URIEncoding\uff09\u8bbe\u7f6e\u7f16\u7801.<\/p>\n\n\n\n<p>\u5728tomcat\u7684server.xml\u914d\u7f6e\u6587\u4ef6\u4e2d,\u6dfb\u52a0 URIEncoding=\"UTF-8\"<\/p>\n\n\n\n<p>\u5728\u8fd9\u91cc\u8981\u7279\u522b\u6ce8\u610f:\u5f53\u4f60\u7528eclipse\u8fd0\u884c\u9879\u76ee\u6587\u4ef6\u65f6,\u5982\u679c\u6539\u4e86tomcat\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684server.xml\u6587\u4ef6\u4f9d\u65e7\u4e0d\u8d77\u4f5c\u7528,\u5c31\u8981\u6539eclipse\u81ea\u8eab\u914d\u7f6e\u7684servers\u5e95\u4e0b\u7684server.xml\u6587\u4ef6.\u56e0\u4e3aeclipse\u8fd0\u884c\u9879\u76ee\u65f6\u7528\u7684\u662f\u8fd9\u4e2aserver.<\/p>\n\n\n\n<p>\u5982\u4e0b\u56fe\u6240\u793a:<\/p>\n\n\n\n<p>\u6dfb\u52a0 URIEncoding=\"UTF-8\"<\/p>\n\n\n\n<p><br>\u4fee\u6539\u4e3a:<\/p>\n\n\n\n<p><br>\u5907\u6ce8:<\/p>\n\n\n\n<p>\u4e0b\u9762\u6765\u89e3\u91ca\u4e00\u4e0b\u8fd9\u4e24\u4e2a\u5c5e\u6027\u7684\u610f\u4e49:<\/p>\n\n\n\n<p>useBodyEncodingForURI\u53c2\u6570\u8868\u793a\u662f\u5426\u7528request.setCharacterEncoding\u53c2\u6570\u5bf9URL\u63d0\u4ea4\u7684\u6570\u636e\u548c\u8868\u5355\u4e2dGET\u65b9\u5f0f\u63d0\u4ea4\u7684\u6570\u636e\u8fdb\u884c\u91cd\u65b0\u7f16\u7801\uff0c\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8be5\u53c2\u6570\u4e3afalse\u3002<\/p>\n\n\n\n<p>URIEncoding\u53c2\u6570\u6307\u5b9a\u5bf9\u6240\u6709GET\u65b9\u5f0f\u8bf7\u6c42\u8fdb\u884c\u7edf\u4e00\u7684\u91cd\u65b0\u7f16\u7801\uff08\u89e3\u7801\uff09\u7684\u7f16\u7801\u3002<\/p>\n\n\n\n<p>URIEncoding\u548cuseBodyEncodingForURI\u533a\u522b\u662f\uff0c<\/p>\n\n\n\n<p>URIEncoding\u662f\u5bf9\u6240\u6709GET\u65b9\u5f0f\u7684\u8bf7\u6c42\u7684\u6570\u636e\u8fdb\u884c\u7edf\u4e00\u7684\u91cd\u65b0\u7f16\u7801\uff0c<\/p>\n\n\n\n<p>\u800cuseBodyEncodingForURI\u5219\u662f\u6839\u636e\u54cd\u5e94\u8be5\u8bf7\u6c42\u7684\u9875\u9762\u7684request.setCharacterEncoding\u53c2\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u7684\u91cd\u65b0\u7f16\u7801\uff0c\u4e0d\u540c\u7684\u9875\u9762\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u91cd\u65b0\u7f16\u7801\u7684\u7f16\u7801<\/p>\n\n\n\n<p>\u914d\u7f6euseBodyEncodingForURI=\"true\"\u540e\uff0c\u53ef\u4ee5\u89e3\u51b3\u666e\u901aget\u8bf7\u6c42\u7684\u4e2d\u6587\u4e71\u7801\u95ee\u9898\uff0c\u4f46\u662f\u5bf9\u4e8e\u901a\u8fc7ajax\u53d1\u8d77\u7684get\u8bf7\u6c42\u4e2d\u6587\u4f9d\u7136\u4f1a\u4e71\u7801\uff0c\u8bf7\u628auseBodyEncodingForURI=\"true\"\u6539\u4e3aURIEncoding=\"UTF-8\"\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0: \u4f7f\u7528SpringMVC\u53c2\u6570\u4f20\u9012\u65f6, \u9047\u5230get\u8bf7\u6c42\u4e2d\u6587\u4fe1\u606f\u65f6,\u9875\u9762\u5e94\u7b54\u4f1a\u663e\u793a\u4e2d\u6587\u4e71\u7801. \u89e3\u51b3\u529e\u6cd5: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-xuexi"],"_links":{"self":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=273"}],"version-history":[{"count":0,"href":"https:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/273\/revisions"}],"wp:attachment":[{"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}