Struts 2 configuration action details

After implementing the action processing class, you can Configure the action in XML to let the struts 2 framework know which action handles which request, that is, establish the corresponding relationship between user request and action class.

Action basic configuration

Struts 2 uses the package package to organize actions in struts In XML, the action is configured by using the action element under the package. When configuring an action, you need to specify the name and class attributes of the action element.

The basic configuration code of action is as follows:

Action is just a logic controller and does not directly generate any corresponding information for user requests. Therefore, after the action processes the user's request, it needs to present the specified view resources to the user, that is, when configuring the action, the mapping between logical view and physical view resources should be configured.

The mapping relationship between the configuration logical view and the physical view is defined through the < result > element. Each < result > element defines a mapping between the logical view and the physical view:

Dynamic method call

Sometimes an action needs to contain multiple control processing logic. For example, for the same form, when users submit through different submit buttons, the system needs to use different methods of action to process user requests. At this time, the action needs to contain multiple control processing logic.

The struts 2 framework allows one action to contain multiple processing logic. In struts 2, the way to request different processing logical methods in an action becomes DMI (dynamic method invocation). The request format is as follows:

Dynamic method call example

productList. jsp

In the above code, each commodity in the commodity list uses hyperlinks to edit and delete. The href attribute value in the hyperlink makes the link request by means of dynamic method call, and passes the product ID to the action as a parameter.

ProductAction. The Java code is as follows:

The above code creates two business methods, edit () and del (). When the user clicks different links, the system will hand them over to the corresponding method for processing.

Next, write edit JSP and del JSP page:

In struts The code for configuring productaction in XML is as follows:

The above configuration file configures the constant struts enable. The value of dynamicmethodinvocation is true so that struts 2 can start dynamic method invocation. Otherwise, dynamic method invocation will not be started by default.

Using the method attribute and wildcards

In addition to dynamic method calls, struts 2 also provides another processing method, that is, the action processing class is defined as multiple logical actions. At this time, when configuring the < action > element, you need to specify the name, class and method attributes. This allows the action to call the specified method instead of the execute () method to process the user request.

For example, the productaction class can be defined as two logical actions, that is, the edit () and del () methods in the class can be mapped into different actions. The example code is as follows:

The above code defines two logical actions: editproduct and delproduct. The processing classes corresponding to these two actions are productaction, but the processing logic is different. The corresponding methods are edit () and del ().

Although the above method can be implemented, most of the two definitions are the same, resulting in redundancy. Struts 2 also provides a wildcard "*" to solve this problem. Use wildcards to use the pattern string when defining the name attribute of the action (that is, use "*" to represent one or more arbitrary strings). Next, you can use the form of {n} in the class, method attribute and < result > sub elements to represent the sub string matched by the previous nth asterisk "*".

*Wildcard

The name attribute value of the action in the above code is "* product", and wildcards are used. At this time, instead of an ordinary action, a series of logical actions are defined. As long as the URL requested by the user conforms to the pattern of "* product. Action", it can be processed through productaction. In addition, you must specify the method attribute, which specifies the method requested by the user. Use the expression {1} in the method attribute, which means that the expression is the value referred to by the first "*" in the name attribute value. Through the above configuration rules, the same operation effect as dynamic call can be achieved.

In addition, struts 2 allows expressions to be used in both the class attribute and the method attribute, for example:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>