Struts 2 -- detailed explanation of struts.xml configuration file

Posted by ronniebrown on Mon, 06 Dec 2021 20:22:31 +0100


Struts.xml is the core configuration file of struts 2 framework, which is mainly used to configure the correspondence between Action and request, as well as logical view and physical view (logical view is the element configured in struts.xml file, and its name attribute value is the logical view name; physical view refers to the result page configured in the element, such as JSP file) Resource correspondence.

The struts.xml file is usually placed in the WEB-INF/classes directory of the Web application. The struts.xml file in this directory can be automatically loaded by the struts 2 framework. Since the MyEclipse development tool will automatically compile the struts.xml file under the src path and put it under the WEB-INF/classes path, it can be directly put under the src path of the project.

1, struts.xml file structure

Let's take a look at a typical struts.xml file structure, as follows:

Here are some inline snippets.

<?xml version="1.0" encoding="UTF-8" ?>
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    <!--<constant>Configuration of constants for element-->
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <!--<package>Element for package configuration-->
    <package name="default" namespace="/" extends="struts-default">
        <!--to configure Action-->
        <action name="index" class="Xxx"/>
            <!--to configure Result-->
            <result type="dispatcher">
                <param name="location">/index.jsp</param>
    <!-- <include>Element is used to contain the configuration -->
    <include file="example.xml"/>

In the above documents, the elements are described as follows:

<struts> Element is the root element of the file, and all other elements are placed in the <struts></struts> Yes.
<constant> Element is used for constant configuration.
<package> Element is used for package configuration Struts2 In the framework, packages are used to organize Action And interceptors, each packet is composed of zero or more interceptors and Action A collection of.
<include> Element is used in a struts.xml The configuration file contains other configuration files.

Next, these common elements and their configurations will be explained in detail.

2, Constant configuration

In projects developed using the struts 2 framework, a large number of constants are usually required. Most of these constants have been configured in the default configuration file, but the development requirements are different due to different user needs. At this time, these constant values may need to be modified. The modification method is to reconfigure the constants in the configuration file.

In struts 2, constants are usually configured in the following three ways.

stay struts.xml Used in files <constant> Element configuration constants (commonly used).
stay Configure constants in the file.
stay web.xml Adopted in the document <init-param> Element configuration constants.

Among the above three configuration methods, configuring constants through elements in struts.xml file is the most commonly used method in project development, and the constant configuration in the tutorial is also realized in this way. Therefore, only the method of configuring constants through elements is introduced here, and the readers of the other two methods only need to understand.

When configuring constants through the < constant... / > element in struts.xml file, you need to specify two required attributes name and value. The name attribute is used to specify the constant name of the constant, and the value attribute is used to specify the constant value of the constant. The example code of its configuration is as follows:

        <!--Set the default encoding set to UTF-8-->
        <constant name="struts.il8n.encoding" value="UTF-8" />
        <!--Set use development mode-->
        <constant name="struts.devMode" value="true" />

In the above example code, the constants struts.i18n.encoding and struts.devMode are configured. Struts.i18n.encoding is used to specify that the default encoding set of the struts 2 application is UTF-8, and struts.devMode is used to specify that the project uses the development mode.

There are many constants supported by struts 2. There is a file in the org / Apache / struts 2 path of the struts 2-core-2.3.37.jar compressed file, which specifies default values for all constants of struts 2. Readers can learn about the constants supported by struts 2 by viewing this file.

In addition to the above three ways to configure constants, constants are also configured in some configuration files built in struts 2. Therefore, if the same struts 2 constant is configured in multiple files, the struts 2 framework loads constants in a certain order. The usual search order is as follows.

  1. this file is saved in the org.apache.struts2 package in struts2-core-2.3.37.jar.

  2. struts-default.xml: this file is saved in the struts 2-core-2.3.37.jar file.

  3. struts-plugin.xml: this file is saved in struts-Xxx-2.3.37.jar and other struts 2 plug-in JAR packages.

  4. struts.xml: this file is the struts 2 configuration file of the Web application itself.

  5. this file is the default struts 2 configuration file for Web applications.

  6. web.xml: this file is the configuration file of the web application.

The above specifies the order in which the struts 2 framework searches for struts 2 constants. It should be noted that if the same struts 2 constant is configured in multiple files, the constant value configured in the latter file will overwrite the constant value configured in the previous file.

3, Package configuration

In the struts 2 framework, packages are used to manage actions and interceptors. Each package is a collection of multiple actions, interceptors, and interceptor references. In the struts.xml file, use elements to define package configuration. Each element defines a package configuration. The example code is shown below:

<package name="default" namespace="/" extends="struts-default">

In the above code, the element uses three key attributes: name, namespace and extends. The details are as follows.

name: Required attribute, used to specify the name of the package (the name must be unique in the configuration file), which is referenced by other packages Key. 
namespace: Optional attribute that defines the namespace of the package.
extends: Optional property that specifies that the package inherits from other packages. Its property value must be the value of another package name Property value, but the property value is usually set to struts-default´╝îIn this way, the Action You have Struts2 The default interceptor and other functions of the framework.

4, Include configuration

Struts 2 allows to decompose a configuration file into multiple configuration files, so as to improve the readability of the configuration file. The element is used to include other configuration files in a struts.xml configuration file. Struts 2 only loads the struts.xml file under WEB-INF/classes by default. Once the Action is configured through multiple XML files, other configuration files must be included through the element.

The sample code of configuration in struts.xml file is as follows:

        <!--No path is specified. The default path is src Down time mode-->
        <include file="struts-post.xml"/>
        <include file="struts-user.xml"/>
        <include file="struts-dept.xml"/>
        <!--How the configuration file is in a specific package-->
        <include file="com/mengma/action/struts-product.xml"/>

In the above code, the struts.xml file contains four configuration files through the element, and the file attribute is used to specify the name of the included configuration file. If the included configuration file is under the src path, you can directly specify the file name. If the included configuration file is in a specific package, you need to import the package path of the included file.

It should be noted that each included configuration file is a standard struts 2 configuration file, which also contains DTD information, root element of struts 2 configuration file and other information. Because the struts.xml file contains other configuration files, all configuration information will be loaded when the struts.xml file is automatically loaded by the struts 2 framework.

Topics: Struts struts2