1. The global logical controller is a filter that filters all requests. The API class referenced by this filter is
Org. Apache. struts2.disapatcher. filterdispatcher
2. MVC: JSP --> filter --> action --> business logic component --> business processing/Dao
3. Place the struts2 configuration file under classpath.
4. struts2 type conversion:
A. inherit defatypetypeconverter,
Overwrite the public object convertvalue (MAP context, object value, class totype) method of the parent class
B. inherit strutstypeconverter,
Overwrite the public object convertfromstring of the parent class (MAP context, string [] values, class toclass)
This method is used to convert the data transmitted from the page.
Overwrite the Public String converttostring (MAP context, object O) of the parent class)
This method converts the processed data in the action to a string in the corresponding format.
5. Verify struts2 data:
A. Use the encoding method for verification. The business controller action inherits the actionsupport class and overrides the public void validate () method.
Perform data verification in this method. If the business processing method in the action is test, you can write public void validatetest ()
To verify the data of the test method. (After performing the validatetest method, the system will then execute the validate method, and then
To execute the corresponding business. Code .)
If a serious error occurs, you can call addfielderror or the addactionerror method to add an error message.
B. Use the configuration XML file for verification. The verification file is named ×××action-validation. xml. Verification methods include field verification and non-field verification.
Field Verification indicates that some types of verification are performed on a field. Non-field verification indicates that a field is verified by a type of verification. The underlying implementation of the two methods is the same, but the performance is different. The field verification method is intuitive.
There are several types of verification: required, requiredstring, Int, date, double, expression, fieldexpression
Email, URL, visitor, conversion, stringlength, RegEx (regular expression ).
For the verification type, you can specify shourt-circuit as true to perform short-circuit verification.
If the method for executing the business in the action is test, you can verify the data of the test method by writing the ××action-test-validation.xml method. The default verification file ××action-test-validation.xml is also executed after the private authentication file for the test method is executed.
C. Verify the client using struts2. First, use the tag library of struts2, and the theme attribute of the tag library cannot be simple, and then set the tag
The validate attribute is true.
Note: The client authentication of struts2 depends on the server authentication configuration file.
6. struts2 interceptor. the interceptor of struts2 is the core of struts2, and its underlying implementation uses the reflection mechanism and dynamic proxy of Java. If an interceptor is introduced to an action in the struts2 configuration file, the default interceptor is not called and must be manually configured to the action.
method for implementing the struts2 Interceptor:
A. Implement the interceptor interface and implement the init, destrory, and intercept methods.
B. inherit the abstractinterceptor class and override the intercept method.
C. inherit from the methodfilterinterceptor class and overwrite the intercept method. This class can intercept specific methods.
the interceptor stack can contain the interceptor stack and the interceptor stack.
7. File upload and download:
A. Use the commons-fileupload and commons-io packages developed by Apache, and configure the fileupload interceptor and corresponding upload parameters as needed,
For example, the type of the file to be uploaded and the size of the file to be uploaded. For multi-File Upload, you can use JS Code to modify the list of uploaded files on the page. in action, you can use three lists to save the file object, file name (filename ), and the file type (filecontenttype ).
B. The file is downloaded and read using a stream: Return servletactioncontext. getservletcontext. getresourceasstream ("file name") and set the return class of the action result to stream, that is, the stream. Configure parameters as needed.
8. struts2 internationalization. The underlying implementation of struts2 internationalization uses the locale (region) and resourcebundle (Message Resource) classes in the Java basic class library.
Struts2 internationalization is mainly used in the following areas:
A. jsp page: When the struts2 tag is used, specify the key attribute of the tag (corresponding to the key in the message resource file ).
Use <s: Text name = "key">
<S: param> </S: param>
</S: Text>
Tag to obtain message resource information.
You can also use the <s: i18n name = "basename"> </S: i18n> label to specify a specific message resource file.
B. Action: Call the gettext (key) or gettext (Key, argS) method to obtain the message resource in the message resource file.
C. xml verification file: Specifies the key attribute of the Message Element (corresponding to the key in the message resource file ). (How to pass parameters ?)
D. During type conversion: view the xwork package's Source code You can find the following key-value pairs in the GE. properties file:
Xwork. Default. invalid. fieldvalue = Invalid Field Value for field "{0 }".
Re-specify the key-value pair in the message resource file.
In addition, setting the key-Value Pair struts. i18n. Encoding = GBK in struts. properties can solve Chinese garbled characters.
9. struts2 exception handling. in action, all our business logic processing methods declare the thrown exception. The specific exception corresponds to the specific processing result.
In Action Configuration:
You can also configure global exception handling: