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
发生一个错误,请检查!
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
发生一个错误,请检查!
相关文章推荐
- struts2.1超简单例子 [1]
- 国际化(简单例子)
- 实战JBuilder8 + Struts,一个简单的例子
- [Struts]学习日记1 - 一个简单的例子
- Struts tiles入门(最最简单的例子)
- Struts1 - 配置 - 说明详细且配合例子,简单易懂
- struts中动态表单(DynaActionForm)的使用(超简单例子)
- struts国际化实例---登录例子(中英文切换)
- 介绍Struts 1.1 框架基本原理,以及介绍Struts 1.1 安装和一个简单的例子。
- 一个简单的Struts +ajax 的例子
- JAVA-STRUTS简单例子
- 简单的Struts 2.0x 登录验证 例子
- struts中的error简单用法及国际化转换的方法
- Struts1 - 配置 - 说明详细且配合例子,简单易懂
- Struts 教程I:一个用jbuilder X 做的最简单的helloworld的struts例子
- Struts_MappingDispatchAction简单的例子01
- struts简单例子的分析(含代码注释和配置文件说明)
- struts简单例子的分析(含代码注释和配置文件说明)
- struts简单例子的分析
- struts简单例子的分析(含代码注释和配置文件说明)