In Struts2, the general action inherits actionsupport this class and can override the public void validate () for data validation. There are two commonly used methods corresponding to the hint information are this.addfielderror ("field name", "error message"); and This.addactionerror ("error message"); two methods.
Since the class implements the Validationaware interface in Actionsupport, the interface declares the two methods:
1public void Addactionerror (String anerrormessage) {
2 Validationaware.addactionerror (anerrormessage);
3}
4
5 public void Addactionmessage (String amessage) {
6 validationaware.addactionmessage (amessage);
7}
The Validationawaresupport implements the Validationaware interface by looking at two error tips in the Validationawaresupport source code:
Private Collection actionerrors;
Private Map fielderrors;
1public synchronized void Addfielderror (String fieldName, String errormessage) {
2 final Map errors = Internalgetfielderrors ();
3 List thisfielderrors = (list) errors.get (fieldName);
4
5 if (thisfielderrors = null) {
6 thisfielderrors = new ArrayList ();
7 Errors.put (FieldName, thisfielderrors);
8}
9
Thisfielderrors.add (errormessage);
11}
1public synchronized void Addactionerror (String anerrormessage) {
2 internalgetactionerrors (). Add (Anerrormessage);
3}
4
Note: Because the fielderrors level is a map type, this.addfielderror ("Field name", "error message"); , and the Actionerror level is collection level, so This.addactionerror ("error message");
If the field and action error levels are used at the same time in the actual project development application, the form in the JSP page is useful to the STRUTS2 label to implement, it is possible for the same form, Both the filed level error prompt and the action level error prompt, you can choose according to the actual needs of the project, but if all two must exist at the same time, only show the action level error, and do not show the field level action, Because the STRUTS2 label defaults to display field-level error, so to modify the template, add Theme= "simple", then the Struts2 form that label defaults to display field error prompt invalidation, The benefits of using this template can also be as casual as normal HTML design layout. For example: <s:form action= "registeraction" theme= "simple" >
PS: To display the action level error prompts, you need to include <s:actionerror/> in the form in the JSP page.