STRUTS2 checksum (XML configuration checksum)

Source: Internet
Author: User
Tags svn

Reference Documentation:
Http://struts.apache.org/2.0.9/docs/ajax-client-side-validation.html
Http://struts.apache.org/2.0.9/docs/pure-javascript-client-side-validation.html
Http://struts.apache.org/2.0.9/docs/client-validation.html

Reference Example:
Http://struts.apache.org/2.0.9/docs/ajax-validation.html


Struts2 Checksum (XML configuration checksum)

Basic Description:

Struts checksums can be divided into two types: annotation type, XML configuration type

The annotation type mainly uses annotation to configure the attributes in the action, here we do too much introduction, need to see the other related articles.

XML configuration type mainly refers to the use of XML to describe configuration information, and then provide the struts framework for validation.

checksums with struts2,xml profiles can be used in three ways: normal server-side checksums, client-side checksums, and Ajax-style checksums.

Common server-side checksums:

The client submits the information to the server, uses the XML configuration information on the server, verifies the information submitted by the client, and returns the input page to display the checksum error message.

Client-side Pure JavaScript checksum method:

In the input screen, according to the configuration content in the XML file to generate the corresponding JavaScript verification script, in the submission of data, the first use of these generated JavaScript checksum input information, error in the screen display error message, do not need to submit content to the server, The request is submitted to the server only if the input data checksum is correct. This way of checking does not support all Struts2 built-in checksums, only partial checksums are supported:

Required Validator
Requiredstring Validator
Stringlength Validator
Regex Validator
Email Validator
URL Validator
int Validator
Double Validator

Ajax Mode Checksum

When submitting a request, first use AJAX to submit the data to the server, in the case of validation errors in the page display error message, but do not refresh the page, in the case of successful validation, before submitting the request to the server.

Three ways of comparison

Common service-side check mode Client Pure JavaScript Ajax Way
Position Server Client Server
Whether to refresh the page Is Whether Whether
Validation code Java Javascript Java
Concrete implementation

1 defines an Action class

    action.java

2 definition profile

     Action-validation.xml

3 defines an action in Struts.xml, noting that because of the use of validation, the result of the input name must be defined.

    <action name= "class=" >

         <result></result>

        <result name= " Input "></result>

    </action>

4 defines a form for a JSP page, for example:

<s: Form action= "Quizaction" >

    <s:textfield label= "name" name= "name"/>
     <s:textfield label= "age" name= "age"/>
    <s:textfield label= "Favorite Color "Name=" answer/>
    <s:submit/>

</s:form>

is essentially the same as "normal server-side checksums," but you need to add validate= "true" when the form is defined, for example:

<s:form action= "Quizaction" Validate= "true" >

    <s:textfield label= "name" name= "name"/>
     <s:textfield label= "age" name= "age"/>
    <s:textfield label= "Favorite Color "Name=" answer/>
    <s:submit/>

</s:form>

Basically the same as "normal service-side checksums," but you need to make the following modifications:

1 Installing Dojo Plugin

2 Using Dojo plugin in JSP

