终极Struts中文及其它语言文字处理问题(文字国际化)

  文字国际化问题,对于初学者来说常常令人感到头痛;其实解决很容易,而且解决的办法有很多种。今天就struts的文字国际化处理作个总结,大家可以在这个思路上进一步灵活运用。

  1. 首先所有的文字编码均采用UTF-8格式,至于为什么要采用UTF-8,大家可以翻阅资料。如:jsp中,可以加入:
  <%@ page language="java" contentType="text/html; charset=utf-8" %>

  2. struts框架提供了资源信息文件,它包含了jsp页面内容的一些文字说明,以及另一些供代码中使用的信息输出等(说白了,就是些文字描述定义,可以当成变量看待)。比如:ApplicationResources.properties(默认的是英文),如果你想要建立中文的资源信息文件,可以copy ApplicationResources.properties一份,命名为:ApplicationResources_a.properties,然后修改ApplicationResources_a.properties的内容为中文文字(可别把变量名也改成中文了,会造扁!)。这样你的中文资源信息文件建立了。
  接下来:dos下(或别的编译器),回到该文件所在的目录,执行:
native2ascii -encoding gb2312 ApplicationResources_a.properties ApplicationResources_zh.properties
这样就在当前目录下生成了ApplicationResources_zh.properties(为什么后面要用“_zh”,别急,下面会描述清楚的)
  OK,大功告成,structs的其它的一切不变,发布上去后,如果对方是中文操作系统,就会以ApplicationResources_zh.properties中的内容为主,如果是英文操作系统就会以ApplicationResources.properties中的内容为主。
  对了,刚刚提到了“_zh”,是如何得来的呢?简单,在IE里面,选择“工具”、“Internet选项”,里面有个“语言”按纽,仔细看看世界各国语言的表示方法吧,我相信,通过后缀,你会知道,如何将资源文件命名成法文、德文。。。。一切就是这么简单。。。。。

  3. struts中,对于中文参数的传递的文字编码处理,一般加了个过滤器类,该类放在你的项目所在的类里面。java源代码如下:

package com.zxjsoft.util;

import javax.servlet.*;
import java.io.IOException;


public class SetCharacterEncodingFilter implements Filter
{
 protected String encoding = null;
 protected FilterConfig filterConfig = null;
 protected boolean ignore = true;
 
 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);
  }
  chain.doFilter(request, response);
 }

 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 String selectEncoding(ServletRequest request)
 {
  return (this.encoding);
 }
}

编译完后,放好位置,修改你的项目所在的web.xml文件,添加内容如下:

<filter>
 <filter-name>Set Character Encoding</filter-name>
 <filter-class>com.zxjsoft.util.SetCharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>utf-8</param-value>
 </init-param>
 <init-param>
  <param-name>ignore</param-name>
  <param-value>true</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>Set Character Encoding</filter-name>
 <servlet-name>action</servlet-name>
</filter-mapping>

(注意上面代码添加的顺序,可见我的Tomcat容器见章节

附:
有人可能在web.xml中添加上述代码,在ecplise中,有报错提示,如果有这种情况发生,不妨修改web.xml前缀为:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

………………………………………………

 

这个问题就描述到这里,如有疑问或不正确的地方,大可提出,以便本人及时更正。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值