struts.xml
<struts> <constant name="struts.custom.i18n.resources" value="global" /> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="locale" class="com.tw511.common.action.LocaleAction"> <result name="SUCCESS">pages/welcome.jsp</result> <param name="EnglishParam">English</param> <param name="ChineseParam">Chinese</param> <param name="FranceParam">France</param> </action> </package> </struts>
要從struts.xml中獲取靜態引數值,Action類必須實現引數化Parameterizable介面。動作的靜態引數是由staticParams攔截,其中包括在預設堆疊控制
//... import com.opensymphony.xwork2.config.entities.Parameterizable; public class LocaleAction implements Parameterizable{ Map<String, String> params; //... public void setParams(Map<String, String> params) { this.params = params; } }
在動作類的初始化,如果建立了getter和setter方法得當,staticParams攔截器將設定預先定義的引數值,以對應於該「引數」的每JavaBean屬性。
//... import com.opensymphony.xwork2.config.entities.Parameterizable; public class LocaleAction implements Parameterizable{ String englishParam; String chineseParam; String franceParam; public String getEnglishParam() { return englishParam; } public void setEnglishParam(String englishParam) { this.englishParam = englishParam; } public String getChineseParam() { return chineseParam; } public void setChineseParam(String chineseParam) { this.chineseParam = chineseParam; } public String getFranceParam() { return franceParam; } public void setFranceParam(String franceParam) { this.franceParam = franceParam; } //... }
在瀏覽器中開啟URL:http://localhost:8080/configure-param/locale.action