<%@ taglib prefix= "SX" uri= "/struts-dojo-tags"%>
3 Add head tag inside head (introduce Dojo's JS library)

<sx:head/>
4 Modify form definition

<s:form method= "POST" Theme= "Ajax" action= "Quizajax" id= "form" >
<s:textfield label= "name" name= "name"/>
<s:textfield label= "Age" name= "age"/>
<s:textfield label= "Favorite Color" name= "Answer"/>
<sx:submit validate= "true"/>
</s:form>

Description: Struts-dojo-plugin may release when 2.1.0. Now use the need to remove 2.1.0SNAPSHOT code from Subversion:

SVN http://svn.apache.org/repos/asf/struts/struts2/trunk

code Example

Quizaction class

package example;

Import Com.opensymphony.xwork2.ActionSupport;

/**
* Created by IntelliJ idea.
* user:ma.zhao@dl.cn
* Date:2007-9-8
* Time:13:58:16
* To change this template use File | Settings | File Templates.
*/
public class Quizaction extends Actionsupport {
Private static final long serialversionuid = -7505437345373234225l;

private String name;
private int age;
Private String answer;

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 String Getanswer () {
return answer;
}

public void Setanswer (String answer) {
This.answer = answer;
}

Public String execute () {
return SUCCESS;
}
}

Quizvalidation.xml Checksum configuration file

<! DOCTYPE validators Public "-//opensymphony group//xwork Validator 1.0.2//en"
"Http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd" >
<validators>
<field name= "Name" >
<field-validator type= "Requiredstring" >
<message>you must enter a name</message>
</field-validator>
</field>
<field name= "Age" >
<field-validator type= "int" >
<param name= "min" >13</param>
<param name= "Max" >19</param>
<message>only people ages to could take this quiz</message>
</field-validator>
</field>
</validators>

Struts.xml configuration file

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Struts Public
"-//apache Software foundation//dtd Struts Configuration 2.0//en"
"Http://struts.apache.org/dtds/struts-2.0.dtd" >

<struts>

<package name= "Example" namespace= "/example" extends= "Struts-default" >

<action name= "quizaction" class= "Example". Quizaction ">
<result name= "input" >/example/quiz-ajax.jsp</result>
<result>/example/quiz-success.jsp</result>
</action>
</package>
</struts>

JSP file

<%--
Created by IntelliJ idea.
user:ma.zhao@dl.cn
Date:2007-9-8
Time:14:02:17
To the template use File | Settings | File Templates.
--%>
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>

<body>

<s:form action= "quizaction" namespace= "/example" validate= "true">

<s:textfield label= "name" name= "name"/>
<s:textfield label= "Age" name= "age"/>
<s:textfield label= "Favorite Color" name= "Answer"/>
<s:submit/>

</s:form>

</body>

Special Note (Ajax Validation):

Why use Ajax checksums.

The pure JavaScript checksum does not refresh the page, but it does not use all the Struts2 checksum methods; The server-side checksum can use all the Struts2 checksums, but the page needs to be refreshed.

STRUTS2 Ajax checksums, you don't need to refresh the page, and you can use all of the checksum methods built into Struts2, so we prefer to use Ajax checksums.

The latest version of (2007/09/08) STRUTS2 is now 2.0.9, and Dojo-plugin is not supported, so there is no way to use Ajax checksums. So to use AJAX validation you have to use the Struts2.1.0 snapshot version.

Download:

Using subversion, you can download the latest STRUTS2 source code from the following locations:

Http://svn.apache.org/repos/asf/struts/struts2/trunk

Structure:

The following commands can be used under blank subdirectories under the downloaded Apps directory

MVN Compile Jar:jar

Can be seen in the target directory: Struts2-blank.war

Use the following command under the Plugins directory Dojo subdirectory

MVN Compile Jar:jar

You can see the Dojo-plugin:struts2-dojo-plugin-2.1.0-snapshot.jar in the target directory

According to the above instructions to construct the AJAX checksum

Some changes:

Struts configuration file:

<action name= "Quizajax" class= "Example". Quizaction ">
<interceptor-ref name= "Jsonvalidationworkflowstack"/>
<result name= "Input" >/example/quiz-ajax.jsp</result>
<result>/example/quiz-success.jsp</result>
</action>

This interceptor is only 2.1.0 in struts.

JSP file:

<%--
Created by IntelliJ idea.
user:ma.zhao@dl.cn
Date:2007-9-8
time:17:29:43
To the template use File | Settings | File Templates.
--%>
<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>
<%@ taglib prefix= "s" uri= "/struts-tags"%>
<%@ taglib prefix= "SX" uri= "/struts-dojo-tags"%>

<title>Validation-AJAX</title>
<sx:head/>

<s:url id= "url" namespace= "/example" action= "Quizajax"/>

<body>
<s:form method= "POST" Theme= "xhtml" namespace= "/example" action= "Quizajax" id= "form" >
<s:textfield label= "name" name= "name"/>
<s:textfield label= "Age" name= "age"/>
<s:textfield label= "Favorite Color" name= "Answer"/>
<sx:submit validate= "true"/>
</s:form>
</body>

Need to be aware

<s:url id= "url" namespace= "/example" action= "Quizajax"/>

Provides a URL for the dojo checksum, be sure to note.

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.