Struts 2 international implementation of the website as a whole Chinese and English switching example code

This paper introduces the international implementation of struts 2. The example code of the overall Chinese-English switching of the website is shared with you, as follows:

Environmental requirements: the Struts2 framework environment is successfully built

In order to realize the internationalization of the program, the resource files required by the program must be provided first. The content of the resource file is basically a key value pair, where key is the part used by the program and value is the display part of the program.

Resource files can be named in the following three forms:

Basename is the basic name of the resource file, which can be freely defined by the user, while language and country are unchanged and must be the language and country supported by Java. (refer to the official documents for the language resources of which country)

Java cannot support all countries and languages. You can get the support through getavailablelocale method of locale class. This method returns a locale array, which contains all supported countries and languages.

To create a resource file:

globalMessages_ en_ US. Properties (English)

globalMessages_ zh_ CN. Properties (Chinese)

Write a JSP page and test the switching effect between Chinese and English on this jsp:

Here is an I18N, which is the English abbreviation of internationalization. Why is it I18N?

Internationalization removes the I and N at the beginning and end, and there are just 18 characters left. It can be named like this

Write action:

The Chinese and English switching is tested here. The login process is not performed. The direct login is successful.

Configure the interceptor to select the language before entering the action

In struts 2, an I18N interceptor is provided, which automatically finds the request named request before executing the action method_ The parameter of locale is converted into a locale object after interception and placed in the attribute named "ww_trans_i18n_local" of the user session. I18N will be loaded automatically when the program is running. We can use these to set the language that allows users to automatically select web pages.

In struts Configure interceptor and internationalized resources in XML:

Finally, look at the running results:

Select Chinese language:

Select English as the language:

We can see that Chinese and English switching has been successfully realized. The switching of different languages only needs to provide language resource files of different countries.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>