Rewrite the validate method verification form in actionform in struts.

Source: Internet
Author: User
Struts Rewrite Actionform Medium Validate Method Verification Form
To verify the function, rewrite the verification function in org. Apache. Struts. Action. actionform.
There are two verification form functions: Public actionerrors validate (actionmapping mapping, javax. servlet. servletrequest request) and Public actionerrors validate (actionmapping mapping, javax. servlet. HTTP. httpservletrequest request) We need to rewrite the latter: The following public actionerrors validate (actionmapping mapping, httpservletrequest request) {actionerrors error = new actionerrors (); if (username = NULL | "". equals (username) {error. add ("nameerror ", New actionmessage ("JSP. login. name. error ");} If (Password = NULL | "". equals (password) {error. add ("passworderror", new actionmessage ("JSP. login. password. error ");} return error;} error in this method. the first parameter in add is the name of the error you want to use on a JSP page. There are multiple errors in a form. Of course, there must be multiple different names to remember. The string used by the parameter in actionmessage must be the key defined in the key-value in applicationresources. properties, and the value will display an error message on your page later. In addition, we also need to modify a struts configuration file. In the action in <action-mappings>, set the validate attribute to true. In the input field, add the page to display the error. Of course, form verification is used, of course, we need to configure <form-beans>, all of which are as follows <form-beans> <form-bean name = "loginform" type = "form. loginform "/> </form-beans> <action-mappings> <action Path ="/login "name =" loginform "type =" loginaction "Validate =" true "input = "/login. JSP "/> </Action-mappings> PS: The Path in <action-mapping> is received from the page. the name of the do operation. Name is the form name used, and type is an action method used to process this transaction.

 

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.