Two methods of validate data verification in Struts2 Struts2 common calibrator _java

Source: Internet
Author: User

The Validate () method in 1.Action

STRUTS2 provides a validateable interface in which only the validate () method exists, and the class that implements the interface can be invoked directly by STRUTS2, and the Actionsupport class implements the Vadidateable interface. But his validate () method is an empty method that requires us to rewrite.

The Validate () method executes before the Execute () method executes and executes the Execute () method only if the data checksum is correct, such as an error that adds an error to the fielderrors domain, and if there are multiple logical processing methods in the defined action, and different processing logic requires different validation rules, in which case validate () uses the same checksum rule for all the processing logic, and in order to implement different validation logic, it needs to pass the Validatex () method, where x represents the method name of the processing logic.

Action class:

public class Loginaction extends actionsupport{
private static final long serialversionuid = 1L;
Private String userName;
Private String UserPassword;
Public String Execute () {
System.out.println ("execute");
return SUCCESS;
}
Public String Login () {
System.out.println ("login");
return SUCCESS;
}
public void Validate () {
System.out.println ("Validata");
}
public void Validatelogin () {
System.out.println ("Validatelogin");
if (!) ( Username.equals ("SBW") && userpassword.equals ("123")) {
addfielderror ("error", "wrong info");
}
Public
String GetUserName () {return
userName;
}
public void Setusername (String userName) {
this.username = userName;
}
Public String Getuserpassword () {return
userpassword;
}
public void SetUserPassword (String userpassword) {
This.userpassword = UserPassword;
}
}

Struts.xml

<package name= "main" extends= "Struts-default" >
<global-results>
<result name= "Login" >/ login.jsp</result>
</global-results>
<action name= "Loginperson" class= " Com.sbw.test.action.LoginAction "method=" Login ">
<result name=" Success ">/success.jsp</result>
<result name= "input" >/validateLogin.jsp</result>
</action>
</package>

success.jsp

<body>
Login Successful <s:property value= "account"/>
</body>

validatelogin.jsp

<body>
<s:fielderror/>
</body>

Run results (first test correct, second Test error)



2. Through the Xwork calibration framework

With the Validate method checksum, if there is a large number of action in the Web application that requires rewriting the Validate method multiple times, you can use the validator framework of Xwork to validate data for Struts2 and reduce the amount of code.

Create a validation file under the Com.action package Loginaction-validation.xml, note: When there are multiple business processes in one action, the naming rule is: Actionname-methodname-validation.xml, where actionn AME is the action class name, methodname the method name for a business process method in the action, and the search order of the file is the same as the Validate () and Validatex () of the method.

Login-validation.xml

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE validators public
"-//apache struts//xwork Validator 1.0.2//en"
"http://struts.apache.org/dtds/ Xwork-validator-1.0.2.dtd ">
<validators>
<field name=" UserName ">
<field-validator Type= "requiredstring" >
<message>the name should not null</message>
</field-validator>
</field>
<field name= "UserPassword" >
<field-validator type= "requiredstring" >
<message>the password should not null</message>
</field-validator>
</field >
</validators>

Run the result again:

The above field checksums are field checksums, and there is a way to do a non-field checksum, as follows:

Login-validation.xml:

<validators>
<validator type= "requiredstring" >
<param name= "FieldName" >username</ Param>
<message>name should not null</message>
</validator>
<validator type= " Requiredstring ">
<param name=" FieldName ">userPassword</param>
<message>password should not null</message>
</validator>
</validators>

The result is the same as the field check mode

Attached: Struts2 Common calibrator:

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.