This post has dragged on for a long time, and now it's done, ending the struts2 section, which is just the basic part, and the project needs a deeper understanding. The Next Web Backend section prepares to do Spring framework learning ~ Well, come on!
Directory:
- Program Internationalization Overview
- Internationalization steps
- Load global resource files in Struts2
- International information output
- Properties File Configuration
1. Specific content
An overview of 1.1 program internationalization
program internationalization has become the basic requirement of Web application. This is to facilitate visitors from all over the world. STRUTS2 internationalization mainly includes JSP page internationalization, verification information internationalization and action information internationalization.
1.1.1 The idea of Java internationalization
The idea of the internationalization of Java is to put the information in the program into the resource file, and the program reads the corresponding resource file according to the supported country and language environment . The resource file is a Key-value pair, and the key in each resource file is constant, but value varies depending on the country/language.
The internationalization of Java programs is mainly accomplished by two classes:
- Java.util.Locale: Used to provide local information, often referred to as the locale . Different languages, different countries and regions are represented by different locale objects;
- Java.util.ResourceBundle: This class is called a resource bundle and contains resource objects for a specific locale.
In order to realize the internationalization of Java programs, the resource files required by the program must be provided beforehand. Where key is the part used by the program, and value is the display of the program Interface .
Resource files can be named in the following 3 ways:
- Basename.properties;
- Basename_language.properties;
- Basename_language_country.properties (usually with this);
BaseName is the basic name of the resource file and is freely defined by the user. However, language and country must be supported languages and country/region codes for the Java language. For example: Mainland China: Basename_zh_cn.properties, the United States for basename_en_us.properties.
1.2 internationalization steps
1.2.1 Loading global resource files in Struts2
The global resource file is loaded by configuring the constant Struts.custom.il8n.resources implementation in the Struts.xml file , which is the basename of the global resource file.
Once a global resource file is specified, program internationalization is achieved. Assuming BaseName is messageresource, the following constant is configured in the Struts.xml file:
<name= "Struts.custom.i18n.resources" value= " Messageresouce "/>
once the global resource file is loaded in this way, the Struts2 app can get these resource files everywhere, including JSP pages, action, and checksum files .
1.2.2 Internationalization Information output
(1) Output mode
There are several ways to export the international information:
In order to output internationalization information in a JSP page, you can use the STRUTS2 <s:text.../> tag, which specifies a name property that specifies the key in the Internationalized resource file.
Specify the key in the resource file by using the key property, such as:
<name= "Realname" key= "username"/> < !-- or the following method - <name= "Realname" Label= "%{gettext (' username ')}"/>
- Output in the Action class
The action class can inherit Actionsupport and use the GetText () method to obtain internationalized information, the first parameter of which is used to specify the key in the resource file.
- Output in a checksum file
In a checksum file, you can specify key in the resource file by using the key property of the message, such as:
<key= "Login.error.username"/>
1.2.3 Properties File Configuration
Properties file configuration, directly under the SRC folder to create a new configuration, note that the Key-value form exists,
In the final analysis, the biggest feature of the STURTS2 framework is the interceptor, which is important to understand the interceptor configuration and principle.
Javaweb frame _struts2_ (eight) internationalization of the----->struts2