Validate data using the validate Method

Source: Internet
Author: User

The simplest way to verify data in struts2 is to use validate. We can see from the source code of the actionsupport class that the actionsupport class implements a validateable interface. This interface has only one validate method. If the action class implements this interface, struts2 will call this method before calling the execute method. We can verify it in the validate method. If an error occurs, you can select a field-level error or an action-Level Error Based on the error level. In addition, addfielderror or addactionerror can be used to add the corresponding error message. If an action or field error exists, struts2 will return "input" (this is not written by developers and is automatically returned by struts2 ), if "input" is returned, struts2 will no longer call the execute method. If no error message exists, struts2 will call the execute method at the end.

The two add methods are similar to the add method in the actionerrors class, but the error message of the add method requires an actionmessage object, which is troublesome. In addition to adding error messages, you can also use the addactionmessage method to add the submitted information. This information is displayed after the submission is successful.

The preceding three add methods are defined in the validationaware interface and have a default implementation in the actionsupport class. In fact, the implementation in the actionsupport class actually calls the corresponding methods in validationawaresupport, that is, these three add methods are implemented in the validationawaresupport class. The Code is as follows:

Privatefinalvalidationawaresupportvalidationaware = newvalidationawaresupport ();
Publicvoidaddactionerror (stringanerrormessage)
{Validationaware. addactionerror (anerrormessage );
}
Publicvoidaddactionmessage (stringamessage)
{
Validationaware. addactionmessage (amessage );
}
Publicvoidaddfielderror (stringfieldname, stringerrormessage)
{
Validationaware. addfielderror (fieldname, errormessage );
}

Next we will implement a simple verification program to experience the use of a validate method.

Create a homepage (validate. jsp) in the web root directory. The Code is as follows:

<% @ Pagelanguage = "Java" Import = "Java. util. *" pageencoding = "GBK" %>
<% @ Taglibprefix = "S" uri = "/Struts-tags" %>
<HTML>
<Head>
<Title> verification data </title>
</Head>
 
<Body>
<S: actionerror/>
<S: actionmessage/>
<S: formaction = "Validate. Action" theme = "simple">
Input content: <s: textfieldname = "MSG"/>
<S: fielderrorkey = "MSG. Hello"/>
<Br/>
<S: Submit/>
</S: Form>
</Body>
</Html>

In the above Code, the tag <s: actionerror>, <s: fielderror>, and <s: actionmessage> of struts2 are used to display the action error information and field error information respectively, and action information. If the information is null, It is not displayed.

Now let's implement a runtime class, the Code is as follows:

Packageaction;

Importjavax. servlet. http .*;
Importcom. opensymphony. xwork2.actionsupport;
Importorg. Apache. struts2.interceptor .*;
Publicclassvalidateactionextendsactionsupport
{
Privatestringmsg;
Publicstringexecute ()
{
System. Out. println (SUCCESS );
Returnsuccess;
}
Publicvoidvalidate ()
{
If (! MSG. inclusignorecase ("hello "))
{
System. Out. println (input );
This. addfielderror ("MSG. Hello", "Hello! ");
This. addactionerror ("processing failed! ");
}
Else
{
This. addactionmessage ("submitted successfully ");
}
}
Publicstringgetmsg ()
{
Returnmsg;
}
Publicvoidsetmsg (stringmsg)
{
This. MSG = MSG;
}
}

As you can see from the code above, a field error requires a key (generally used to indicate the error of the attribute), and an Action error and an action message only need to provide an information string.

Finally, configure the action. The Code is as follows:

<Packagename = "Demo" extends = "struts-Default">
<Actionname = "Validate" class = "Action. validateaction">
<Resultname = "success">/error/validate. jsp </result>
<Resultname = "input">/error/validate. jsp </result>
</Action>
</Package>

If the context path of the application is demo, you can test the program using the following URL:

Http: // localhost: 8080/demo/validate. jsp

We can also use other methods of the validationaware interface (implemented by the validationawaresupport class) to obtain or set field error information, action error information, and action message. For example, if the hasactionerrors method is used to determine whether an action layer error exists, getfielderrors obtains the field error message (a map object ). The following are all the methods provided by the validationaware interface:

Packagecom. opensymphony. xwork2; importjava. util. collection;
Importjava. util. Map;
Publicinterfacevalidationaware
{
Voidsetactionerrors (collectionerrormessages );
Collectiongetactionerrors ();
Voidsetactionmessages (collectionmessages );
Collectiongetactionmessages ();
Voidsetfielderrors (maperrormap );
Mapgetfielderrors ();
Voidaddactionerror (stringanerrormessage );
Voidaddactionmessage (stringamessage );
Voidaddfielderror (stringfieldname, stringerrormessage );
Booleanhasactionerrors ();
Booleanhasactionmessages ();
Booleanhaserrors ();
Booleanhasfielderrors ();
}

Collection:

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.