The last chapter introduced struts2 The label of the core configuration file , This chapter mainly introduces struts2 Constant configuration file for 、 How constants are modified 、 Common constants and constant settings advanced —— Dynamic method call, etc ;



Struts2 Constant configuration of


1、struts2 Constant configuration of

struts2 Constant of refers to the auxiliary setting of the system running time , For example, the setting of character set 、action Class suffix, etc , Please see the notes below for details ;

1.1、struts2 Constant configuration file for

struts2 Default configuration file (default.properties) It's all in the file shown below :
default.properties file location
If you want to modify a constant without knowing the configuration information of the constant, you can go to this file to find it , The most important thing in this file is not constant configuration, but the explanation of constant configuration , So don't worry about what you don't understand ~ The file content is shown in the figure below :
default.properties File internal style
You can see that these configurations exist in the form of key value pairs ~


1.2、 How to modify the constant configuration file

There are three ways to modify constants , Namely :
【1】 Create a struts.properties How to file
【2】 Configure constants in the core configuration file
【3】 stay web.xml And configure

The following is a detailed introduction to the three modification methods :

1.2.1 Create a struts.properties How to file
stay src Create a folder named struts.properties The file of ( This file was created of type file), This name is fixed ;
struts.properties File creation
Paste the configuration key value pair to be modified copied in the default configuration file in the created file , For example, I want to modify the coded character set , The default is UTF-8 Of , Modified into GBK Of , Here's the picture :
 Copy information from default.properties Inside the document
In the default parameter profile default.properties Find the key value pair code of the coded character set inside the file and copy it ;
Then paste the copied code into the newly created struts.properties File and modify its configuration values , As shown below ;
struts.properties Modify configuration style in file
1.2.2、 Configure constants in the core configuration file
This way is to configure constants directly in the core configuration file , The method is as follows :
First , It's still the first one default.properties Find the configuration constant to modify inside the file , And then in struts.xml Create a tag named d The label of , There are two attributes in this tag , One is name, The other is value,name The property value of is the name of the configuration to be modified ,value Which configuration value to modify the property value of , The specific situation is demonstrated as follows :

<struts>
   <constant name="struts.i18n.encoding" value="UTF-8"></constant>
   <package name="" namespace="" extends="struts-">
	   <action name="" class="" method="">
		   <result name="">/</result>
	   </action>
   </package>
</struts>

 Configure constants in the core configuration file
Pay attention to the position of the label , Constant settings should be placed at the top ~

1.2.3、 stay web.xml Middle configuration struts2 Constant
The last way is in web.xml File to modify constant configuration , The modification method is shown in the figure below :
 stay web.xml File to modify the constant configuration style
stay web.xml Configure tags in , There are 、 These two labels , These two tags write the name and value of the attribute to be modified respectively , As shown in the figure above ;

1.3、 Constant modification summary

The most commonly used method is method 2 ( Configure in the core configuration file ), in addition , First configured first loaded , Configure the same property twice , The post configuration overlaps the previous configuration ( In the same file );


1.4、 Common constant configurations

Struts2 Many constants in the framework do not always need to be modified during development , Only individual ones need to be configured separately , Next is to introduce the constants that are often used in this part ;

Constant name Name value value value Interpretation of the
International coding settings struts.i18n.encoding UTF-8/GBK… Set internationalization code , The specific setting depends on the requirements of system development , By default, it is UTF-8, Configuring this constant can solve post Submit the problem of garbled code
action Suffix settings struts.action.extension Action,, /… Generally, the two English commas after the suffix can be empty , It's worth anything , For example, use sss、ccc Wait for , But once this happens, the access path in the browser must be in action Add your own suffix after the class name (pass: Due to blog post editing settings ,action The last two commas are in Chinese instead of , Please note when copying ~)
Developer mode switch struts.devMode true/false True: Open developer mode , In this mode, the core configuration file can be modified without system restart ( Hot load the core configuration file ), And it can output more error information when programming , Easy to debug during development ; After development, it is better to set this configuration to false, Turn off this function , Because this configuration will consume some resources ;
Call constant switch dynamically struts.enable.DynamicMethodInvocation true/false This setting is used in conjunction with dynamically calling methods ;

The configuration information is in struts2 Default constant profile for default.properties You can find it all in the library , And there are notes on it ;
The specific configuration code is as follows :
 Default common constants


1.5、 Constant loading order

Pass above struts2 You can see the way constants are defined , Constant configuration can be configured in multiple files ,sturts2 The framework will search and load in the following order of files :


pass: The next chapter is about struts2 Dynamic method calls in ~

《 End of this chapter 》