<Include/> label in struts. xml
When the system becomes largerStruts. xml
There is a lot of content, so we can useModular
Method
Distribute different configuration files in different configurations, and then load them in the same way by struts. xml.
For exampleStruts_1.xml
,Struts_2.xml
,Struts_3.xml
Three files are configured for different modules.
Finally, it can be integrated by struts. xml. The integration method is to include them.
<Include file = "struts_1.xml"/> <include file = "struts_2.xml"/> <include file = "struts_3.xml"/>
InStruts_1.xml
OrStruts_2.xml
AndStruts. xml
The configuration method in isExactly
Of
That is, they all haveRequired
XML Declaration
AndDTD
, And their DTD are the same. Next<Struts/> root element
That is, it cannot be consideredStruts_1.xml
If the file is contained, the XML declaration and DTD at the top are omitted. This is not acceptable.
Finally, Struts. XML is uniformly introduced, so the system loads the configurations such as struts_1.xml and struts_2.xml into the memory.
This is equivalent to modular configuration. For large and medium-sized systems, modular configuration is inevitable.
<Package name = "#"/> In struts. xml
In struts. XMLName
Can be named at will, without affecting anything else
Its name attribute only servesID <package/>
So that other <package/>Inheritance
It
<Package name = "struts-Default" abstract = "true">
AAbstract = "true"
Attribute
We found that this package declares the result type, interceptor, and so on, but it is not declared<Action/>
Configuration
HoweverStruts. xml
There are several <action/> configurations
ThereforeAbstract = "true"
Indicates that the current package is an abstract package, so it is not allowed to appear in the current package<Action/>
Configuration
Abstract packages are very similar to Java Abstract classes. It cannot be instantiated by itself. It can only be inherited, and several methods implemented by sub-classes are finally instantiated by sub-classes.
Therefore, the Struts-default package cannot contain any <action/> definition. The specific definition is defined by its Sub-package.
Meaning of extends in <package name = "struts2" extends = "struts-Default">
Available in struts2Package Mechanism
Differentiate different actionsSimilar functions
Put the action in onePackage
Medium
Similar to the Mechanism in Java,The difference is that the struts2 package supports multi-inheritance.
, As shown below
<Package name = "struts2" extends = "struts-default, jfreechart-Default">
In addition, it can be implemented between packages of struts2.Inheritance
Link
After inheritance, you can use everything in the inherited package.
So it is equivalent<Package name = "struts-Default"/>
All of these are inherited.
So you can directlyStruts-default.xml
All the result types and interceptors in the package are used by us.
If our package does not inherit the Struts-default package provided by struts2, a lot of work will become impossible.
When the struts2 application is startedAuto read struts-default.xml
To define everything in the file.Load to memory
Next, loadStruts. xml
File, and then read the information of the struts-default.xml in the memory
Then it will be added to our current application andDefault. Properties
Lines 180th and 181 have the following code:
### A list of configuration files automatically loaded by struts
Struts. configuration. Files = struts-default.xml, struts-plugin.xml, Struts. xml
Translation
: A list of configuration files automatically loaded by struts
The loading sequence of the application is as follows:Load struts-default.xml first
,Followed by struts-plugin.xml
,The most struts. xml
Therefore, the configuration information in the latter can overwrite the configuration information in the former.
<Package/> namespace attribute
If the namespace attribute of the <package/> label is not configuredNamespace = ""
Indicates that the current package is in the default namespace.
The namespace must startStart with a slash
Assume thatNamespace = "/hello"
And formAction = "login"
And then clickSubmit
Button
This will be displayed on the pageThe requested resource (/struts2/login) is not available
In this case, you can change the formAction = "/Hello/login"
Or use the namespace and Action attributes in the form to separate and write data.
That is<Form action = "login" namespace = "/Hello">
,Note that if login. Action is written, an error occurs.
After modifying the form action, it is displayed when you submit the form again.The requested resource (/struts2/Hello/login) is not available
This is because this path does not exist at all. The correct path should be/Struts2/Hello/login. Action
Therefore, After configuring the namespace of the package, you must manually add the action suffix, that isAction = "/Hello/login. Action"
If you submit it again, it will be displayed normally, and the browser will displayHttp: // 127.0.0.1: 8088/struts2/Hello/login. Action
In addition
: Assume thatAction = "/test/login. Action"
Request,Struts. xml
RootNo
The namespace isTest
Package
When struts2 finds that the namespace does not exist, it willDefault namespace
Search below<Action/>
Information
Assume that the message is sentAction = "/Hello/login. Action"
Request, when/Hello
If login. action does not exist in the namespace
Struts2 tries to go to the default namespace to find whether there is any <action/> information called login.
If no <action/> information with the name of login exists in the default namespace, an error cannot be found for the resource is reported.
Struts2 also supports the root namespace 【"/"]
When a direct requestContextpath
WhenRoot namespace
Search for matched actions
For example, requestHttp: // 127.0.0.1: 8088/project/BB. Action
Strtus2 will first"/"
Find the action in the namespace.
Server Root
YesHttp: // 127.0.0.1: 8088/
Root of the website
YesHttp: // 127.0.0.1: 8088/struts2_demo/
Hypothesis<A href = "/login. jsp"> </a>
, ThenActual point
YesHttp: // 127.0.0.1: 8088/login. jsp
That isRoot in JSP
Indicates thatServer Root
. WhileNamespace slash
RepresentativeWebsite Root
, Also calledProject Root
OrApplication Root
The name value of <action/> In struts. xml cannot start with a slash.
This is causedDefault. Properties
The attribute of row 109th is determined, that isStruts. Enable. slashesinactionnames
Default Value:False
For example, in struts. XML, the namespace attribute of <package/> is not specified and written<Action name = "Hello/login">
Then write in the formAction = "<% = request. getcontextpath () %>/Hello/login. Action"
And can still be accessed normally.
Note: You cannot write<Action name = "/Hello/login"/>
OtherwiseReport Error 404
In short, you can directly use the namespace. We do not recommend that you modify it.Struts. Enable. slashesinactionnames
Attribute, keep the default configuration.