Struts2 study notes (8) -- International, struts2 Study Notes
1. Introduction to Struts2 Internationalization
Internationalization (i18n): to internationalize a program, you must first provide the resource files required by the program. The content of the resource file is a key-value pair.
Resource file naming can be in the following three forms:
- BaseName_language_country.properties
- BaseName_language.properties
- BaseName. properties
The baseName is the basic name of the resource file, which can be freely defined by the user. language and country cannot be changed at will and must be the language and country supported by Java.
2. Classification of international resource files
Struts2 provides four methods to load international resource files:
1). the global range can be placed anywhere, but the resource file must be introduced through constants in the struts. xml file.
BaseName_language_country.properties. For example, message_zh_CN.properties
1 <! -- This is in com. sunny. under the action package --> 2 <constant name = "struts. custom. i18n. resources "value =" com. sunny. action. message "> </constant> 3 <! -- This is under src --> 4 <constant name = "struts. custom. i18n. resources" value = "message"> </constant>
2). Package range, under a certain package.
Package_language_country.properties. For example, package_zh_CN.properties
3). Class scope, under the same path of the class.
ActionName_language_country.properties. For example, LoginAction_zh_CN.properties
4). Temporary resource file. Use the <s: i18n> flag on the jsp page to specify the resource file under the classes path.
1 <s:i18n name="com.sunny.action.I18NAction">2 <s:text name="GOOD"></s:text>3 </s:i18n>
3. Struts2 uses international messages
Struts2 uses international messages in the following three methods:
1) use international messages on the JSP page, and use the <s: text... /> Tag, which can specify a name attribute that specifies the key in the international resource file.
1 <s:text name="login.username"></s:text>2 <s:textfield name="username" key="login.username"></s:textfield>
You can also output information with placeholders. For example, if there is a login. welcome = Hello {0}, you can use the following method to display it:
<s:text name="login.welcome"> <s:param>username</s:param></s:text>
2) When using international messages in the Action class, you can use the getText () method of the ActionSupport class. This method can accept a name parameter, which specifies the key in the international resource file.
1 getText("login.username");
You can also use information with placeholders. For example, if there is a login. welcome = Hello {0}, you can use the following method to display it:
1 String params [] = {"Zhang San"}; 2 String welcome = getText ("login. welcome", params );
3) using international information in the form meta Label, you can specify a key attribute for the form Label, which specifies the key in the international resource file.
4. International instances
1) create a global resource file
Login_zh_CN.properties
1 item. name = name 2 item. password = password
Login_en_US.properties
1 item.name=name2 item.password=password
2) configure the international resource file in the struts. xml file
1 <struts> 2 <constant name="struts.devMode" value="true" /> 3 <constant name="struts.custom.i18n.resources" value="Login" /> 4 5 <package name="default" namespace="/" extends="struts-default"> 6 <action name="i18nAction" class="com.sunny.action.I18NAction"> 7 <result>/input.jsp</result> 8 </action> 9 </package>10 11 </struts>
3) create a jsp page
1 <body> 2 <a href = "$ {pageContext. servletContext. contextPath}/i18nAction? Request_locale = zh_CN "> Chinese </a> 3 <a href =" $ {pageContext. servletContext. contextPath}/i18nAction? Request_locale = en_US "> English </a> 4 <form action =" $ {pageContext. servletContext. contextPath}/login. action "> 5 <s: textfield name =" name "key =" item. name "/> <br> 6 <s: textfield name =" password "key =" item. password "/> <br> 7 </form> 8 </body>
4) jsp display interface