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
發生一個錯誤,請檢查!