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