Struts2 verification framework 2 (via action class name-validation. xml method), struts2validation

Source: Internet
Author: User

Struts2 verification framework 2 (via action class name-validation. xml method), struts2validation

1 index. jsp

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

<Body>
<S: form action = "ValidateByXML">
<S: textfield name = "username" label = "username:"> </s: textfield> <br>
<S: textfield name = "password" label = "password:"> </s: textfield>
<S: submit name = "submit" label = "submit" align = "center"/>
</S: form>
</Body>

2action class

Package org. action;

Import com. opensymphony. xwork2.ActionSupport;

Public class ValidationTestAction extends ActionSupport {
/**
*
*/
Private static final long serialVersionUID = 1L;
Private String username;
Private String password;

Public String getUsername (){
Return username;
}

Public void setUsername (String username ){
This. username = username;
}

Public String getPassword (){
Return password;
}

Public void setPassword (String password ){
This. password = password;
}

@ Override
Public String execute () throws Exception {
// TODO Auto-generated method stub
System. out. print ("input verification passed ");
Return SUCCESS;
}

// @ Override
// Public void validate (){
/// TODO Auto-generated method stub
// If (username = null | username. trim (). equals ("")){
// AddFieldError ("username", "user name is required! ");
//}
// If (password = null | password. trim (). equals ("")){
// AddFieldError ("password", "password is required! ");
//}
//}

}

3action class corresponds to the validation xml file-ValidationTestAction-validation.xml (naming rules: action class name-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 of the field to be verified -->
<Field name = "username">
<! -- The verification string cannot be blank, Which is required. -->
<Field-validator type = "requiredstring">
<! -- Remove space -->
<Param name = "trim"> true </param>
<! -- Error message -->
<Message> the user name is required </message>
</Field-validator>
</Field>
<Field name = "password">
<! -- The verification string cannot be blank, Which is required. -->
<Field-validator type = "requiredstring">
<! -- Remove space -->
<Param name = "trim"> true </param>
<! -- Error message -->
<Message> password is required </message>
</Field-validator>
</Field>
</Validators>

4struts. xml (link between the interface and action)

<? 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 = "default" extends = "struts-default">
<Action name = "ValidateByXML" class = "org. action. ValidationTestAction">
<Result name = "success">/success. jsp </result>
<Result name = "input">/index. jsp </result>
</Action>
</Package>
</Struts>


5. Run





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.