您的位置:首页 > 编程语言 > Java开发

Struts中国际化的简单例子

2005-03-18 16:25 417 查看
application_tmp.properties
word.key=word
hello.key=你好
aaaa.key=这个bean存在.
bbbb.key=这个bean不存在.
error.key=发生一个错误,请检查!
params.key=暗暗暗暗暗暗啊,{0},不?徊徊徊徊徊徊?{1},我我我我我我我,{2},的的的的的的的

编码转换命令:
D:/yangjin/test3>native2ascii -encoding GBK ./web-inf/classes/test/application_cn_tmp.properties ./web-inf/classes/test/application_zh_CN.properties

application_zh_CN.properties
word.key=word
hello.key=/u4f60/u597d
aaaa.key=/u8fd9/u4e2abean/u5b58/u5728.
bbbb.key=/u8fd9/u4e2abean/u4e0d/u5b58/u5728.
error.key=/u53d1/u751f/u4e00/u4e2a/u9519/u8bef/uff0c/u8bf7/u68c0/u67e5/uff01
params.key=/u6697/u6697/u6697/u6697/u6697/u6697/u554a,{0},/u4e0d/u4e0d/u4e0d/u4e0d/u4e0d/u4e0d/u4e0d/u4e0d,{1},/u6211/u6211/u6211/u6211/u6211/u6211/u6211,{2},/u7684/u7684/u7684/u7684/u7684/u7684/u7684

application.properties
word.key=word
hello.key=hello
aaaa.key=this bean is present.
bbbb.key=this bean isn't present.
error.key=raise a error, please check!
params.key=AAAAAAAAAAA,{0},BBBBBBBBBB,{1},CCCCCCCCCCC,{2},DDDDDDDDDD

application_en.properties //在字符串后加上”_“,以便和默认文件相区别。
word.key=word_
hello.key=hello_
aaaa.key=this bean is present._
bbbb.key=this bean isn't present._
error.key=raise a error, please check!_
params.key=AAAAAAAAAAA,{0},BBBBBBBBBB,{1},CCCCCCCCCCC,{2},DDDDDDDDDD_

HelloBean.java
.......
private boolean match(String str) {
Pattern p = Pattern.compile("[^0-9]+"); //正则表达式验证:如果字符串中含有数字,返回错误。
Matcher m = p.matcher(str);
return m.matches();
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (word == null || !match(word)) {
errors.add("word", new ActionMessage("error.key"));
}
return errors;
}

struts-config.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts-config PUBLIC '-//Apache Software Foundation//DTD Struts Configuration 1.1//EN' 'http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd'>
<struts-config>
<form-beans>
<form-bean name="helloBean" type="test.HelloBean" />
</form-beans>
<action-mappings>
<action path="/HelloAction" type="test.HelloAction" name="helloBean" scope="request" validate="true" input="/error.jsp">
<forward name="sayHello" path="/hello.jsp" />
</action>
</action-mappings>
<message-resources parameter="test.application"/>
</struts-config>

error.jsp
<%@ page contentType="text/html;charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:errors/>

结果:
将浏览器的“语言”选项设为"en"。

http://localhost:2000/test3/HelloAction.do?word=bi234

raise a error, please check!_

将浏览器的“语言”选项设为"zh_CN"。
http://localhost:2000/test3/HelloAction.do?word=bi234

发生一个错误,请检查!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: