Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of initializing the property resource file by Struts

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how Struts initializes the attribute resource file, and the content is very detailed. Interested friends can refer to it and hope it will be helpful to you.

Resource files, whether they are used within the strus architecture or user-defined resource files, are resource files. Resource files are used for two reasons:

One aspect is that the system is implemented through configuration, so it will have better scalability and flexibility.

On the other hand, internationalization can be achieved.

So we may have one or more resource profiles.

So how exactly is the resource file initialized?

Internal and external resource files are initialized separately, but different initialization methods are used. The internal resource file is initialized directly by init () calling initInternal (), while the user's resource file is implemented through the configuration module.

How are internal resource files initialized?

Protected void initInternal () throws ServletException {try {internal = MessageResources.getMessageResources (internalName);} catch (MissingResourceException e) {

The above method implements the initialization of internal resources.

In fact, it is very simple, through the resource file location, and then returned as a MessageResources object on the OK.

That is, the internal variable, through which you can directly take the value of the attribute later.

So you might ask, how does getMessageResources (internalName) achieve access to resource files?

Let's follow it again:

Public synchronized static MessageResources getMessageResources (String config) {

Obviously, factory mode is also used in this. Then the factory generates a resource MessageResources.

We can understand that all resources actually belong to the same category of products, so they use the same factory.

In the initialization plant section above, we can actually see that MessageResourcesFactory is an abstract class, and its abstract method is createResources (config).

So who is his realization? You should ask our factory, because only when you know the factory will you know the products.

So we need to know what factory we initialized.

So let's take another look:

Public static MessageResourcesFactory createFactory () {

In fact, the factory is decided by factoryClass.

This is in

Protected static String factoryClass = "org.apache.struts.util.PropertyMessageResourcesFactory"

It's defined.

So we can see that PropertyMessageResourcesFactory actually initializes the * .properties file.

Where is the result after initialization? How to use it?

The result of the initialization of the internal resource file is saved on the internal variable as an instance of MessageResources. It is defined as follows:

Protected MessageResources internal = null

So only struts can be used internally, so we can't call it.

How is the user's resource file initialized?

The user's resource file is realized through the configuration module ModueConfig. We have initialized a ModuleConfig above. So we can

To initialize our resource files.

InitModuleMessageResources (moduleConfig)

The internal division of labor in struts is also very obvious.

ModuleConfig is used to manage the configuration of different modules.

In fact, each module managed by him also has its own configuration.

The configuration of the property resource file is MessageResourcesConfig

Instances of this configuration are managed by ModuleConfig, and all instances of ModuleConfig can get instances of all properties file configurations.

In fact, every properties file will correspond to a MessageResourcesConfig.

So we can understand some of the code below.

Protected void initModuleMessageResources (ModuleConfig config) throws ServletException {

This place gets the name of the class that is the factory. Such as:

"org.apache.struts.util.PropertyMessageResourcesFactory"

By comparing this with the previous one, we can see that as long as it is a property file, we use this factory.

String factory = mrcs [I] .getFactory ()

Once you know which factory to use, you can instantiate and create a factory.

MessageResourcesFactory.setFactoryClass (factory); MessageResourcesFactory factoryObject = MessageResourcesFactory.createFactory (); factoryObject.setConfig (mrcs [I])

A MessageResources is produced by the factory

MessageResources resources = factoryObject.createResources (mrcs [I]. GetParameter ()); resources.setReturnNull (mrcs [I]. GetNull ()); resources.setEscape (mrcs [I]. IsEscape ()); save the parsed results in context.

}

At this point, the process of how strtus initializes the resource file is complete. We now know the process by which struts initializes the properties resource file.

The final result of parsing is saved in context in the form of MessageResources.

On the Struts initialization properties resource file is how the process is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report