1, there is a validate () method in Actionsupport, this method is a validation method, it will be executed before the Execute () method execution, so it can play a good role in validation.
The Validate () method in the @Override//override action is public
Void Validate () {
if null==this.username| | This.username.length () <4| | This.username.length () >6) {
this.addactionerror ("username invadate");
}
A, if the validation does not pass, we can call Addactionerror ("error message"), so that the error information is saved.
Validate the entire method is completed, the system will automatically check the name= "input" corresponding to the JSP page, general recommendations to jump to our registered page, that is, where to go back to
/registerresult.jsp
/register.jsp
b, and then we're in register.jsp.
That is, the initial registration page adds this struts2 tag
<s:actionerror cssstyle= "color:red"/>
If there is an error message, that is, the actionerror exists, the error message is output.
Also, we can set the CSS for the label
Note that the use of the struts2 tag must introduce a tag in the header file:
<%@ taglib prefix= "s" uri= "/struts-tags"%>
C
A method of comparing dates:
Brithday and graduate are date types
if (null!= birthday && null!= graduation)
{
Calendar C1 = calendar.getinstance ();
C1.settime (birthday);
Calendar C2 = calendar.getinstance ();
C2.settime (graduation);
if (!c1.before (C2))
{
this.addactionerror ("Birthday should be before graduation");
}
2, Action level and field level. In this way, we can add the error to the previous level of the different levels of added, prompt error messages can be more flexible, without adding all the information to the action level, all the information is displayed. For example, we want to use the error message of duplicate password in red font, and other information, such as user name, age and other information in green font, this time by using to different levels to add it.
On the registration page, just write a field-level label. As follows:
<s:actionerror cssstyle= "color:red"/>//action level
<s:fielderror cssstyle= "Color:blue" ></s: Fielderror>//field level public
Void Validate () {
if null==this.username| | This.username.length () <4| | This.username.length () >6) {
this.addactionerror ("username invadate");//Add error message
to action level This.addfielderror ("username", "username invadate in field");//Add error message elsewhere to field level
}
}
3, send the error, the original information also displayed in the form inside.
<s:form action= "Registeraction" >
<s:textfield name= "username" label= "username" ></s:textfield >
<s:password name= "password" label= "password" ></s:password>
<s:password name= " Repassword "label=" Repassword ></s:password>
<s:textfield name= ' age ' label= ' age ' ></s: textfield>
<s:textfield name= "Birthday" label= "brithday" ></s:textfield>
<s:textfield Name= "Graduate" label= "graduate" ></s:textfield>
<s:submit value= "Submit" ></s:submit>
</s:form>
Using the struts2 tag, you can automatically format and then display the wrong field level information, as shown below.
4, but this automatic use of table layout is convenient, but in many cases does not meet our needs, so we can use the custom layout method.
Method Two: The definition of typesetting is simple, so we can follow the HTML way to the layout of their own.
Field-level errors are not automatically displayed.
<s:form action= "registeraction" theme= "simple" ><br/> username:<s:textfield name= "username
" Label= "username" ></s:textfield><br/>
password:<s:password name= "password" label= "password" ></s:password><br/>
repassword:<s:password name= "Repassword" label= "Repassword" ></s: password><br/>
Age:<s:textfield name= "age" label= "age" ></s:textfield><br/>
Birthday:<s:textfield name= "Birthday" label= "brithday" ></s:textfield><br/>
graduate:<s: TextField name= "Graduate" label= "graduate" ></s:textfield><br/>
<s:submit value= "Submit" > </s:submit>
5, for security, struts in the absence of definition method, is submitted in the post way, this is more secure
6. If the value entered is not valid, for example, the age is type int, and the input is string type, this time the system will judge and add invalid field value for field ' age ' to the field level as follows:
Execution process:
1 The first type conversion
2) then perform the input-effectiveness (execute Validate method)
3 If there are any errors in the above procedure, the Execute method will not be executed, and the page will turn to the page where the action's name in Struts.xml corresponds to the result of input.
Implementation of the Addactionerror () method of the 8.ActionSupport class: First create a ArrayList object, and then add the error message to the ArrayList object.
9. When the call to the Getactionerrors () method returns a list of error messages at the action level, the return is actually a copy of the collection rather than the collection itself, so calling the clear () method on the copy of the collection still clears the elements in the replica, not the elements in the original collection. The contents of the original collection do not receive any effect at this time. In other words, the list of error messages at the action level is readable to the developer, but not writable
If you want to remove the error message after validate to execute the Execute method even if you have an error message, you can call the This.clearallactionerrors () or This.clearallfielderrors () method
10, the Fielderror level of error information is implemented through the LINKEDHASHMAP, the map key is String type, value is the list<string> type, which represents a field Name can correspond to multiple error messages, all of which are placed in the List<string> collection. To achieve the same error with multiple error messages
The above is a small set of struts to introduce the use of validate () input check method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!