Struts2 resource file search order

http://www.blogjava.net/max/archive/2006/11/01/78536.html

Struts 2.0 of internationalization is more flexible because it can be configured and access to resources (properties) file according to different needs. There are several ways in Struts 2.0:

  1. Using global resource files, the method shown in the above example. This applies throughout the entire application internationalized strings, which are referenced in different packages (package), as some of the more common error message;
  2. Using resource files within the package range. Practice is a new name in the root directory of the package package.properties and package_xx_XX.properties file. This applies to access different types of resources in the package;
  3. Action range using resource files. Practice for the new package file name Action Action class with the same name of the resource file (except the file extension). It can only be accessed in the Action. In this way, we can use the same name properties represent different values ​​in different Action. For example, in the title ActonOne as "an action", while the same title in ActionTwo represented by "Action II", named save some time;
  4. Use <s: i18n> flag properties file access a particular path. Use Please refer to my earlier article, " commonly used Struts 2.0 logo (Tag) Introduction ." When you use this method, please note that <s: i18n> range logo. In the <s: i18n name = "xxxxx "> to </ s: i18n> among all international string will look for a file named xxxxx resources, if not find, Struts 2.0 will output the default value (international the name of the string).

The above mentioned four configuration and method of access to resources, their scope are descending, and Struts 2.0 internationalized strings in the lookup is followed by a specific order, shown in Figure 3:

3 resource file search sequence diagram
3 resource file search sequence diagram

Suppose we call the getText in a ChildAction in ( "user.title"), Struts 2.0 will perform the following operations:

  1. Find ChildAction_xx_XX.properties file or ChildAction.properties;
  2. Find interfaces ChildAction implemented, find the resource file MyInterface.properties interface with the same name;
  3. Find ChildAction parent ParentAction the properties file named ParentAction.properties;
  4. Determine whether the current ChildAction implement the interface ModelDriven. If so, call getModel () to get the object, find the resource file with the same name;
  5. Find the current package.properties file in the package;
  6. Find a parent package of the current packet until the top level package;
  7. In the value of the stack (Value Stack), look for a property named user to user types with the same name of the resource file, find the key for the title of the resource;
  8. Find a default resource file struts.properties configuration, refer to Example 1;
  9. Output user.title.
Published 18 original articles · won praise 16 · views 390 000 +

Guess you like

Origin blog.csdn.net/f_zongjian/article/details/5712368