[Java framework] Struts2 (2)-Struts2 configuration

1. Details of Struts2 configuration items

1.1. Import external xml file

Use the <include> tag in the struts.xml file, and import an external .xml file on the file attribute.

 

 

 

Another action is separated in the example.xml configuration, and the package is generally different. details as follows:

The purpose of this is to split too many actions and messy package in struts.xml.

1.2. Package configuration

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd" > 
< struts > 
    <!- package: package, the role of the package is for modular management;
        name: represents the package name (must be written and unique), the name is unique in the entire configuration (including other xml);
        namespace: represents the path at the packet level,
        External access path: http: // ip address <: port> / <context path> / namespace / action name
        extends (inherited): it inherits from a parent package, the name of this parent package is called struts-default
        abstract: represents that the current package is an abstract package (that is used to let others inherit, there is such a package in struts-default.xml- > 
  < package name = "default" namespace = "/ extra" extends = "struts -default " > 
        < action name =" index " class =" cn.yif.action.UserAction " method =" execute " > 
            < result name =" success " type =" dispatcher " >
               /success.jsp
            </result>
        </action>
  </package>
</struts>

The specific access request is as follows:

note:

①    The action names under the same package cannot be repeated, and the actions under different packages can be repeated;

②    The name of the package cannot be repeated;

③    The namespace of the package cannot be repeated.

 

1.3. The search process of package

Corresponding to the struts.xml configuration, the access path is mainly on packageNameSpace and actionName, as follows: http: // localhost / system / abc / hello access as an example, here will divide the url into two parts,

packageNameSpace:/system/abc,actionName:/hello。

Here we will first search for the corresponding <package> tag in the configuration file through packageNameSpace. If it is not found, the path will be lowered by one level, that is, / system. If it is found, then the corresponding <action> will be found in the package. No matter whether the corresponding <action> is found or not, it will not be searched again.

Finally find "/", here is also a package, which means that the root package will continue to search in the root package.

Note: In the above process, if no corresponding <action> is found in all packages, then it will be searched in the default package <package namespace = "">

The analysis of the query process is illustrated as follows:

Now my path on the browser is: / path1 / path2 / path3 / hello

We need to think of this as two parts:

Package path part: / path1 / path2 / path3

Action part: hello                                                            

When searching, first find the path:

 / path1 / path2 / path3 If you don't find it, you will find / path1 / path2

 / path1 / path2 will find / path1 if not found

 / path1 will find the root package if not found /

Note: When the path part is not found, it will go to the upper path. If it is found, it will not look up again; find the Action after the path is found; Action is used directly, if it is not found, it will try to find the default in the default default package Action (in the default.xml file) .

namespace = "/": represents a root package;

namespace = "": represents a default package;

1.4. Struts2 configuration file priority

The Struts2 framework loads struts2 configuration in the following order
default.properties This file is saved in the org.apache.struts2 package in struts2-core-2.3.7.jar
Many constants in our configuration are inside
struts-default.xml The file is saved in struts2-core-2.3.7.jar
 At the stage of our learning, you can come here to view many configuration methods
struts-plugin.xml This file is saved in struts-Xxx-2.3.7.jar There are some plug-ins
The above three configuration files are the default configuration files of Struts2, we cannot modify it
struts.xml This file is the default struts configuration file for web applications
struts.properties This file is the default configuration file of Struts (generally not written here)
web.xml This file is the configuration file of the web application (generally not needed)
If multiple files are configured with the same struts2 constant, the constant value configured in the latter file will override the constant value configured in the previous file

The preceding three jar packages are not allowed to be modified. The latter three can be modified by yourself.

The priority of the configuration file: the priority is increased in order from top to bottom, and the higher the priority, the lower the priority .

1.5. Struts2 constant configuration

You can find our constant configuration in default.properties.

Constant configuration commonly used in Struts2.xml file:

<!- Development mode; modify the configuration file without restarting the server- > 
<!- Refers to struts.xml, other configuration file modifications still need to be restarted- > 
<!- Automatically reload the configuration file, not Will definitely succeed. -> 
<!- struts.devMode = true; -> 
< constant name = "struts.devMode" value = "true"  /> 
<!-The system uses encoding- > 
< constant name = "struts.i18n .encoding " value =" UTF-8 "  /> 
<!- upload file size (byte) -> 
< constant name =" struts.multipart.maxSize " value =" 2097152 " 
-->
<constant name="struts.action.extension" value="action,do,," />

1.6. Struts2 default configuration

① The default class in <action> is inherited from the <default-class-ref class = "com.opensymphony.xwork2.ActionSupport" /> in the struts-default package, and the configuration is not written by default in practice;

② The default configuration of method in <action> is execute ;

③ The default configuration of the name in the <result> tag is success ;

④ The default configuration of type in <result> tag is dispather ;

< package name = "default" namespace = "/" extends = "struts-default" > 
<!- Which configuration in action is the default configuration, can it not be written?
   method can not be written, the default is execute 
   Class can not be written, the default is to find (this can be changed by ourselves)
   <default-class-ref class = "com.opensymphony.xwork2.ActionSupport" /> can be found in struts-default
    Can name not be written? This is required
    Which configuration in the result is the default configuration, can it not be written?
    name: can not be written, the default is success
    type: can not be written, the default type is dispatcher (we can also change it) -> 
< action name = "const" > 
    < result > /success.jsp </ result > 
</ action > 
</ package >

Guess you like

Origin www.cnblogs.com/yif0118/p/12688905.html