1. There is a validate() method in ActionSupport. This method is a verification method. It will be executed before the execute() method is executed, so it can play a good role in verification.
@Override //Rewrite the validate() method in Action public void validate() {if(null==this.username||this.username.length()<4||this.username.length()>6){this.addActionError("username invadate");}} a. If the verification fails, we can call addActionError("Error Message"); in this way, the error message will be saved.
After the entire validate method is executed, the system will automatically check the jsp page corresponding to name="input". It is generally recommended to jump to the page we registered, that is, where we come from and where we go
/registerResult.jsp/register.jsp
b. Then we are register.jsp
That is, add this struts2 tag to the initial registration page
<s:actionerror cssStyle="color:red"/>
The meaning is: if there is an error message, that is, actionerror exists, then output this error message.
Moreover, we can set the tags to css
//Note that using struts2 tags must introduce tags in the header file: <%@ taglib prefix="s" uri="/struts-tags" %>
c.
A way to compare 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 to different levels when adding errors, and we can be more flexible when prompting error messages, without adding all information to the Action level, all information will be displayed the same. For example: We need to use red fonts to represent the error message of repeated password errors, while other information, such as username, age, etc., are represented in green fonts. At this time, you can add it to different levels by using it.
On the registration page, just write a Field-level tag. 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 an error message to the Action level this.addFieldError("username", "username invadate in field");//Add an error message to the field level}}3. After sending an error, the original information will be displayed in the form.
<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, it can automatically type and then display the wrong Field level information as shown below.
4. However, although this method of automatically using tables to type out is convenient, it does not meet our needs in many cases, so we can use custom typesetting.
Method 2: Define the layout method to simple, so that we can type it ourselves in the HTML method.
Field-level errors will not be 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></s:form>
5. For security, struts is submitted in post when no method is defined, which is safer.
6. If the input value does not conform to the method, for example, age is of int type and input is of String type, the system will judge and add Invalid field value for field "age" to the Field level. The information is as follows:
Execution process:
1) First perform type conversion
2) Then perform the input validation (execute the validate method)
3) If any error occurs in the above process, the execute method will not be executed again. The page will turn to the page in struts.xml whose name is the result corresponding to the input.
8. Implementation of the addActionError() method of ActionSupport class: First create an ArrayList object, and then add an error message to the ArrayList object.
9. When the getActionErrors() method is called to return an Action-level error message list, the returned is actually a copy of the collection rather than the collection itself. Therefore, the elements in the copy are still cleared by calling the clear() method on the collection copy, rather than the elements in the original collection. At this time, the content in the original collection has not received any impact. In other words, Action-level error message lists are readable to developers, but not writable
If you want to delete the error message after validate and let it execute the execute method even if there is an error message, you can call this.clearAllActionErrors() or this.clearAllFieldErrors() method
10. The underlying error message at FieldError level is implemented through LinkedHashMap. The key of the Map is String type and the value is List<String> type. This means that a Field Name can correspond to multiple error messages, and these error messages are placed in the List<String> collection. Thus, there are multiple error messages to achieve the same error
The above is a detailed explanation of the verification method used to input validate() in Struts introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!