Struts Source Research

Source: Internet
Author: User

One of the most common tags in struts is this:

<bean:message key= "Welcome.title"/>

As we all know, this label does things like this:

Access to resource files defined in Struts-config.xml, typically application.properties, is generally defined as:

<message-resources parameter= "Resources.application"/>

Based on the above definition, struts will go to web-inf/classes/resource/ Go down and find Application.properties file, this is from the above configuration information on the surface looks like this, but by looking at the struts source, you can see the following code: in Org.apache.struts.util.PropertyMessageResources Class, like the following code:

Through this code, you can see

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

B, struts is not simply to find application.properties files, but first find application, assigned to the name variable and then add the underscore "_", and then add Localekey (such as Zh,en), And then Add. properties

C, after confirming the filename, struts used the ClassLoader class getResourceAsStream method to get a InputStream

D, then struts uses the load method of the Java.util.Properties class to read all the resources in the resource file and put it in a hashmap.

E, then struts can take out different message to the foreground according to the key value

The Set up to load the property resource for this locale key, if we can
String name = Config.replace ('. ', '/');
if (localekey.length () > 0) {
Name + + "_" + Localekey;
}
Name = = ". Properties";
InputStream is = null;
Properties Props = new properties ();
Load the specified property resource
if (log.istraceenabled ()) {
Log.trace ("Loading resource '" + name + "");
}
ClassLoader ClassLoader = Thread.CurrentThread (). Getcontextclassloader ();
if (ClassLoader = = null) {
ClassLoader = This.getclass (). getClassLoader ();
}
is = Classloader.getresourceasstream (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);
}
}
}

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.