Display international information, custom interceptor

Source: Internet
Author: User
Tags i18n

IV. International Information Display
1. What is the internationalization principle? What is globalization?
The same software can provide different language interfaces for different users-International Software
A language resource package is required (many properties files, each of which is for a country or language. Different properties files are automatically read using Java programs based on the country language of the visitor)
 
2. Write a resource package
Properties File Name: Basic name_language (lower case) _ country (upper case). Properties
For example:
Messages_zh_cn.properties Chinese
Messages_en_us.properties us English

3. resourcebundle reads properties files from different countries based on different locale (region information)
Resourcebundle bundle = resourcebundle. getbundle ("messages", locale. US );

4. struts2 framework internationalization Configuration
The first global information file (all actions can be used) ------- the most common
* The properties file can be in any package
* You must configure the location of the global information file in struts. xml.
 
Struts. xml
<Constant name = "struts. Custom. i18n. Resources" value = "messages"> </constant> messages. properties is in the SRC root directory.
<Constant name = "struts. Custom. i18n. Resources" value = "cn. itcast. Resources. Messages"> </constant> messages. properties in the CN. itcast. Resources package

International Information
Use this. gettext ("MSG ");
Use: <s: Text name = "MSG"/> in JSP
In the configuration file (verify XML): <Message key = "agemsg"> </message>
 
The second action range information file (can only be used in an action)
Data can only be used in the corresponding action. Create the action class name in the package where the action class is located. properties --------- no need to configure

The third package range information file (all actions in the package can be used)
Data is valid for all actions in the package (including sub-packages). Create package. properties in the package ----- No configuration is required.

The fourth temporary information file (mainly introducing international information in JSP)
In JSP, specify the properties file to read.
<S: i18n name = "cn. itcast. struts2.demo7. Package">
<S: Text name = "customer"> </S: Text>
</S: i18n>

* ** Pass the {0} {1} ------------ messageformat dynamic message text to the message
This. gettext ("required", new string [] {"username "});

========================================================== ========================================================== ========================================================== ======================================
5. Custom interceptor
The use of interceptor comes from the idea of Spring AOP (for Aspect-Oriented Programming ).
The interceptor adopts the responsibility chain mode.
* In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain.
* Each node of the responsible chain can continue to call the next node or stop the process from being executed.

In struts2, multiple interceptors can be defined to form an interceptor stack (each interceptor in the stack is called sequentially)

1. All interceptors of struts2 must implement the interceptor interface.
2. The abstractinterceptor class implements the interceptor interface and provides a blank implementation for init and destroy.

In all practical development, the custom interceptor only needs to inherit the abstractinterceptor class and provides the intercept method implementation.
3. Common struts2 interceptor
<Interceptor-ref name = "modeldriven"/> model-driven
<Interceptor-ref name = "fileupload"/> File Upload
<Interceptor-ref name = "Params"> Parameter Parsing Encapsulation
<Interceptor-ref name = "conversionerror"/> type conversion error
<Interceptor-ref name = "validation"> request parameter verification
<Interceptor-ref name = "workflow"> intercept jump to the input View

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.