SpringMVC中的GET请求乱码

rgvfgf 2015-10-14 08:53:46
SpringMVC中的GET怎么解决请求乱码呀?
[code=text]<filter>  
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
[/code]

过滤器为什么对POST请求起作用对GET请求没有呀
...全文
216 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
街头小贩 2015-10-19
  • 打赏
  • 举报
回复
1.字符过滤器中调用的是

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		arg0.setCharacterEncoding("utf-8");
		arg1.setCharacterEncoding("utf-8");  	
		arg2.doFilter(arg0, arg1);
	}
看 HttpServletRequest的setCharacterEncoding方法的描述 Overrides the name of the character encoding used in the body of this request. 是说请求的内容体采用指定的charset,而http get方法的内容体是空的,它的请求参数是附加上URL上,而post方法的内容体存在请求参数,内容是key=value这种键值对 2.tomcat的Connector URIEncoding属性 This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used. useBodyEncodingForURI属性 1) This setting is applied only to the query string of a request. Unlike URIEncoding it does not affect the path portion of a request URI. 2) If request character encoding is not known (is not provided by a browser and is not set by SetCharacterEncodingFilter or a similar filter using Request.setCharacterEncoding method), the default encoding is always "ISO-8859-1". The URIEncoding setting has no effect on this default.
街头小贩 2015-10-19
  • 打赏
  • 举报
回复
引用 5 楼 u012402971 的回复:
为什么用POST请求就不会出现乱码呢?
可能差错的地方: 设:页面的charset=utf-8: post: utf8---->过滤器的charset设置---->目标charset get: iso8859-1(get请求的默认charset) ---->过滤器的charset设置---->目标charset
rgvfgf 2015-10-19
  • 打赏
  • 举报
回复
为什么用POST请求就不会出现乱码呢?
rgvfgf 2015-10-15
  • 打赏
  • 举报
回复
我是用form表单的GET方法来进行对象传值的,URL上也显示中文了,在对象.toString()输出,中文乱码,但是我改成POST请求,就不会出现乱码的情况
街头小贩 2015-10-15
  • 打赏
  • 举报
回复
我测一下确实有乱码!http://stackoverflow.com/questions/25314177/spring-roo-spring-mvc-how-can-i-set-uriencoding-for-controller-method,照 此在tomcat/conf/server.xml设了一个Connector的属性:URIEncoding="UTF-8".不乱码了

  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="80" 
                protocol="org.apache.coyote.http11.Http11NioProtocol" 
                connectionTimeout="20000" 
                redirectPort="443" 
                compression="on" 
                compressionMinSize="2048"
                URIEncoding="UTF-8" 
                noCompressionUserAgents="gozilla,traviata" 
                compressableMimeType="text/html,text/xml,application/javascript,text/css,text/plain,text/json,application/json" secure="true"/>

街头小贩 2015-10-15
  • 打赏
  • 举报
回复
<form method="GET" action="/a">
<input type="text" name="nick" value="中文" />
</form>
提交后:/a?nick=中文,就乱码
街头小贩 2015-10-14
  • 打赏
  • 举报
回复
你的查询参数是中文字符吗

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