ich bin neu in Java und Spring. Ich mache meine erste App mit Spring MVC Begginers Guide und ich habe Probleme mit meinem "Kategorien" -Feldprüfer. Es funktioniert nicht richtig. Tbh Es funktioniert überhaupt nicht.Inoperativer Validator
category.java
@Target({ METHOD, FIELD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CategoryValidator.class)
@Documented
public @interface Category {
String message() default "{com.packt.webstore.validator.Category.message}";
Class<?>[] groups() default {};
public abstract Class<? extends Payload>[] payload() default {};
String[] allowedCategories() default {"Laptop","Tablet","Smartfon"};
}
CategoryValidator.java
public class CategoryValidator implements ConstraintValidator<Category, String> {
String[] allowedCategories;
public void initialize(Category constraintAnnotation) {
this.allowedCategories = constraintAnnotation.allowedCategories();
}
public boolean isValid(String value, ConstraintValidatorContext context) {
for(String category : allowedCategories) {
if(value == category) {
return false;
}
}
return true;
}
Das Problem ist, dass alles, was ich in meiner "Kategorie" geben Sie es, dass es nicht korrekt Kategorie zurückkehren Feld:/
Ich habe über mein Problem gegoogelt aber nichts gefunden :(Kann mir jemand erklären was ich falsch mache :)? Vielen Dank!
Wie hast du einen Validator an den Kontext gebunden? –