Action and field level error handling in STRUTS2

Source: Internet
Author: User
Tags error handling

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.