解决Struts乱码问题 a) 配置一个Filter过滤器,加入request.setCharacterEncoding("UTF-8"); b) 写一个Servlet继承自ActionServlet在doGet,doPost方法中加入request.setCharacterEncoding("UTF-8"); i. 修改web.xml,把ActionServlet改成自己的类 c) 利用自定义的RequestProceor类中的processPreprocess,加入request.setCharacterEncoding("UTF-8");a) get/post 方式乱码解决方案: i. Tomcat默认在5以后get和post提交的解决不一样。 ii. Post---> 1. 提交页面设置contentType="text/html;charset=utf-8"; 2. 接收页面设置request.setCharacterEncoding("utf-8"),显示时也设置 contentType="text/html;charset=utf-8"; 3. Struts1默认ISO-8859-1,所以我们需要人工设置乱码解决。参见四 iii. Get 1. 默认URL地址提交的是ISO-8859-1的编码。即使做了上面的处理,也不行。 2. 修改tomcat/conf/server.xml a) <Connector port="80" protocol="HTTP/1.1" b) connectionTimeout="20000" c) redirectPort="8443" URIEncoding="UTF-8"/>
web.xml里面配置下
<filter>
<filter-name>char</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>char</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
a) 配置一个Filter过滤器,加入request.setCharacterEncoding("UTF-8");
b) 写一个Servlet继承自ActionServlet在doGet,doPost方法中加入request.setCharacterEncoding("UTF-8");
i. 修改web.xml,把ActionServlet改成自己的类
c) 利用自定义的RequestProceor类中的processPreprocess,加入request.setCharacterEncoding("UTF-8");a) get/post 方式乱码解决方案:
i. Tomcat默认在5以后get和post提交的解决不一样。
ii. Post--->
1. 提交页面设置contentType="text/html;charset=utf-8";
2. 接收页面设置request.setCharacterEncoding("utf-8"),显示时也设置 contentType="text/html;charset=utf-8";
3. Struts1默认ISO-8859-1,所以我们需要人工设置乱码解决。参见四
iii. Get
1. 默认URL地址提交的是ISO-8859-1的编码。即使做了上面的处理,也不行。
2. 修改tomcat/conf/server.xml
a) <Connector port="80" protocol="HTTP/1.1"
b) connectionTimeout="20000"
c) redirectPort="8443" URIEncoding="UTF-8"/>
过滤器不能过滤get提交的哈
无论你怎么配都必须手动转换
package com.yourcompany.struts;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class MyActionServlet extends org.apache.struts.action.ActionServlet{
protected void process(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
/**@todo Override this org.apache.struts.action.ActionServlet method*/ request.setCharacterEncoding("UTF-8");//就加这一行一切都解决了
super.process(request, response);
} }
要是数据库存的还是乱码的话 那就是你数据库设置问题了 一般mysql 和 oracle数据库容易有乱码问题
所有项目的编码方式用UTF-8就OK了 数据库也用UTF-8
上周还有人问这个问题还没有结贴呢,我帮你找到了帖子:
http://topic.csdn.net/u/20091224/17/f068fe17-bba6-4a2d-8df9-d44407d6d54f.html 下面的这些都讨论得很详细了:
http://topic.csdn.net/u/20080629/00/2f669f44-6e30-4e2e-9cce-08889dba2ae2.html 关于bom头的http://topic.csdn.net/u/20081009/09/e899898c-591f-4985-ae88-5972475708fb.html
字符,字节和编码 http://www.regexlab.com/zh/encoding.htm
gbk与utf-8的区别 http://hi.baidu.com/%C0%B6%B3%BD/blog/item/a0074cae0b9832c67cd92ae7.html
三只小熊http://topic.csdn.net/u/20080623/16/e0f44f00-eaf9-4d38-b325-a3cc443f2ec9.html