Struts Message Resources

xiaoxiao2021-03-06  20

Struts Message Resources Author: Nick Heudecker From: unknown Overview   many programmers just learning Struts in the use of Struts MessageResources properties will encounter many difficulties. This article will try to explain the advantages of the MessageResources characteristics and give specific examples to explain its usage. Author: Nick Heudecker, System Mobile Inc. Overview  MessageResources class enables developers to easily support multiple languages, including support for multi-time format and digital format. Another benefit using the resource package is to allow developers to store label strings in a location instead of dispersing in different JSP pages. For example, for the label "first name" of each user, we can write it in the resource package, simply reference to the Struts label in the appropriate place:

This will make you easier for you to change your program, you don't have to change the content of the label in every JSP page.   Using the message resource package requires you to do the following:   1. Create a message resource package for the places you want to support. 2. Configure web applications, load message resource packages. 3. Use the corresponding JSP tag to load resources or ...4. ... Load resources in an Action class. Creating a resource package The default implementation of the MessageResources class is a file containing "key = value" pair, and an example of a message resource package file below. Label.username = username

Label.password = password

Label.first.name = first name

Label.last.name = last name

Label.email = Email Address

Label.phone.number = Phone Number

Label.welcome = Welcome Back {0} {1}!

Error.Min.Length = The Input Must Be at Least {0} Characters in Length.

Error.max.length = The Input Cannot Be Longer Than {0} Characters in Length.

The integer surrounded by braces is support for the Java.Text.MessageFormat class, and the programmer can pass parameters to the Value string, and you can pass 4 parameters for each Value string. Configuration   There are two ways to inform the Struts Your resource package location: web.xml file or struts-config.xml file. First, look at the configuration of the web.xml file:

Action

Org.apache.struts.Action.ActionServlet

Application

com.systemMobile.example.ApplicationResources

This configuration indicates that your resource package is ApplicationResources.properties, which is located in the com.systemMobile.example package. The suffix ".properties" is implied, you don't have to write it out. If you have another resource file in the same package, such as ApplicationResources_Fr.Properties, you can support French, you only need to list the file name as you defined above.   Define the second middle method of the resource file (mentioned above), is configured in the struts-config.xml file: Properties parameter is necessary. As in the web.xml file, you need to pay attention to the location of the file in the package. Use the struts-config.xml file to configure the message resource file is a recommended approach because it is more scalable and flexible.

You can use the Message-Resources tab from different resource files from different resource files, provided the value of different Key properties for different resource files when configuring. For example:

