第一:code
@RequestMapping(value = "/query/{keyword}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
public String query(@PathVariable String keyword) {
}
第二:tomcat server.xml 中
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
注意:如果使用eclipse启动tomcat时,需要将workspace下面 的server.xml 中的Connector也添加URIEncoding="UTF-8"
参考: http://www.cnblogs.com/zhonghan/p/3339150.html
=======================================
<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <!--强制进行转换--> <param-name> forceEncoding</param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
如果不加强制转化参数,无效
参考
http://blog.csdn.net/lufeng20/article/details/7311959
===================================
今天搭建spring3 mvc框架的时候,发现居然又出现了乱码(万恶的iso编码啊),想起之前解决post提交乱
package com.mocha.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.DispatcherServlet;
public class EncodingDispatcherServlet extends DispatcherServlet {
private String encoding;
public void init(ServletConfig config) throws ServletException {
encoding = config.getInitParameter("encoding");
super.init(config);
}
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding(encoding);
super.doService(request, response);
}
}
然后将web.xml中的配置改为:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>com.mocha.servlet.EncodingDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
OK,乱码消失,现在总结一下乱码的问题,get方法乱码解决方法一般是将tomcat中conf目录下server.xml文件中加入
<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />
而post方法解决乱码是设置一个专门处理编码转换的filter或者servlet。
http://blog.csdn.net/nivana999/article/details/18557179