Afraid of people losing data? Improve your data validation-ajax input checksum (8)

Source: Internet
Author: User
Tags xmlns

7.4   AJAX input checksum

Struts 2 when using client-side checksums, the framework needs to be converted to JavaScript script, so not all server-side checksum rules can be converted to client checksums, and AJAX checksums can use all server-side checksum rules.

AJAX is a new technology, the main feature is the asynchronous processing of user requests, for example, when a user fills out a form, when a text box is filled out, the text box loses focus and asynchronously completes the interaction with the server, prompting for the checksum, rather than clicking "Submit" after all the forms have been filled out Displays the checksum information when the button is clicked.

Here's a simple example , let the reader experience the ajax ajax The details of the

7.4.1 Configuring AJAX environments

the following author according to the Establishment Order, guide the reader to configure AJAX environment.




1 . Installing Dwr-1.1-beta-3.jar files

Struts 2Framework to integrateAJAX, need aJARfile, the reader canStruts 2of thestruts2-showcase- 2.0.11 found in the application example, in this exampleLibthe directory has aDwr-1.1-beta-3.jarfile, copy the file to the currentWebApplication ofWeb-inflibTable of Contents.

★ Note ★

struts2-showcase- 2.0.11 for a War compressed files, the reader can extract the file to obtain Dwr-1.1-beta-3.jar .




2 . dwr.xml file

Build a Dwr.xml configuration file, which is saved in the same Web.xml in the same directory, add the following content to the file:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE DWR Public

"-//getahead limited//dtd Direct Web Remoting 1.0//en"

"HTTP://WWW.GETAHEAD.LTD.UK/DWR/DWR10.DTD" >

<dwr>

<allow>

<create creator= "new" javascript= "Validator" >

<param Name= "class" value= "Org.apache.struts2.validators.DWRValidator"/>

</create>

<convert converter= "Bean" match= "Com.opensymphony.xwork2.ValidationAware Support"/>

</allow>

<signatures>

<! [cdata[

import Java.util.Map;

import Org.apache.struts2.validators.DWRValidator;

Dwrvalidator.dopost (String, String, map<string, string>);

]]>

</signatures>

</dwr>

★ description ★

The contents of the file are in a fixed format.




3 . web.xml file

in the Web.xml Add the relevant configuration content to the file, the modified content such as code 7.20 as shown.




Code 7.20 Join AJAX Configuration of web.xml

<?xml version= "1.0" encoding= "UTF-8"?>

<web-app id= "Webapp_9" version= "2.4"

xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"

xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >

<display-name>struts blank</display-name>

< !-- Configure the core filter--> of Struts 2

<filter>

<filter-name>struts2</filter-name>

< !-- Configure the core implementation class of Struts 2 -->

<filter-class>

Org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

</filter>

<filter-mapping>

< !-- intercept all URL requests -->

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

< !-- Configuration Welcome interface -->

< !-- Configure Struts2 's core Servlet-->

<servlet>

<servlet-name>dwr</servlet-name>

< !-- Specify The implementation class for the Servlet -->

<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>

< !-- specified in the development phase -->

<init-param>

<param-name>debug</param-name>

<param-value>true</param-value>

</init-param>

</servlet>

< !-- specifies DWR 's core Servlet - blocked URLs-->

<servlet-mapping>

<servlet-name>dwr</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

Thus, a simple AJAX the environment is completed.

7.4.2 Establish a business controller

establish a simple business controller implementation class, such as code 7.21 as shown.




Code 7.21 AJAX Verify the business controller for the sample

Package Ch7;

Import Java.util.Date;

Import Com.opensymphony.xwork2.ActionSupport;

public class Reg_ajax_action extends Actionsupport {

private String name;

private int age;

private Date birthday;

Getter and setter methods for// attributes

public String getName () {

return name;

    }

public void SetName (String name) {

this.name = name;

    }

public int getage () {

return age;

    }

public void setage (int age) {

this.age = age;

    }

public Date getbirthday () {

return birthday;

    }

public void Setbirthday (Date birthday) {

this.birthday = birthday;

    }

}

the Action It 's very simple, it just defines 3 a corresponding property. Add the following in the configuration file:

<action name= "reg_ajax_action" class= "Ch7". Reg_ajax_action ">

<result name= "Input" >/ch7/reg-ajax.jsp</result>

</action>

7.4.3 Establish a checksum rule file

Build a Reg_ajax_action-validation.xml validation rule files, such as code 7.22 as shown.




Code 7.22 AJAX Validation rule file for sample validation

<! DOCTYPE validators Public "-//opensymphony group//xwork Validator 1.0.2" "//en Ymphony.com/xwork/xwork-validator-1.0.2.dtd ">

<!--

ADD The following DOCTYPE declaration as line of your Xxx-validation.xml file:

<! DOCTYPE validators Public "-//opensymphony group//xwork Validator 1.0.2"

"Http://www.opensymphony.com/xwork/xwork-validator- 1.0.2 . DTD" >

-->

<validators>

<field name= "Name" >

<field-validator type= "requiredstring" >

<message> user name cannot be empty! </message>

</field-validator>

</field>

<field name= "Age" >

<field-validator type= "int" >

<param name= "min" >13</param>

<param name= "Max" >19</param>

<message> age must be between ages </messag e>

</field-validator>

</field>

<field name= "Birthday" >

< !-- definition type is date-->

<field-validator type= "Date" >

< !-- Verify birthday valid time period -->

<param name= "min" > 1990-01-01 </param>

<param name= "Max" > 2006-01-01 </param>

<message> birthdays must be between ${min} and ${max} < /message>

</field-validator>

</field>

</validators>

★ description ★

The checksum rule file is also a standard checksum rule file.

< Span lang= "en-US" "   set jsp View

Build a reg-ajax.jsp view files, such as code 7.23 as shown.




Code 7.23 AJAX of the validation sample JSP View

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

<%@ taglib prefix= "s" uri= "/struts-tags"%>

<!--START Snippet:ajaxvalidation-->

<title>Validation-Basic</title>

<s:head theme= " Ajax "/>

<body>

<s:form method= "POST" validate= "true" Theme= " ajax " >

<s:textfield label= " username " name= "name"/>

<s:textfield label= " Age " name= "ages"/>

<s:textfield label= " birthday " name= "Birthday"/>

<s:submit/>

</s:form>

</body>

<!--end Snippet:ajaxvalidation-->

the document is the same as the previous JSP depending on the graphic, <s:form/> the tag adds the theme= "Ajax" Property Definition.

7.4.5 running an AJAX Validation Sample

enter in the browserhttp://localhost:8080/bookcode/ch7/Reg_ajax_Action!input.action, in the Run Interface Age column, enter "0when the cursor focus leaves the text box, the checksum exception information is immediately displayed without the need to clickSubmitbutton to display the checksum exception information. Visible,AJAXinteracts with the server side in an asynchronous manner and fires when an element loses focus. Its operating interface as shown7.11as shown.

AJAX technology has brought a more friendly experience to the user, please refer to the details in the book later.

/font> ★ note ★

When you run this example, you need to enter in the address bar Http://localhost:8080/bookcode/ch7/Reg_ajax_ action!input.action , rather than http://localhost:8080/bookcode/ch7/test.jsp .

Figure 7.11 AJAX Check Interface







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