[Java Web] Struts2 basic summary (1)
Struts2 environment configuration to import the jar package required by Struts2 create struts under the WEB-INF/classes (src) directory. the xml file is stored in the web. add Struts common configuration file struts to the xml file. xml: used to store Action ing relationships and set Struts configuration information web. xml: used to configure the Struts filter of the container. The general configuration is as follows:
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
Struts-default.xml: Struts2 default configuration information, it is generally not recommended to modify default. properties: Struts2 default settings information, also do not recommended to modify Action
The core function of Struts2 is generally composed of two parts: the Action ing of struts. xml and the Action entity class written in Java. In struts. xml, , And a series of tags to complete the Action ing, their properties are: package
Attribute |
Required? |
Description |
Name |
Yes |
The package name, which is used as a flag for other packages. |
Extends |
No |
Set this package to inherit from other packages |
Namespace |
No |
Set the package namespace |
Abstact |
No |
Set as abstract package |
Action
Attribute |
Required? |
Description |
Name |
Yes |
Name of the requested Action |
Class |
No |
Specific path of Action processing class |
Method |
No |
Specifies the method name in the Action. |
Converter |
No |
Type converter used by the specified Action |
Result
Attribute |
Required? |
Description |
Name |
No |
The view name returned by Action. The default value is success. |
Type |
No |
Return result type. The default value is dispatcher. |
A simple Action ing can be written:
/welcome.jsp
The Action object class must inherit the ActionSupport class and override its execute method (or other methods. If there is no parameter, return the String type and correspond to the method attribute in the action label ), and returns the string corresponding to the name attribute of the result tag. The object class corresponding to the above configuration is:
public class WelcomeAction extends ActionSupport {public String execute() {return SUCCESS;}}
Dynamic method call (DMI)
It is inconvenient to configure only one method for an Action in actual application. Struts2 supports specifying different methods for calling the Action class in the url, in the format of "Action name! Method Name ". Before using this function, add the following code to struts. xml to enable DMI:
Then, you can easily call it by adding a corresponding method with the return value of the String type in the Action class.
Wildcard (wildcard)
Struts2 supports wildcard operations to reduce the workload by using the agreed format when you need to configure a large number of actions. It uses "*" to replace words that are easy to change and then uses {n} to retrieve them, n indicates the nth wildcard. For example, a program requires four actions named DogAction, CatAction, BirdAction, and FishAction. If you follow the previous method, you need to configure four action ing relationships, but after using the wildcard, you only need the following action:
/pay.jsp
The wildcard has a wide range of scopes. The action name can change the result of method, class, and result. Frequent use can save a lot of time, but the format must be specified in advance.