Best ways to perform server-side validation based on data using Java

Can someone help me on this fundamental question: What are the best ways to do server side validation on data using Java?

I would like to know if there are open source libraries available for server side validation.

Any help is appreciated.

+4
source share
3 answers

It depends on what kind of web development framework you are using.


JSP Servlet Gray

If you use a simple jsp servlet, then I would suggest adding one validation package / banner for each screen of the / jsp form. and confirm it from the controller before proceeding with service.


Jsf

If you use JSF, then there is a very well-designed validation phase, you can use ready-made validators (you can simply add the annotation to the form field and its execution), or you can also create your own validators.


Spring mvc

If you use Spring MVC, it also has a saperate layer for validation .


Spacers

Struts also got a saperate level to test

+4
source

If you want to check POJO, you can take a look at the Oval structure http://oval.sourceforge.net/

JSR-303 (and its implementation) can also help if ur application is developed based on pojos or beans

Here is an example of a custom check in OVal: Let's say you need to check the variable map in SomeValueClass and make sure that the welcome key is always present.

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()); } 

@FormCollection annotations on "Variable maps"; is a regular validator and looks like this:

 @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 this helps, Greetings

+3
source

then you go on to an infrastructure that provides built-in checks such as racks, etc. This will give you a validation component. It also opens.

+1
source

Source: https://habr.com/ru/post/1345207/


All Articles