国际化及乱码问题
来源:互联网 发布:好的软件创意 编辑:程序博客网 时间:2024/05/23 19:17
一.JSP页面部分
1. 页面
1.1概述
由于STRUTS提供了很好的国际化的支持,因此JSP页面可以STRUTS的这一特性做到国际化。
STRUTS允许将不同语言的文字写在多个资源文件中,每个资源对应一种语言,而在JSP页面上则通过STRUTS
1.2资源文件
实现国际化,首先需要准备好资源文件,需要支持的语言各对应一个。资源文件一定要用支持多国语言文字的文本编辑器(如WORD,一定不能用NOTEPAD、UE等)编写,其格式如下:
# Project international
index.title=XX信息系统
index.copyright=XXX有限公司 版权所有
其中以#开头的行是注释,其它的行都是KEY=VALUE的形式,定义了一个KEY对应当前语言下的文字。保存资源文件时,需要在文件类型选项中选择纯文本类型,
这样保存的资源文件还不能直接被STRUTS使用,JDK提供了一个工具native2ascii对编写的资源文件进行转换,该
native2ascii -encoding gb2312 ApplicationResources.properties ApplicationResources_zh_CN.properties
其中-encoding参数指明编写的资源文件的编码格式,这里必须与编辑时选择保存的编码格式一致,后面两个参数分别是需要转换的资源文件名和转换后保存的资源文件名。
转换后的资源文件名都必须以一个特定的前辍开头,该前辍写在struts-config.xml配置中,后辍是对应语言的简称,如
英文(美国) ApplicationResources _en_US.properties
中文(简体)ApplicationResources _zh_CN.properties
中文(繁体)ApplicationResources _tw.properties
泰语 ApplicationResources _th.properties
资源文件的存放目录可以是/WEB-INF/classes/下的任意目录,为了便于管理 ,建议在/WEB-INF/ classes/下建立目录resources,将所有的资源文件都放在该目录下。STRUTS通过配置文件struts-config.xml配置资源文件的所在位置,如:
其中目录以包的方式表示。
1.3 JSP页面显示
为了支持国际化,JSP页面的编码方式需要设成UTF-8,即在页面的头部加入以下几行:
<%@ page contentType="text/html; charset=UTF-8" %>
……(STRUTS及其它标签库引用)
JSP页面的中文字要全部写到资源文件中,注意每种语言资源文件都要写到,否则在使用某种语言
Struts+Mysql+Tomcat5.0.28+mysql-connector-java-3.0.16-ga-bin.jar 国际化乱码解决方法
国际化的东西带来的问题还真的好多,各国语言不同,所使用的字符集都不一样,JAVA,Mysql,Tomcat,浏览器等等用的字符集也不一样,这几天气得我都说了好几次不用什么Struts,Mysql,Tomcat了,全部都是自己写出来好了,用统一的编码统一的字符集,可惜能力不够,说说而已,问题还是得解决。在网上查了好久,自己也实践了好多天,问题终于算是解决了。
因为要考虑到很多国的语言,一开始就把项目立足于国际化,在把连接方式从Struts连接池改为Tomcat的连接池之后,原来我遇到的乱码有
1、资源文件里读出来在页面上的乱码;
2、数据库读出来的乱码
3、数据库写进去的乱码
4、在ActionForm验证不通过Errors返回的乱码,也就是request,IE参数传递的乱码了。
5、有一个JS写的时间选择跳出框,如果在charset=UTF-8的时候就出错,但如果我改为charset=GBK或GB2312时就一切正常了,很奇怪的现象,我还找不出答案来。
哇,好多的乱码。。。。头都大了好几天。
下面是我的解决方法
1、资源文件里读出来在页面上的乱码:这个最容易解决了,把写好的ApplicationResources.properties文件,在DOC底下用 native2ascii -encoding gb2312 ApplicationResources.properties
ApplicationResources_zh_CN.properties 命令来个字符编码转换,将原来的中文转为Unicode编码就搞定了中文简体,繁体也用同样的命令,只是把 bg2312 改为 bgk 就可以了。
2、数据库读写的乱码,刚开始的时候因为受以前的SQL Server+JDBC 影响(在那时写入数据库是可以不用做什么工作的,只是在读出来的时候来个 gbk = new String(iso.getBytes("ISO-8859-1"), "GBK") 转换就行了)我也都在把读写都在转换,搞得好复杂也很麻烦,后来在连接池连接代码jdbc:mysql://localhost:3306/database?autoReconnect=true&useUnicode=true&characterEncoding=GBK那里加上一个&useUnicode=true&characterEncoding=GBK,保证了在数据库操作时候使用了统一的编码字符集,又解决了两个乱码问题,一举两得,嘿嘿。
4、request,response的乱码在网上找了好久,也有两个解决的办法,也是来个转换,还有种办法是写一个过滤器,我选择了后者,因为简单:),这方法用到两个文件,一个是 filter ,一个是 web.xml 文件,代码在后面。
5、至于JS的这个问题,还没办法,只好在JSP页面上改为<%@ page contentType="text/html; charset=GBK"%>了,反正这样也没问题。
到此为止,乱码问题总算告一段落了,感觉蛮不错的,郁闷了这么久,终于可以高兴了好大一段子了。
-------------------------------------------------------------------------------------------------------------------
过滤器使用代码:EncodingFilter.java
package com.***.***.***;
import javax.servlet.*;
import java.io.IOException;
/**
* <p>Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:</p>
* <ul>
* <li><strong>encoding</strong> - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the <code>ignore</code> initialization parameter. This parameter
* is required, so there is no default.</li>
* <li><strong>ignore</strong> - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* <code>selectEncoding()</code> method is set. If set to "false,
* <code>selectEncoding()</code> is called <strong>only</strong> if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".</li>
* </ul>
*
* <p>Although this filter can be used unchanged, it is also easy to
* subclass it and make the <code>selectEncoding()</code> method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the <code>Accept-Language</code>
* and <code>User-Agent</code> headers, or a value stashed in the current
* user's session.</p>
*
*/
public class EncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;
/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;
/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}//EOC
WEB.XML文件:将下面的代码放在WEB.xml文件的<display-name>下面就行了
<filter>
<filter-name>encodingFilter</filter-name>
<display-name>EncodingFilter</display-name>
<description>Set the request encoding</description>
<filter-class>com.voip.admin.util.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB18030</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 国际化及乱码问题
- 中文乱码问题和国际化
- Struts2中文国际化乱码问题
- struts解决中文乱码和国际化问题
- web ajax应用国际化、乱码问题总结
- java国际化IDEA控制台乱码问题
- struts2 国际化 中文乱码英文不乱码问题
- struts的国际化文件和中文乱码问题
- 初接触JAVA国际化问题-解决JAVA中文字符乱码
- struts国际化带来的javascript 中文乱码问题
- Qt 国际化编程,真正解决汉字显示方块、乱码问题!
- 一个解决VC++程序国际化的类,解决乱码问题
- Mysql数据库的乱码、字符集、国际化问题的解决
- 一个解决VC++程序国际化的类,解决乱码问题
- 解决 IntelliJ IDEA 中 i18n国际化 中文乱码问题。
- jsp乱码及java后台乱码问题
- Struts2国际化乱码问题解决
- Struts国际化乱码解决方法- -
- CF卡启动WinCE
- 使用C#格式化字符串
- jbpm例子的结构
- Spring+Hibernate3的框架中用到了关联查询
- 灰度图如何避免闪烁
- 国际化及乱码问题
- ASP.Net调用存储过程的几句话
- http错误代码含义
- Linux下两种自动启动Tomcat的方法
- 树窗口
- ASP.NET 1.1中页面认证
- Borland Together Architect for Eclipse启动Dos窗口屏蔽方法
- SQL Server数据库三种分页方案详尽分析
- 如何分析散户跟风系数和盘口能量