Struts Source Code Research - Bean-Message Tags [Reserved]

xiaoxiao2021-03-06  20

http://www.7880.com/info/Article-44a40580.html

Struts is very common with such a tag:

As we all know, this label does this:

Access the resource files defined in Struts-Config.xml, generally Application.properties, usually defined:

According to the above definition, Struts will go to Web-INF / CLASSES / RESOURCE / go to the Application.properties file, which is from the above configuration information.

The surface looks like this, but by viewing the Struts source code, you can see the following code:

In the org.apache.struts.util.propertyMessageResources class, there is the following code:

Through this code, you can see

A, .properties extension is written in the code, so the resource file must use this extension

B, Struts is not simply looking for Application.properties files, but first find Application, assigning Name variables.

Then add the underscore "_", then add LocaleKey, then add .properties

C. After determining the file name, Struts has used the GetResourceAasstream method for the ClassLoader class to get an InputStream.

D, then Struts uses the LOAD method of the Java.util.Properties class, and read all the resources in the resource file to a HashMap.

E, then Struts can take out different messages to the front desk according to the Key value.

// set up to load the property resource for this local key, if we can

String name = config.replace ('.', '/');

IF (localeKey.length ()> 0) {

Name = "_" localkey;

}

Name = ".properties";

InputStream IS = NULL;

Properties PROPS = New Properties ();

// load the specified property resource

IF (log.isticraceenabled ()) {

Log.Trace ("Loading Resource '" Name "'");

}

ClassLoader ClassLoader = thread.currentthread (). GetContextClassLoader ();

IF (ClassLoader == Null) {

ClassLoader = this.getClass (). getClassLoader ();

}

IS = ClassLoader.getResourceSstream (Name);

IF (is! = null) {

Try {

Props.Load (IS);

} catch (ioException e) {log.error ("LoadLocale ()", e);

} finally {

Try {

Is.close ();

} catch (ioexception e) {

Log.Error ("LoadLocale ()", E);

}

}

}

The LOAD method in the D step can see the help documentation of JDK.

So, if we write Chinese in the resource file and there is a Chinese code problem (? Appear) at runtime (appearing), just confirm your resource file

Whether it is written by ISO8859 for coding

In addition, Struts is first in accordance with the above description, the Struts is found in the FileName_ $ locale.properties file.

If he can't find it, then he will find the default $ filename.properties, if you can't find it, then report an error.

This Struts lookup order is not my Du, there is the following struts source code (this method is in PropertyMessageResources.java):

Public String GetMessage (Locale Locale, String Key) {

IF (log.Indebugeload ()) {

Log.debug ("GetMessage (" Locale "," Key ")")

}

// Initialize Variables We will Require

String localekey = localkey (local);

String OriginalKey = MessageKey (LocaleKey, Key);

String messkey = null;

String message = null;

INT underscore = 0;

Boolean Addit = false; // add if not found under the original key

// Loop from specific to general Locales looking for this message

While (true) {

// load this Locale's Messages if We Have not Done So Yet

LoadLocale (LocaleKey);

// Check if We Have this key for the capital locale Key

MessageKey = MessageKey (LocaleKey, Key);

Synchronized (Messages) {

Messages.get (MSSIGEKEY);

IF (Message! = null) {

IF (address) {

Messages.Put (OriginalKey, Message);

}

Return (Message);

}

}

// strip trailing modifiers to try a more general local logle key

Addit = True;

Underscore = localeKey.lastIndexof ("_");

IF (underscore <0) {

Break;

}

LocaleKey = localeKey.substring (0, underscore);

// Try The Default Locale If The Current Locale Is Difance

IF (! DefaultLocale.equals (local) {

LocaleKey = localekey (DefaultLocale);

MessageKey = MessageKey (LocaleKey, Key);

LoadLocale (LocaleKey);

Synchronized (Messages) {

Messages.get (MSSIGEKEY);

IF (Message! = null) {

Messages.Put (OriginalKey, Message);

Return (Message);

}

}

}

// as a last resort, Try The Default Locale

=================================== This can be seen in the Struts finally assay LocaleKey

=================================== 这样 This resource file is $ filename.properties.

LocaleKey = "";

MessageKey = MessageKey (LocaleKey, Key);

============================================ ============================================================================================================================================================================================ 这个l

==================================================================================================================================================================================================================================================

Synchronized (Messages) {

Messages.get (MSSIGEKEY);

IF (Message! = null) {

Messages.Put (OriginalKey, Message);

Return (Message);

}

}

// Return An Appriate Error Indication

IF (ReturnNun) {

Return (NULL);

} else {

RETURN ("???" messagekey (local, key) "???");

}

}

As for the value of this $ locale, it is found in some code after a long time looks:

There is a method in the org.apache.struts.util.requestutils class, there is a method:

Public Static Locale GetUserLocale (httpservletRequest Request, String Locale) {

Locale UserLocale = NULL;

Httpsession session = request.getations (false);

IF (locale == null) {

Locale = globals.locale_key; // This value is org.apache.struts.Action.locale

}

// only Check Session IF sessions area enabled

IF (session! = null) {

UserLocale = (local) session.getattribute (local);

}

IF (userlocale == null) {

// Returns locale based on accept-language header or the server default

UserLocale = Request.getlocale ();

}

Return UserLocale;

}

It can be seen that Struts puts the instance object of LOCALE in the session, but when will he put this object in Session, not yet found

(Many configured and stored in the ActionServlet, because this class is the first class that started, but this class did not find the storage of Locale objects)

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

New Post(0)