Real-time data validation is one of the great advantages of Ajax technology. By applying this technology, the Struts validation framework enhances struts MVC and makes Web applications closer to desktop applications.
This validation framework is used to validate fields. There are many ways to validate on a Web application. These methods can be divided into two categories: server-side methods and client methods. The Struts validation framework is one of the best frameworks for a java-based Web-application environment. It is able to configure the application by using server-side validation and error messages, which are rendered on the calling verification process when processing the request, and are capable of client-side validation by using the JavaScript rendered on the request page.
Ajax is a JavaScript technology that makes it possible to call servers asynchronously and get XML documents, a document that has been very popular recently. One of its uses is real-time data validation.
This article focuses on using AJAX to enhance the existing struts validation framework. Several components, such as a controller, must be developed to select a validation framework and to render messages in a particular format (for clients) and tag libraries (handling error message rendering).
Necessary
Requires a Windows system with Eclipse and Tomcat application servers. Make sure that the MSXML 3.0 ActiveX object has been registered with the operating system. The Struts Library (http://struts.apache.org) and the Jdom Library (www.jdom.org) are also required for XML development (see Figure 1 and Figure 2).
Server-side Scenario Strustsactionservlet
We have to extend the class from Org.apache.struts.action.ActionServlet to get the servletmapping variable, which stores information about how to format the extension as a browser address form for the action class. When adding code, we must configure Web.xml as the application server's Web application descriptor.
The Web.xml configuration 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>
...
StrutsActionServlet Java代码如下:
public class StrutsActionServlet extends the ActionServlet.
{
public String getServletMapping() {
return this.servletMapping;
}
}
Ajaxvalidationrequestprocessor