竹磬网-邵珠庆の日记 生命只有一次,你可以用它来做些更多伟大的事情–Make the world a little better and easier

175月/080

解决struts国际化和中文问题

当今Struts框架的应用已经非常的成熟了,基本的配置我就不多说了,大家上google去搜搜就有一大堆. 最近在一次项目开发中碰到了Struts的I18N问题,我来粗略谈谈基本用法,让大家有对Struts的国际化问题有一个简略的认识.(注意,以下是在struts开发环境配置好了的情况下实现国际化的步骤,顺便解决了中文问题):

1.  设置所有JSP页面的charset为UTF-8.  即在每个JSP页面前加上<%@ page language="Java" contentType="text/html;charset=UTF-8" %>.  java是通过unicode实现国际化的,然而unicode和UTF-8是一一对应的关系.

2.  JSP页面里面没有硬编码的文字(即页面的文字都是从*.properties资源文件里面读出来的,用<bean:message key="keyword in property file">读取即可.) 资源文件的配置也不多说了,在web.xml里面配配就好. 下面假设英文的资源文件叫ApplicationResources_en.properties ,中文的源文件叫ApplicationResources_xx.properties(value都是中文的) . 用JDK自带的native2ascii工具把中文的资源文件里面的中文转化为为用ASCII表示的Unicode编码, 命令如下:  native2ascii -encoding GBK ApplicationResources_xx.properties ApplicationResources_zh.properties . (中文操作系统里面默认是GBK,它是gb2312的扩充集),好了,如果你不用form传中文,不用入库,那么你成功了.打开浏览器在internet选项里面设置一下语言试试. 容易吧,呵呵. 下面的步骤涉及到入库问题.

3.  写一个Filter类,一个最简单的代码例子如下:

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

public class CharsetFilter implements Filter{
private FilterConfig config = null;
private String defaultEncode = "UTF-8";

public void init(FilterConfig config) throws ServletException {
this.config = config;
if(config.getInitParameter("Charset")!=null){
defaultEncode=config.getInitParameter("Charset");
}
}

public void destroy() {
this.config = null;
}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest srequest=request;
srequest.setCharacterEncoding(defaultEncode);
chain.doFilter(srequest,response);
}
}

然后你需要在web.xml里面设置一下Filter,加入下面的即可(注意,如果你是在JBX里面开发,声明filter一定要在声明<servlet>前面,否则会报错,但是用的时候好像又没有问题.)

<filter>
<filter-name>Character Encoding</filter-name>
<filter-class>com.alex.util.CharsetEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>

4.   接下来是写一个Converter类,在入库前调用encode(),出库的时候调用decode()就ok. 下面是一个简单例子:

public class Converter {
public Converter() {
}

public static String enCode(String str) {
byte temp [];
temp = str.getBytes();
try {
//System.out.println("in before convert: " + str);
str = new String(temp , "ISO-8859-1");
//System.out.println("in after convert: " + str);
}
catch(Exception e) {
System.err.println("convert error: " + e);
}
return str;
}

public static String deCode(String str) {
byte temp [];
try {
//System.out.println("out before convert: " + str);
temp = str.getBytes("ISO-8859-1");
str = new String(temp,"GBK");
//System.out.println("out after convert: " + str);
}
catch(Exception e) {
System.err.println("convert error: " + e);
}
return str;
}
}

5.  应该都OK了吧,我就这样解决了struts的中文问题和国际化问题.

喜欢这个文章吗?

考虑订阅我们的RSS Feed吧!

发布在 邵珠庆

评论 (0) 引用 (0)

还没有评论.


Leave a comment

引用被禁用.