目前对遇到jsp中文及其乱码问题做个总结,方便自己也方便他人. 主要是5大配置点:
1 struts2配置 2 数据库 3 页面 4 jdbc连接 5 tomcat 6 Js传值给Action问题
以下解决方法不分顺序,大家按照自己的配置对比.
1、struts2配置
在struts.xml里加上<constant name="struts.i18n.encoding" value="UTF-8" />
web.xml中增加过滤器
<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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、数据库
2.1 mysql的表的编码方式,其DDL应该为: ENGINE=InnoDB DEFAULT CHARSET=utf8; 可以使用show create table tbname查看.
2.2 安装mysql数据库时就应该以utf8编码默认安装查看mysql数据库默认编码方式可以在其目录下my.ini文件中找到mysql和mysqld下default-character-set值为utf8或者在mysql数据库命令行下以"/s;"查看.
msyql数据库安装时候需要默认编码方式或者使用MySQL Server Instance Config Wizard 配置,
在设置Configure the MySQL Server 5.0 server instance那里的时候选择Manual Selected Default Character Set / Collation 为 UTF8 而不是默认的 LATIN1
my.ini文件内容为
[mysql] default-character-set=utf8 # SERVER SECTION # ---------------------------------------------------------------------- # # The following options will be read by the MySQL Server. Make sure that # you have installed the server correctly (see above) so it reads this # file. # [mysqld] # The TCP/IP Port the MySQL Server will listen on port=3306 #Path to installation directory. All paths are usually resolved relative to this. basedir="E:/MySQL/MySQL Server 5.0/" #Path to the database root datadir="E:/MySQL/MySQL Server 5.0/Data/" # The default character set that will be used when a new schema or table is # created and no character set is defined default-character-set=utf8 ...........
3、页面
每个页面上加上<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> 这样在jsp页面里,点右键,查看编码方式则为UTF-8.
4、jdbc连接
设置characterencoding为UTF-8 如jdbc.mysql.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF8
5、tomcat
为了保证get/post数据都采用相同的UTF8编码,我们在server.xml中进行了如下设置: <Connector port="8080" protocol="HTTP/1.1"
其中,当当使用IIS作为webserver转发servlet/jsp请求时IIS是通过AJP协议,把请求转发到Tomcat监听的8009端口,所以需要配置端口8009的URIEncoding为UTF-8.
6 Js传值给Action
1、在通过js传值给Action前,运用encodeURIComponent() 函数先对要传的值进行编码。
2、然后在Action里获取编码并把编码转化成中文编码格式。
3、具体运用如下:在JavaScript里编码:
var value= encodeURIComponent(strValue);//对strValue进行编码
var url = "login!test.action?value="+value;//将value值附带在URL并传给loginAction中的test方法处理
sendRequest(url, test_callback);//发送处理请求 .... 在Action里获取编码并把转化成中文:
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);//定义request对象
String value = new String(request.getParameter("value").getBytes("ISO-8859-1"),"UTF-8");//获取编码并把转化成中文
4、关于JavaScript encodeURIComponent() 函数定义和用法 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。语法 encodeURIComponent(URIstring) 参数URIstring:必需。一个字符串,含有URI组件或其他要编码的文本。返回值 URIstring的副本,其中的某些字符将被十六进制的转义序列进行替换。说明该方法不会对ASCII字母和数字进行编码,也不会对这些ASCII标点符号进行编码: - _ . ! ~ * ' ( ) 。其他字符(比如 :;/?:@&=+$,# 这些用于分隔URI组件的标点符号),都是由一个或多个十六进制的转义序列替换的。如使用encodeURIComponent()对URI进行编码:document.write(encodeURIComponent("http://hi.baidu.com/ouwennuan")) 输出结果为:http://hi.baidu.com/ouwennuan 提示和注释提示:请注意encodeURIComponent()函数与encodeURI()函数的区别之处,前者假定它的参数是URI 的一部分(比如协议、主机名、路径或查询字符串)。因此encodeURIComponent()函数将转义用于分隔URI各个部分的标点符号。