1. Validate() method in Action
Struts2 provides a Validateable interface, which only has the validate() method in this interface. The class that implements this interface can be called directly by Struts2. The ActionSupport class implements the Vadidateable interface, but its validate() method is an empty method, which we need to rewrite.
The validate() method will be executed before the execution of the execute() method. The execute() method will be executed only if the data verification is correct. If an error is made, the error will be added to the fieldErrors field. If there are multiple logical processing methods in the defined Action, and different processing logics require different verification rules, in this case validate() will use the same verification rules for all processing logics. In order to implement different verification logics, it is necessary 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" method="login"><result name="success">/success.jsp</result><result name="input">/validateLogin.jsp</result></action></package>
success.jsp
<body>Login successfully<s:property value="account"/></body>
validateLogin.jsp
<body><s:fielderror/></body>
Running results (the first test is correct, the second test is wrong)
2. Pass the XWork verification framework
When using the validate method to verify, if there are a large number of Actions in the web application, the validate method needs to be rewrite multiple times. Therefore, you can use XWork's validator framework to verify Struts2 data to reduce the amount of code.
Create a verification file LoginAction-validation.xml under the com.action package. Note: When there are multiple business processing methods in an Action, the naming rule is: actionName-methodName-validation.xml, where actionName is the Action class name, methodName is the method name of a business processing method in the Action, and the search order of the file is the same as validate() and validateX().
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 again as follows:
The above field verification method is field verification method, and another method is non-field verification method, 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 verification method
Attachment: Commonly used verifiers for Struts2: