简体   繁体   English

Struts 1.X 中的 ValidateForm class

[英]ValidateForm class in Struts 1.X

Using the ValidateForm from Struts 1, we will be able to validate the FORM used in the Struts, I went through number of links ( https://cwe.mitre.org/data/definitions/103.html ) still not able to figure the functionality of super.validate() method in Struts使用 Struts 1 中的ValidateForm ,我们将能够验证 Struts 中使用的表单,我浏览了很多链接( https://cwe.mitre.org/data/definitions/103.html )仍然无法确定功能Struts 中的super.validate()方法

Default Validation (using super.validate() in Struts):默认验证(在 Struts 中使用super.validate() ):

public class RegistrationForm extends org.apache.struts.validator.ValidatorForm {
// private variables for registration form
private String name;
private String email;
...

public RegistrationForm() {
super();
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = super.validate(mapping, request);
if (errors == null) {
    errors = new ActionErrors();
}
return errors;
}
// getter and setter methods for private variables
}

In the above code what is the significance of super.validate() method, What is the validation does it perform?上面代码中super.validate()方法的意义是什么,它执行的验证是什么? Could someone explain this please!有人可以解释一下吗!

It performs validation of request parameters according to the rules defined in validation.xml .它根据 validation.xml 中定义的规则执行请求参数的validation.xml It provides the basic validation and if you want to use a custom validation then you should override its validate() method.它提供了基本的验证,如果你想使用自定义验证,那么你应该覆盖它的validate()方法。 Here's the description of the method:以下是该方法的说明:

Validate the properties that have been set from this HTTP request, and return an ActionErrors object that encapsulates any validation errors that have been found.验证已从此 HTTP 请求设置的属性,并返回一个ActionErrors object,它封装了已发现的任何验证错误。 If no errors are found, return null or an ActionErrors object with no recorded error messages.如果未发现任何错误,则返回nullActionErrors object 且不记录任何错误消息。

What happens if you override validate() and not calling super.validate() :如果您覆盖validate()而不调用super.validate()会发生什么

It will cancel the Struts validation if you override the validate method and not call the super.validate(mapping, request) .如果您覆盖验证方法而不调用super.validate(mapping, request) ,它将取消 Struts 验证。 Do it in your code to coexist the custom validation through validate() method and framework validation via validation.xml .在您的代码中执行此操作,以通过validate()方法共存自定义验证,并通过validation.xml进行框架验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM