Struts verification framework based on AJAX skills

Source: Internet
Author: User
I. INTRODUCTION the basic purpose of the verification framework is to implement domain verification. There are many methods in the Web application to implement domain verification. In general, there are two types: server and client. Among them, the Struts verification framework is suitable for Java-based Web application

I. INTRODUCTION

The basic purpose of the verification framework is to implement domain verification. There are many methods in the Web application to implement domain verification. 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 be used for server-side verification to configure and utilize the program and the application's inherent error messages in the verification process (activated during the request processing; in addition, it can also implement client verification through the natural JavaScript on the page of the request.

AJAX is a kind of technique that can call the server asynchronously and return XML documents. it has become very popular recently. One of its important strengths is its ability to implement real-time data verification.

This article will discuss how to use AJAX techniques to further strengthen the effectiveness of the existing Struts verification framework. To this end, we must develop several components to select the verification framework and specify the message pattern for the client. we also need to develop a natural tag library for handling error messages.

II. conditions

You need to apply the Windows Control system, install the Eclipse integrated development environment and Tomcat exploitation program server, and ensure that MSXML 3.0 ActiveX object has been registered on your control 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 and.

. AjaxForm Class Hierarchy Chart

. ErrormessageHandler Class Hierarchy Chart

III. server

StrustsActionServlet

First, we must use this class to expand 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 used to take advantage of the program server.

The configuration of web. xml is as follows:

Reference content 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 like it has the following situation:

Reference content is as follows:
Public class StrutsActionServlet extends the ActionServlet.
{
Public String getServletMapping (){
Return this. servletMapping;
}
}
AjaxValidationRequestProcessor

To support the existing Struts framework, we must expand the RequestProcessor class in the Struts package. To this end, we need to customize the pleading processor. First, we need to distinguish how verification is implemented-to apply the existing Struts framework or to apply AJAX-based plans; the second reason is that we will sign a contract between the server and the client regarding how to block messages. In the natural process of messages, we apply the XML pattern-a good media message sending pattern. The XML schema we use is defined as follows:

Reference content is 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 indicate the position of the message to 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, you can use the program to prepare a form instance that continues from the AjaxForm class. The next process will govern AJAX verification and should be checked to ensure that the client's request does not apply the Struts verification framework. In addition, it should be implemented during the solicitation process. one is to process the header of the information sent from the client to the action form; the other is to use the application that already exists in the parent class AjaxValidationRequestProcessor (TilesRequestProcessor) the methods in the existing Struts verification framework in to process the verification title.

The verification process in TilesRequestProcessor calls all validation based on the Struts verification framework and keeps the action error to the request. We need to analyze the action error into fragments and send them to the client for XML message verification. Since we want to change the verification method, we should check which verification framework we are applying during the verification process (see ).

Process of the controller

Here, we use JDOM as the processing engine and apply its natural XML message. 4. when an AJAX verification framework is used for initial verification, fill in the error message and construct XML message verification.

. Inherent process of filtering and XML validation

ErrorMessageHandler

This class has the function of the XML message builder. 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 responsible for adding other validation messages to XML (see list 1 in source code ).

The process method in the AjaxValidationRequestProcessor class is responsible for setting the content type of the response to "text/xml" and sending XML messages as strings. The code for this method is shown in list 2 (see the source code below.

The processValidation method in the AjaxValidationRequestProcessor class is responsible for filling in action errors and agreeing to build messages for the client based on the XML format. The code for this function is as follows:

Reference content is as follows:
...
ActionErrors errors = (ActionErrors) request. getAttribute (Globals. ERROR_KEY );
Locale locale = (Locale) request. getAttribute (Globals. LOCALE_KEY );
GenerateXMLMessage (errors, identity, locale, sbXMLMessage );
...

IV. client

Build the Taglib component

· AjaxJavaScriptLibraryTag: the taglib component is responsible for the JavaScript function of the XMLHTTP controller function based on the client.

· AjaxErrorHtmlRenderTag: this taglib component is used to display the error message area on the JSP page.

Configure Taglib definition

After the taglib component is developed, we need to apply the method of listing 3 (see the source code) to configure the tld file of taglib.

5. create JSP and Struts configurations

To mimic the results of verification, we first need to build the description layer-this can be achieved by participating in the taglib we have built. In this example, I will try to use the validation rules component provided by Struts and the validation function of the form to provide an example of verification. Now, we prepare five text boxes on the JSP page. The first text box applies the verification rule configuration, and the fourth text box applies the verification process from the action form. In addition, we also need a submit button to simulate the form after submission. Note that the existing Struts verification still does not apply AJAX. User interface 5.

. JSP verification page

6. construct Action and Action forms

For Struts action, we only need to forward it to the created JSP. the corresponding action code has the following similar situation:

Reference content is as follows:

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.