When the visitor input is complete, the system automatically completes the verification. Struts2 Ajax verification is built on the DWR and Dojo frameworks. DWR is responsible for calling remote Java methods in JavaScript, while Dojo is responsible for implementing page effects. Conversion. To enable the core Servlet of DWR to work, you must configure the core Servlet in the web. xml file.
<! -- Configure the core Servlet of Struts2 --> <servlet-name> dwr </servlet-name> <! -- Specify the Servlet implementation class --> <servlet-class> uk. ltd. getahead. dwr. DWRServlet </servlet-class> <! -- Specify to be in the development stage --> <init-param> <param-name> debug </param-name> <param-value> true </param-value> </init- param> </servlet> <! -- Specify the URL intercepted by the core Servlet of DWR --> <servlet-mapping> <servlet-name> dwr </servlet-name> <url-pattern>/dwr/* </url -pattern> <! -- Do not modify the value of url-pattern. problems may occur after modification. --> </servlet-mapping>
Expose, you can specify it in the dwr. xml configuration file. Therefore, you need to add a dwr. xml file in this application ., You only need to add the dwr. xml file under the WEB-INF path of the Web application, and the code of the dwr. xml file is also solid:
<! DOCTYPE dwr PUBLIC "-// GetAhead Limited // DTD Direct Web Remoting 1.0 //" http://www.getahead.ltd.uk/dwr/dwr10.dtd "> <dwr> <! -- Define all Java methods to be exposed --> <allow> <! -- Define org. apache. struts2.validators. DWRValidator is created as a validator object --> <create creator = "new" javascript = "validator"> <param name = "class" value = "org. apache. struts2.validators. DWRValidator "/> </create> <! -- Define a converter --> <convert converter = "bean" match = "com. opensymphony. xwork2.ValidationAwareSupport"/> </allow> <signatures> <! [CDATA [import java. util. map; import org. apache. struts2.validators. DWRValidator; DWRValidator. doPost (String, String, Map <String, String>);]> </signatures> </dwr>
Validator. Of course, the creation here is a false creation, because the Java class cannot create JavaScript objects, but DWR provides a method that allows us to call the validator method on the client, to call the DWRValidator instance. Usage and details. Development can be performed based on the Struts2 encapsulation. It is worth noting that in order to add DWR support for Web applications, it is clear that the DWR binary package needs to be copied to the Web application. Even for the Struts2.0.6 official version, the latest version of DWR is not supported (the latest version of DWR is 2.0.1), so you can only use the previous official version of DWR 1.1.3, copy the dwr-1.1.3.jar file to the Web application. The input results are sent to the server for verification.
<% @ Page language = "java" contentType = "text/html; charset = GBK" %> <! -- Import the Struts2 tag library --> <prefix = "s" uri = "/struts-tags" %>
When an input component loses focus, the system sends the input content to the server for verification. Method, rewrite this method to complete Ajax verification. Some unknown exceptions may occur (this may be something that Struts2 still needs to improve ). Check the file code:
<? Xml version = "1.0" encoding = "GBK"?> <! -- Specify the DTD information of the validation configuration file --> <! DOCTYPE validators PUBLIC "-// OpenSymphony Group // XWork Validator 1.0.2 // EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"> <! -- Verify the root element of the file --> <validators> <! -- Verify the name attribute of Action --> <field name = "name"> <! -- The specified name attribute must meet the required rule --> <field-validator type = "requiredstring"> <param name = "trim"> true </param> <message> the name must be entered. </message> </field-validator> <! -- The specified name attribute must match the Regular expression --> <field-validator type = "regex"> <param name = "expression"> <! [CDATA [(\ w {4, 25})]> </param> <message> the user name you enter can only be a letter or an array, the length must be 4 to 25 </message> </field-validator> </field> <! -- Verify the pass attribute of an Action --> <field name = "pass"> <! -- The pass attribute must meet the required rule. --> <field-validator type = "requiredstring"> <param name = "trim"> true </param> <message> the password must be entered. </message> </field-validator> <! -- The specified pass attribute must match the specified Regular expression --> <field-validator type = "regex"> <param name = "expression"> <! [CDATA [(\ w {4, 25})]> </param> <message> the password you enter can only be a letter or an array, the length must be 4 to 25 </message> </field-validator> </field> <! -- The specified age attribute must be in the specified range --> <field name = "age"> <field-validator type = "int"> <param name = "min"> 1 </ param> <param name = "max"> 150 </param> <message> the age must be between 1 and 150 </message> </field-validator> </field> <! -- The specified birth attribute must be in the specified range --> <field name = "birth"> <field-validator type = "date"> <! -- When the date string is specified below, the date format of this Locale must be used --> <param name = "min"> 1900-01-01 </param> <param name = "max"> 2050-02-21 </param> <message> the age must be between $ {min} and $ {max} </message> </field-validator> </field> </validators>
After the above definition is complete, if the viewer enters the user name on the input page, the Ajax verification result will be displayed when the focus is moved to the password input box. Source: http://blog.sina.com.cn/s/blog_77cb836301017d99.html