使用 Java 对数据执行服务器端验证的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5430529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best ways of performing server side validations on data using Java
提问by Khushroo Mistry
Could someone guide me on this fundamental question: What are the best way ways of performing server side validations on data using Java?
有人可以指导我解决这个基本问题:使用 Java 对数据执行服务器端验证的最佳方法是什么?
I would like to know if there are open source libraries available to perform server side validations as well.
我想知道是否有可用的开源库来执行服务器端验证。
Any help is appreciated.
任何帮助表示赞赏。
回答by Jigar Joshi
It depends on which web developement framework you are using.
这取决于您使用的 Web 开发框架。
Raw JSP Servlet
原始 JSP Servlet
If you are using simple jsp servlet, Then I would suggest add one validator package/jar for each form/jsp screen. and validate it from controller before passing to service.
如果您使用的是简单的 jsp servlet,那么我建议为每个表单/jsp 屏幕添加一个验证程序包/jar。并在传递给服务之前从控制器验证它。
JSF
JSF
If you are using JSF then there is very well designed validation phase provided , You can use ready made validators (you can just add annotation on the form field and its done) or you can create your own validators also.
如果您使用的是 JSF,那么提供了设计良好的验证阶段,您可以使用现成的验证器(您可以在表单字段上添加注释并完成),或者您也可以创建自己的验证器。
Spring MVC
春季MVC
If you are using Spring MVC , it has also got a saperate layer for validation.
如果您使用的是 Spring MVC,它还有一个单独的验证层。
Struts
支柱
Struts has also got saperate layer for validation
Struts 也有单独的验证层
回答by aldrin
If you are looking to validate POJOs, you can have a look at the Oval framework http://oval.sourceforge.net/
如果您想验证 POJO,您可以查看 Oval 框架http://oval.sourceforge.net/
JSR-303 (and its implementations) may also help if ur app is designed based on pojos or beans
如果您的应用程序是基于 pojos 或 bean 设计的,则 JSR-303(及其实现)也可能有所帮助
Here is a sample custom validation in OVal: Lets say you have to validate variables map in SomeValueClass and ensure the value of key 'greeting' is always present.
这是 OVal 中的示例自定义验证:假设您必须验证 SomeValueClass 中的变量映射,并确保键“greeting”的值始终存在。
public class SomeValueClass {
@FormCollection
Map variables;
public static void main(String[] args) {
SomeValueClass svc1 = new SomeValueClass();
svc1.variables = new HashMap();
svc1.variables.put("greeting", "");
Validator validator = new Validator();
List<ConstraintViolation> violations = validator.validate(svc1);
System.out.println("svc1 violations.size() = " + violations.size());
}
The @FormCollection annotation on "Map variables;" is a custom validator and is as below:
“映射变量”上的@FormCollection 注释;是一个自定义验证器,如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@net.sf.oval.configuration.annotation.Constraint(checkWith = FormCollectionCheck.class)
public @interface FormCollection {
String message() default "Some errors in the form";
}
And the constraint check class will look like this:
约束检查类将如下所示:
public class FormCollectionCheck extends AbstractAnnotationCheck<FormCollection> {
public boolean isSatisfied(Object validatedObject, Object valueToValidate, OValContext context, Validator validator) {
Map vars = (Map) valueToValidate;
return !(vars.get("greeting")==null || ((String)vars.get("greeting")).length()<=0);
}
}
Hope that helps, Cheers
希望有帮助,干杯
回答by developer
then you go for the framework which are providing inbuilt validations like struts etc. Those will provide you validation component. Also its an opensource.
然后你去寻找提供内置验证的框架,如 struts 等。这些将为你提供验证组件。也是它的一个开源。