You must use Bean: Message Tags: After setting the value of the attribute null is "false", if a resource string does not exist, it will return ??? KEY ??? rather than just display NULL. This is easy to see the resources you have not defined in the JSP page so that internal testing is faster. (For details on how to get messages from resource files See INTATIONS PARAMETER = "com.systemmobile.example.ApplicationResources" null = "false" /> In addition, the Message-Resources tag allows you to use yourself The implementation of the MessageResourcesFactory interface, which is not discussed herein. The resource file is placed   about the most common problem of resource documents is where the resource file is placed in the WAR file. Simple answer is that this file must be placed under your classpath, which means putting the resource file in a JAR file, or put it under the ultimate subdirectory of the / web-inf / class content. The following table gives the location of the resource file, the value of the "Parameter" attribute in the Message-Resources tab, and a short description.

Resources Locationparameter ValueDescription / WEB-INF / classes / ApplicationResources.propertiesApplicationResources files in the classes directory, the directory in the classpath web applications in ./WEB-INF/classes/resources/ApplicationResources.propertiesresources.ApplicationResources the files in "resources" In the directory, the package name is to give it. In the app.jar file, in the com.systemmobile.example package / directory.com.systemMobile.example.ApplicationResources file in the JAR file in the JAR file. Tags  most common Struts tag is a bean: Message label. "Key" using this tag can read a specific message resource from the resource file. You can also pass one or all of the four parameters:

                                                       Obviously, error messages or message information must be saved in the request, otherwise, nothing will not be displayed. Here is an example of displaying message information:

There are some tags that are also limited to message resources, such as HTML: LINK. HTML: The LINK tag displays the title text by defining the "titlekey" property. Many HTML uses "Altkey" properties to get Alternate text from the resource file. You can also use the message resource file in the Action class. Action class has two ways to get an instance of a MessageResource class: // Return to a resource file in a request

PROTECTED MessageResources getResources (httpservletRequest Request);

/ / Return to a resource file in a request,

/ / The content of the resource file element

Protected MessageResources getResources (javax.servlet.http.httpservletRequest request, java.lang.string key);

The MessageResources class allows you to get localized messages from the resource file. The API for messageResources can be found in the resource. Comparative common methods include: // There Methods Load A Resources Key for the Given Localepublic String GetMessage (Java.util.Locale Locale, Java.lang.String Key);

Public String GetMessage (java.util.locale locale, java.lang.string key,

Java.lang.Object arg0);

Public String GetMessage (java.util.locale locale, java.lang.string key,

Java.lang.Object [] args);

Public String GetMessage (java.util.locale locale, java.lang.string key,

Java.lang.object arg0, java.lang.object arg1

Public String GetMessage (java.util.locale locale, java.lang.string key,

Java.lang.object arg0, java.lang.object arg1, java.lang.object arg2;

Public String getMessage (java.util.locale locale, java.lang.string key, java.lang.object arg0,

Java.lang.Object arg1, java.lang.object arg2, java.lang.object arg3);

// THESE METHODS LOAD A Resources Key for the Locale Retrieved

// from the httpservletRequest

Public String GetMessage (java.lang.string key);

Public String GetMessage (java.lang.string key, java.lang.object arg0);

Public String GetMessage (java.lang.string key, java.lang.object [] args);

Public String GetMessage (java.lang.string key, java.lang.object arg0,

Java.lang.object arg1);

Public String GetMessage (java.lang.string key, java.lang.object arg0,

Java.lang.Object arg1, java.lang.object arg2;

Public String GetMessage (java.lang.string key, java.lang.object arg0,

Java.lang.Object arg1, java.lang.object arg2, java.lang.object arg3);

  These returned strings can be set to the parameters of the Request or Session and the tabs will represent the layer. You may notice some overload method getMessage (...) to select the parameter Object, while others use parameter arg0 ... Arg3. This is equivalent to bean: Message Arg0 ... arg3 attribute. In addition to the MessageResources class, there are some types of resource files. The ActionMessage class is used to deliver keys in the message resource from the Action to JSP. The message is used as the properties of the bean. ActionError, ActionMessage subclasses, using the keys in the message resource to store the error message after the failure. Internationalization   extracting a localization information from the resource file can be handled by class MessageResources or by its direct subclass PropertyMessageResources class. Since Class PropertyMessageResources, you have used it, then let's take a look at how it is using the getMessage (local, String) method to read the message from the resource file. Example: 1. If you do not find the definition of the message in ApplicationResources_pt_br.properties (Brazilian Portuguese), the system will be found in the ApplicationResources_pt.properties file, if the ApplicationResources_pt.properties file does not exist or there is no message, then Go to the ApplicationResources.properties file to find it. 2. If the message is found, it is added to a localized cache and returns java.lang.string data. 3. If the message is not found, if the returnnnull property is the default true, NULL will be returned. Otherwise the string of similar? ?? KEY ???, Key is the passable parameter. The FMT tag ofJSTL (JavaServer Pages Standard Tag Library "is recently started to display the information of the resource file to the JSP. It can be combined with Struts. Using it is very simple, just download JSTL's JAR files and TLDs and copy them to your application's correct location, then add the following: javax. servlet.jsp.jstl.fmt.localizationContext

ApplicationResources

The above configuration is assumed that your ApplicationResources.properties file is placed under the / web-inf / class directory. See Above more situations. Then put this tag library directly in your JSP: <% @ taglib prefix = "fmt" URI = "http://java.sun.com/jstl/fmt"%>

Finally, the following tag will display the content of the resource file:

There is also an example of obtaining resources using an FMT tag. (Note: This program is taken from JAKARTA JSTL EXAMPLES.) // Loading A Resource from a specific bundle and populating a parameter

// using the foreach iterator to populate paramartes

Conclusion The Struts uses a message resource file to create an international Web application while providing a message to the JSP file. We can use the JSTL tags in rapid development, or you can use the Struts tag or to get a specific message directly in the action. I hope this article will clarify some Struts often used, sometimes confused. About the author   Nick Heudecker is a software developer, has a 6-year development experience of enterprise applications. His company, SYSTEM Mobile, Inc., specializing in application integration, custom software development and wireless applications. He is still a Sun certified Java programmer, now living in Ann Arbor, Michigan. Resources The following resources may be helpful to find more about Struts resource files:

Javadoc for the classes of intert:

java.util.ResourceBundle java.util.Locale org.apache.struts.util.MessageResources org.apache.struts.action.ActionError org.apache.struts.action.ActionMessageTed Husted's Struts Tips: Very handy advice, especially for resource bundle usage . Struts Home Page Struts-User mailing List Archive: Many people use Struts, so there is a very good chance that your question has be answered already Please use all available resources before asking your question on the mailing list JSTL Homepage and the Jakarta.. JSTL Implementation considerations Packages are just directory structures used to avoid naming conflicts. If you put a message bundle in a directory named resources under / WEB-INF / classes, this is the equivalent of putting the file in a package named resrouces. While Basic, this Point Seems to Trip Up Many New Program.

转载请注明原文地址:https://www.9cbs.com/read-42821.html

New Post(0)