Struts2 key knowledge (notes)

Source: Internet
Author: User

1. Execution Process of strtus2:

The form data on the page is submitted to name. Action:

Struts2 filter (Org. apache. struts2.dispatcher. ng. filter. strutsprepareandexecutefilter) Intercepts requests and finds struts. for the action element in the XML file, the name attribute value of the action element corresponds to the Action submitted by the form, and the class attribute value corresponding to the class attribute value of the action element is instantiated, set the value submitted by Form to the class attribute through setxxx.

Call the execute method of the action class. This method must return a string! Based on the value corresponding to the name attribute name of the returned string in the result sub-element of the Action element, if yes, the page specified by the result element value is returned.

Generally, each action class inherits the com. opensynphony. xwork2.actionsupport class.


2. For a custom type converter, three types of information are required: The action name, the attribute name to be converted in the action, and the type converter corresponding to this attribute. The action name is obtained through the attribute file (the same as the action package), and the attribute name to be converted in the action is obtained through the key value in the attribute file, the type converter corresponding to this attribute is obtained by the value corresponding to the key value.

3. Use the struts2 built-in type converter strutstypeconverter to simplify type conversion. Strutstypeconverter inherits from defatypetypeconverter. Provides two Abstract METHODS: convertfromstring and convertostring, which are used to convert a page to a background object and a background object to a string.


4. Global type conversion. The property file is under the SRC root directory and the property file name is xwork-conversion.properties. Class to be converted = converter name



Struts2 Verification:

Encoding Method 1: implement the validate method in action. This method is empty in the actionsupport class. You must inherit the implementation method by yourself. After defining this method, the Framework automatically runs the validate method first, then execute the execute method.


Struts first performs data verification after type conversion. If the execute method is not executed in the above process, the page will switch to struts. in XML, the page specified by the Action element name input result (usually the input page ). If an error occurs during type conversion, put the error in fielderror instead of actionerror!


Implementation of the addactionerror () method of the actionsupport class: first create an arraylist object and add the error message to the arraylist.

When the getactionerrors () method is called to return an action-level error information list, a copy of the set is actually returned. Therefore, the clear () method cannot be called to clear the error information of the original set! Action-level error messages are read-only for developers.


Field-level error information is implemented using javashashmap at the underlying layer. The key value of the map is string, the value is a list <string>, and a fieldname can correspond to multiple error messages.


To clear the error message, call clearactionerrors (), clearfielderrors (), and clearerrors ()


For custom methods specified by the method attribute of the action, the corresponding custom input verification method is validatemyexecute () (assume that the custom method is named myexecute). The underlying layer is called through reflection.

When the execute method is specified in the action, the custom validate method is called first, and then the validate method is called. Any error occurs during the verification process and the execute method is not executed.


Custom field-level error message 1: Create an attribute file named after action. 2. Specify the error message invalid. fieldvalue. fieldname = Error MSG for each error field in the property file.



The validation framework of struts2 (a valid XML file) is divided into the field optimization checksum and the validators give priority to verification.


Naming rules for international resource files. Package _ language name (lower case) _ country name (upper case)


Sequence of struts2 framework verification execution

1. First, execute the verification framework. 2. Execute the validation method of the custom method.

3. Execute the validate method.


For the result configuration of the Struts. xml file, the local configuration is better than the global configuration.

We can either define exceptions and results in actions, or define global exceptions and results. Local exceptions always take precedence over global ones. If they are defined as global, it can be shared by all actions, while local exceptions and results can only be exclusive to the current action and cannot be used by other actions.


The model driver of sturts2 is compared with the property DRIVER:

1. Attribute drivers are flexible, accurate, and model drivers are not flexible. In many cases, parameters submitted on pages are not attributes in the model.

2. The model driver is more in line with the object-oriented programming style, so that we get an object instead of a discrete value.


Server-side code unit testing has two modes:

1. In-container testing (jetty)

2. Mock testing (inheriting servlet APIs such as httpservletrequest, httpsession, and httpservletresponse)


The preparable interface is used to initialize the action, which is executed in the prepare method!


Methods To prevent form resubmit:

1. Redirect

2. Through the session token (Session token), when the client requests the page, the server generates a random number through the token tag, places the random number in the session, and then sends the random number to the client, when the client sends a request, the server receives the random number and compares it with the random number saved in the session. In this case, the two are the same, and the server considers it the first time to submit and update the random number, if the client re-submits the request, the client sends the server to the previous random number, and the server's random number has changed. The two are different. This server considers the request to be submitted repeatedly, so the request is unsuccessful, then switch to invalid. the result page to which the Token points.


Interceptor: is the core of sturts,

1. Write a class that implements the interceptor Interface

2. Define the interceptor in the Struts. xml file.

2. Use interceptor in action


Once you have defined your interceptor and configured it to the action, you must add the default interceptor stack at the end of the action. Otherwise, the default interceptor will become invalid.


Defining a rotten machine can directly inherit the abstractinterceptor abstract class (this class implements the interceptor interface and implements empty implementations of the init and destroy methods) and then implements its abstract method.


Method Interceptor (you can filter and intercept methods)


In the method filter interceptor, if neither the includemethods parameter nor execludemethods is specified, all methods will be blocked. If only the includemethods parameter is specified, only the contained methods are blocked.


The abstract attribute of the package element indicates that the package is abstract and cannot be used directly. It must be inherited by a sub-package. For example, Struts-default package.


The namespace attribute of the package element is used to separate namespaces. Generally, the namespace value is set to the directory name.


File Upload:

When uploading a file, you must set the method attribute of the form to post and enctype attribute to multipart/form-data.


Struts2 file upload: first, save the File Uploaded by the client to struts. multipart. in the directory specified by the savedir key, if the directory corresponding to the key does not exist, save it to javax. servlet. context. the directory specified by the tempdir environment variable.

The file variable defined in action actually points to a temporary file in the temporary directory, and then the server writes it to the specified directory through Io.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.