深入理解Struts2國際化信息機制
這兩天學習了Struts2國際化信息機制,感覺很重要,所以,今天添加一點小筆記。
國際化信息機制 (三種 Action范圍、 Package范圍、 全局)
1. 全局國際化配置信息文件
全局國際化文件,對所有Action 生效,任何程序都可以訪問到,需要在struts.xml 配置常量 struts.custom.i18n.resources指定信息文件
頁面product.jsp
<s:fielderror/>
<form action="${pageContext.request.contextPath }/product_add.action" method="post">
商品名:<input type="text" name="name"/><br/>
價格:<input type="password" name="price"/><br/>
<input type="submit" value="登錄"/>
</form>
編寫ProductAction
public class ProductAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String name;
private double price;
public String add(){
System.out.println(name+"---------"+price);
return SUCCESS;
/*
get(),set()方法略去.................
*/
}
}
添加校驗信息:(對Action的方法進行校驗 ProductAction-product_add-validation.xml)
ProductAction-product_add-validation.xml其中product_add是Struts.xml中action標簽中的name的值
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<!-- 校驗商品 -->
<field name="name">
<field-validator type="requiredstring">
<message key="wc"/>
</field-validator>
</field>
</validators>
新建國際化信息文件 src下 messages.properties (默認的國際化文件)
注意:

1. 其中<message key="wc"/>中的Key必須是messages.properties 的Key值

2.messages.properties 的value值必須裝換成Unicode碼, 使用myeclipse開發(fā)工具,內(nèi)置properties editor 自動將中文轉(zhuǎn)換 Unicode碼
2. Action范圍國際化文件
在Action類 所在包 創(chuàng)建 Action類名.properties (無需在struts.xml 配置 )


3. package范圍國際化文件
在package下面 建立 package.properties (無需在struts.xml )


4. 在JSP頁面獲取
在國際化 messages.properties 添加一個信息

JSP頁面代碼:
<h1><s:i18n name="messages">
<s:text name="cn.wc"></s:text>
</s:i18n></h1>
5. 在Action代碼獲取
在messages.properties 添加國際化信息

Action轉(zhuǎn)發(fā)的頁面JSP
<s:text name="welcome">
<s:param>lxp</s:param>
</s:text>
Action代碼:
public class Product2Action extends ActionSupport {
private static final long serialVersionUID = 1L;
public String add(){
System.out.println(this.getText("welcome",new String[]{"Action"}));
return SUCCESS;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Spring Boot實戰(zhàn)練習之單元測試篇
單元測試(unit testing),是指對軟件中的最小可測試單元進行檢查和驗證。對于單元測試中單元的含義,一般來說,要根據(jù)實際情況去判定其具體含義,如C語言中單元指一個函數(shù),Java里單元指一個類,圖形化的軟件中可以指一個窗口或一個菜單等2021-10-10
Java如何獲取@TableField,@TableName注解的值
這篇文章主要介紹了Java如何獲取@TableField,@TableName注解的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring中@PropertySource注解使用場景解析
這篇文章主要介紹了Spring中@PropertySource注解使用場景解析,@PropertySource注解就是Spring中提供的一個可以加載配置文件的注解,并且可以將配置文件中的內(nèi)容存放到Spring的環(huán)境變量中,需要的朋友可以參考下2023-11-11

