Implementation of struts verification framework based on AJAX technology

Source: Internet
Author: User
Tags format extend implement locale return string variable domain
   SummaryReal-time data validation is one of the most important advantages of AJAX technology, and the struts verification framework enriches its MVC by joining this technology, which makes Web application development more close to desktop applications.

   first, the introduction

The fundamental purpose of the validation framework is to implement domain checksums. There are many ways to implement domain checksums in Web applications, which can be grouped into two categories: Server side and client. Among them, the Struts validation framework is one of the best frameworks for a java-based Web application environment. It can use server-side checksums to configure the application and use error messages generated during the validation process (activated during request processing), and it can also implement client-side checksums by generating JavaScript on the request page.

Ajax is a technology that can asynchronously call a server and return XML documents, which is very popular recently. One of its important advantages is the ability to achieve real-time data verification.

This article will mainly explore how to use AJAX technology to further enhance the existing struts verification framework functions. To do this, we must develop several components to implement a message that selects the validation framework and generates the specified format for the client, as well as developing a tag library that handles the generation of error messages.

   Second, the premise

You need to use the Windows operating system and install the Eclipse integrated development environment and Tomcat application server, and make sure that the MSXML 3.0 ActiveX object is already registered on your operating system. In addition, you will also need the Struts library ( and the Jdom library ( for XML development), please refer to Figure 1 and Figure 2, respectively.

Figure 1. Ajaxform class Hierarchy Diagram

Figure 2. Errormessagehandler class Hierarchy Diagram

   third, server-side


First, we must extend the Org.apache.struts.action.ActionServlet class with this class to create a servletmapping variable, and use this variable to store information about the action path of the browser's address. When adding code, we must configure Web.xml as a Web application descriptor for the application server.

The Web.xml configuration is as follows:





The corresponding Strutsactionservlet code appears to have the following form:

public class Strutsactionservlet extends the Actionservlet.
Public String getservletmapping () {
return this.servletmapping;


To support the existing struts framework, we must extend the Requestprocessor class in the struts package. To do this, we have to customize the request processor, one of which is because we have to distinguish between how to implement validation-using the existing struts framework or using an AJAX based scenario, and secondly because we will sign a pact between the server and the client on how to intercept the message. In the message generation process, we are using XML format-This is a good media message send format. The XML format we use is defined as follows:

XML Format



· Identity is the ID of the client JavaScript that indicates where the message should be placed.

· Description is the result of an error message generated by the server side.

First, we need to get the servlet mapping configuration information from the Web descriptor before we process the mapping. After the procedure is activated, the application begins to prepare a form instance that inherits from the Ajaxform class. The next processing will manage the Ajax checksum and should be checked to ensure that requests from the client do not use the Struts validation framework. In addition to implementation during the request, one is to handle the collection of the collected information sent by the client to the action form, and the other is by using the Ajaxvalidationrequestprocessor (tilesrequestprocessor) that already exists in the parent class In the existing struts validation framework in the method to handle the checksum problem.

The validation process in Tilesrequestprocessor invokes all checksums based on the Struts verification framework and saves the action error to the request. We need to parse the action error into fragments and generate an XML message checksum that will be sent to the client. Now that we want to change the checksum, in the validation process, we should check which calibration frame we are using (see Figure 3).

Figure 3. Controller processing Process

Here we take jdom as the processing engine and use it to generate XML messages. As shown in Figure 4, when you start a checksum and the validation framework used is an AJAX validation framework, the next step is to populate the error message and build the XML message checksum.

Figure 4. Filtering and XML validation generation process


This class has the functionality of the XML message builder, which constructs XML messages based on the identity and description properties. After calling Buildxmlmessage, the caller prepares the document and sets the root element of the XML message. This class also has a addnextxmlmessage function, which is responsible for adding other validation messages into the XML (see Listing 1 in the source code).
The process method in the Ajaxvalidationrequestprocessor class is responsible for setting the content type of the response to "Text/xml" and sending an XML message as a string. The code for the method is shown in Listing 2 (see the attached source code).

The Processvalidation method in the Ajaxvalidationrequestprocessor class is responsible for populating the action error and building the message for the client based on the XML format convention. The code for this function looks like this:

Actionerrors errors = (actionerrors) request.getattribute (Globals.error_key);
Locale Locale = (Locale) request.getattribute (Globals.locale_key);
Generatexmlmessage (Errors, identity, Locale, sbxmlmessage);

[1] [2] Next page

Related Article

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: 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.