Struts verification framework based on AJAX technology (1)

Source: Internet
Author: User

I. Introduction
The fundamental purpose of the verification framework is to implement Domain Verification. There are many methods for Domain Verification in Web applications. In general, there are two types: server and client. Among them, the Struts verification framework is one of the best frameworks suitable for Java-based Web application environments. It can use server-side verification to configure the application and use the error message generated during the verification process that is activated during request processing; in addition, it can also implement client verification through the JavaScript generated on the request page.
AJAX is a technology that can call servers asynchronously and return XML documents. It has become very popular recently. One of its important advantages is its ability to implement real-time data verification. This article will mainly discuss how to use AJAX technology to further enhance the functions of the existing Struts verification framework. To this end, we must develop several components to select the verification framework and generate messages in the specified format for the client. We also need to develop a tag library for processing error messages.
Ii. Prerequisites
You need to use the Windows operating system, install the Eclipse integrated development environment and Tomcat application server, and make sure that the MSXML 3.0 ActiveX object has been registered on your operating system. In addition, you also need Struts Library (http://struts.apache.org) and JDOM Library (www.jdom.org for XML Development), please refer to the following figures 1 and 2 respectively.


Figure 1. AjaxForm class hierarchy chart


Figure 2. ErrormessageHandler class hierarchy

Iii. Server
StrustsActionServlet
First, we must use this class to extend the org. apache. struts. action. ActionServlet class to create a servletMapping variable, and use this variable to store information about the action path of the browser address. When adding code, we must configure web. xml as a Web application descriptor for the application server.
The configuration of web. xml is as follows:
...
<Servlet>
<Servlet-name> action </servlet-name>
<Servlet-class> net. sf. struts. servlet. StrutsActionServlet </servlet-class>
...
<Servlet-mapping>
<Servlet-name> action </servlet-name>
<Url-pattern> *. do </url-pattern>
</Servlet-mapping>
...

The corresponding StrutsActionServlet code looks as follows:
Public class StrutsActionServlet extends the ActionServlet.
{
Public String getServletMapping (){
Return this. servletMapping;
}
}

AjaxValidationRequestProcessor
To support the existing Struts framework, we must extend the RequestProcessor class in the Struts package. Therefore, we must customize the request processor. First, we must distinguish how to implement verification-using the existing Struts framework or using AJAX-based solutions; the second reason is that we will sign a contract between the server and the client regarding how to intercept messages. In the process of message generation, we use XML format-this is a good format for sending media messages. The XML format we use is defined as follows:
XML Format
<? Xml version = "1.0" encoding = "UTF-8"?>
<Message>
<Identity name = messageAreaId>
<Description>
MessageValue
</Description>
</Identity>
</Message>

Description:
• Identity is the JavaScript ID of the client, used to specify the location where the message should be placed.
• Description is the result of an error message generated by the server.
First, we need to obtain the servlet ing configuration information from the Web descriptor before processing the ing. After activating this process, the application starts to prepare form instances inherited from the AjaxForm class. The next process will manage AJAX verification and should be checked to ensure that requests from the client do not use the Struts verification framework. In addition, during the request, one is to fill in the information collected from the client sent to the action form; the other is to use the AjaxValidationRequestProcessor (TilesRequestProcessor) that already exists in the parent class) to solve the verification problem.
The verification process in TilesRequestProcessor calls all Validation Based on the Struts verification framework and saves the action error to the request. We need to analyze the action error into fragments and generate XML message verification that will be sent to the client. Since we want to change the verification method, we should check which verification framework we are using during the verification process, as shown in figure 3 ).

Figure 3. Controller processing process

Here, we use JDOM as the processing engine and use it to generate XML messages. 4. When the verification is started and the verification framework used is the AJAX verification framework, fill in the error message and construct XML message verification.

Figure 4. Filter and XML validation Generation Process

ErrorMessageHandler
This class has the XML message builder function. It constructs XML messages based on the identity and description attributes. After buildXMLMessage is called, the caller prepares the document and sets the root element of the XML message. This class also has an addNextXMLMessage function, which is used to add other validation messages to XML. See list 1 in source code ).
The process method in the AjaxValidationRequestProcessor class is responsible for setting the response content type to "text/xml" and sending XML messages as strings. The code of this method is shown in List 2 in the source code attached.
The processValidation method in the AjaxValidationRequestProcessor class fills in action Errors and builds messages for clients based on XML format conventions. The code for this function is as follows:
...
ActionErrors errors = (ActionErrors) request. getAttribute (Globals. ERROR_KEY );
Locale locale = (Locale) request. getAttribute (Globals. LOCALE_KEY );
GenerateXMLMessage (errors, identity, locale, sbXMLMessage );
...


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.