There are two types of data verification, one is the input validity verification of data, and can be verified in the Validate method of ActionForm; the other is the logic validity verification of the data, and verification can be performed in the action.
First, explain how to input validity verification on how to input data input in ActionForm:
1> First configure the verification result information of the illegal data, you can configure it in the Properties file, for example: enter the information in Application.properties:
Error1 = Username is NULL
Error2 = Username.length = 0
Error3 = Username.length> 0
2> Declare this file in Struts-Config.xml:
......
..............
Struts-Config>
3> Inheriting the Validate method of the parent class in ActionForm, checking if it is not legal, then returns the original page:
Public ActionerRors Validate (ActionMApping ActionMapping, Javax.Servlet.http.httpservletRequest HttpservletRequest) {
ActionerroS Errors = new actionerrors ();
IF ((username == null) {
Errors.Add ("UserName", New ActionMessage ("Error1"));
Return Errors;
Else IF ((UserName.length () <1)) {
Errors.Add ("Username", New ActionMessage ("Error2"));
Return Errors;
Else IF ((Username.length ()> = 1)) {
Errors.Add ("UserName", New ActionMessage ("Error3"));
Return Errors;
}
Return Errors;
}
4> Display error message on the front page:
html: form>
If you need to output all the error messages in Errors to the front desk, call
If you only need to output a specified error message to the front page, call
5> When verification of the validity of input data, there is a matter of attention:
i> When configuring an action in Struts-Config.sml, you need to set the validate to True;
II> When configuring an action in Struts-Config.sml, you need to specify the return to the page: Input = "/ struts / login.jsp"
III> Properties files are placed in ClassPath, for example, put to /com/xxxx.properties, need to specify the path in Struts-Config.xml:
,
in
Action
Logic validity verification of data in data
,
Need to use hard coding to implement
,
There is no more