Validate () method in actionform-Details

Source: Internet
Author: User

Write struts1.3.8 today. A problem occurred, which took me a long time to think about.

Login is written independently. loginform and loginaction are available for the login function. You can use validate () in loginform () form Verification (although it is better to use JS + Regular Expressions for form verification, it also provides a way of thinking ). Validate () Verification passed, the struts-config.xml will jump to the loginaction according to the type to execute the execute () method, otherwise turn to the error display page (the page specified by the input ).

This time I write the login and crud operations on the user in the same action (sharing an actionform, action, and struts-config.xml configuration block), this problem occurs, after the verification is successful, when performing a CRUD operation on a user, the user cannot jump to the specified page, but the user cannot jump to the error page. Later I found the error cause (The Validate () method used for login form verification during the CRUD operation will also be called, and the form is already null, so validate () cannot pass ), I also found some detailed explanations on the Internet. Share:

Similar to a map, actionerrors stores several actionmessage objects (actionerror is not recommended). Each actionmessage has a key, which is similar to a cookie, each actionmessage contains a small verification error message.

Actionerrors inherits from actionmessages, which encapsulates several error messages. It contains a method of add (string property, actionmessage message ).

The error message stored in actionmessage is a string and comes from the resource file. When an actionmessage is added to encapsulate exception information, the key is stored (the key and key value in the resource file are in the resource file ).

Struts provides the <HTML: errors/> label to display the error message of the entire actionerrors. However, you need to set <action... input = "Reg. jsp"> In the struts-config.xml (the input Property specifies the page to jump when a validation error occurs ).

If the actionerrors object is null or there is no actionmessage, the verification is successful --> jump to the action specified by type to execute the execute method.

If at least one actionmessage exists in the actionerrors object, the message is invalid --> jump to the JSP page specified by input.

<HTML: errors/> displays all error messages. You can use <HTML: errors property = "username"/> On the JSP page to configure and Display username error messages.

Steps for error display: <HTML: errors property = "username"/>
1 --> find errors. Add ("username", message); username in to get the corresponding message object
2 --> message = new actionmessage ("errors. Name"); the key is errors. Name.
3 --> find the value corresponding to errors. name in the resource file
4 --> the value is displayed on the error page.

I am a cainiao. I started to officially engage in struts. I started from the classic struts1 and then switched to struts2.

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.