Struts2 Two classic methods for validating error messages-addfielderror&addactionerror

Source: Internet
Author: User

Both of these methods are methods implemented by the Actionsupport class, and in the application we will inherit the Actionsupport class, implementing a HasErrors () method in the Actionsupport class that will determine if there are two levels ( Action and field level), so when we need to make an error, we can apply it conveniently:

try {

Xxx

} catch (Exception e) {

Super.addfielderror ("username", "input error");

Or:

Super.addactionerror ("incorrect input");

}

Interested can study the source code, when used we only need to display these errors on the page OK.

Such as:

<s:fielderror>

<s:param>username</s:param>

</s:fielderror>

Or

<s:actionerror/>

But in practice, fielderror we tend to have a targeted error message output, because Fielderror itself is a map that can specify the wrong field in detail-the reason. And it is more appropriate when the form is submitted, as long as the field that corresponds to the error field and the submission property. STRUTS2 will automatically format the table for us (no test)

Modified topic: <s:form theme= "Simple" > can not display error messages, cannot automatically add table layout, as needed to play

Note: We only need to set Error the default input , Struts2 will automatically do the view forwarding for us, <result name= "input" >/input.jsp</result>

Struts2 Two classic methods for validating error messages-addfielderror&addactionerror

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.