Configure international resources for Struts2 and international resources for struts2

Source: Internet
Author: User
Tags i18n

Configure international resources for Struts2 and international resources for struts2

1. International goals

1). How to configure international resource files

I. Action range resource file: create a file named ActionName_language_country.properties in the path where the Action class file is located.


II. Package range resource file: Create an attribute file named package_language_country.properties under the package root path,
Once created, all actions in this package can access the resource file. Note: The baseName of the resource file in the package range is the package, not the package name of the Action.

 


III. Global Resource file
> Naming method: basename_language_country.properties
> Struts. xml <constant name = "struts. custom. i18n. resources" value = "baseName"/>

Step 1: Add two resource files under the src directoryThe naming method is very important. Format: baseName_language_country.properties. There are also two naming methods, both of which can be used. This does not need to be studied too much. For example, we need to add resource_zh_CN.properties and resource_en_US.properties to implement a switchover between Chinese and English. Then add the key-value pairs to these two files, and the key fields must be unified. For example:

Here, the value can use placeholders, such as: Hello {0}. As for how to pass the parameter, I will explain it later.

Step 2: configure the struts. properties FileThis file is also stored in the src directory.

The first key-value pair is used to tell the program that the international resource file is a resource file named after loginResource.

In fact, the configuration is complete, and the next step is to use it in various places.

(1) Use in jsp. Here are two examples.

<S: text name = "login. title"> </s: text>

<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> <s: property value = "username"/> </s: param> </s: text>

(2) used in Action. For example:

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:

String params [] = {"James "};

String welcome = getText ("login. welcome", params );

Step 3: switch between Chinese and English on the jsp page.

The main code of index. jsp is as follows:

1 <a href = "lang. action? Request_locale = zh_CN "> Chinese </a> 2 <a href =" lang. action? Request_locale = en_US "> English </a> 3 4 <s: form id =" loginform "action =" login "method =" post "> 5 <s: textfield name = "username" key = "login. username "> </s: textfield> 6 <s: textfield name =" psd "key =" login. psd "> </s: textfield> 7 <s: submit key =" login. submit "> </s: submit> 8 </s: form>

Mainly look at the first two lines of code, lang. action is actually an Action that implements page conversion. There is no substantive content. You can define a HelloAction at will. java: Return success in the execute () method. The request_locale parameter in the url is read by the i18n interceptor, and then the language environment is set based on the value. The i18n interceptor is the default interceptor in struts.

The struts. xml configuration is as follows:

 1         <action name="login" class="com.main.action.LoginAction"> 2             <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping> 3             <result name="success">/success.jsp</result> 4             <result name="error">/index.jsp</result> 5             <result name="input">/index.jsp</result> 6             <result name="regist">/regist.jsp</result>     7         </action> 8          9         <action name="lang" class="com.main.action.HelloAction">10             <result name="success">/index.jsp</result>11         </action>

Oh, it's basically done. The next step is to see the results. Run:

 

Expand knowledge:

Illustration:

 

 

Text description:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.