get请求传值中文乱码

解决方法:
  1. 方法一:修改tomcat的server.xml文件: 添加URIEncoding="utf-8"  即可解决get请求乱码
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>

  2. 方法二(比较麻烦):在客户端使用 URLEncoder.encode(“中文”,”UTF-8”)对中文参数进行编码,在服务器端需要进行解码this.setName(java.net.URLDecoder.decode(name, “UTF-8”)); 

        方法2的2中情况

        encodeURIComponent()和encodeURI()效果一样

        情况一 ajax的get请求:

            编码一次就好了
            siteName : encodeURIComponent(siteName)

        情况二 location.href的get请求 :

            编码两次

            (window.可以省略)location.href = "exportMappingList?siteName="+encodeURI(encodeURI(siteName));

       情况一和情况二:      

       controller都只解码一次,使用URLDecoder.decode(customerName,"UTF-8");

 

解决方法:
  1. 方法一:修改tomcat的server.xml文件: 添加URIEncoding="utf-8"  即可解决get请求乱码
    <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="utf-8"/>

  2. 方法二(比较麻烦):在客户端使用 URLEncoder.encode(“中文”,”UTF-8”)对中文参数进行编码,在服务器端需要进行解码this.setName(java.net.URLDecoder.decode(name, “UTF-8”)); 

猜你喜欢

转载自blog.csdn.net/qq_29883183/article/details/79621337