Validator


Validator itself implements the Interceptor interface, so it is also an interceptor, and the configuration method is exactly the same as the interceptor. The following is a Validator example:

public class LoginValidator extends Validator {
protected void validate(Controller c) { validateRequiredString("name", "nameMsg", "Please enter the user name"); validateRequiredString ("pass", "passMsg", "Please enter your password");
}
protected void handleError(Controller c) { c.keepPara("name");
c.render("login. html");
}
}

In the protected void validator(Controller c) method, you can call the validateXxx(…) series of methods for back-end verification, protected void handleError(Controller c) ) method, you can call the c.keepPara(…) method to return the submitted value back to the page to maintain the original input value, and you can also call the c.render(…) method to return the corresponding page. Note that handleError(Controller c) will only be called when verification fails.

The keepXxx method in the handleError method of the above code is used to keep the data in the page form and pass it back to the page, so that the user does not need to repeatedly enter the form fields that have passed verification. If the model object is passed , you can use the keepModel method to keep the data entered by the user.