1、对JSP文件的文本,图片和按钮进行国际化
1).设置字符编码,将JSP页面字符编码统一设为“UTF-8”
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2).对文本国际化
即页面要显示内容:<bean:message>标签从Resource Bundle中获得。
<bean:message key="" />
3).对按钮国际化
常规的定义按钮为<html:submit property="submit" value="Submit" /><html:reset />
修改为:<html:submit property="submit" >
<bean:message key="jsp.page.submit" />
</html:submit>
<html:reset property="reset">
<bean:message key="jsp.page.reset" />
</html:reset>
4).对图片国际化
常规:<html:img page="/image/a.gif" alt="a" />
改为:<html:img pageKey="page.image.a" altKey="page.a" />
<html:img>标签将从Resource Bundle中读取图片名,当移到图片上显示的替代文本内容。其中pageKey属性指定图片名,altKey属性指定当用户将鼠标移到图片上显示的替代文本内容。
2、创建临时中文资源文件。
3、对临时中文资源文件进行转码。
在JDK中提供了native2ascii命令。在DOS下执行以下命令,将生成指定编码的资源文件。
native2ascii -encoding gb2312 application_temp.properties application_zh_CN_properties
application_temp.properties为刚创建的临时文件。
application_zh_CN.properties为生成的文件名。
4、创建其它语言环境的资源文件
5、采用servlet过滤器设置请求数据的字符编码
/**
*
*/
package com.yzinfo.gzzgov.tools;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* @author yuxueming
*
*/
public class StrutsFiter implements javax.servlet.Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
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");//取web.xml中配的参数
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
WEB.XML中的配置
<filter>
<filter-name>SetEncoding</filter-name>
<filter-class>com.yzinfo.gzzgov.tools.StrutsFiter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>