Spring 3 annotation-based validation: password and confirm password?

You need the bernate Validation and JSR 303 Api jar.

You need the bernate Validation and JSR 303 Api jar. Org. Bernate hibernate-validator 4.1.0.

Final javax. Validation validation-api 1.0.0. GA mvnrepository.com/artifact/org.hibernate... mvnrepository.com/artifact/javax.validat... See this question: Cross field validation with bernate Validator (JSR 303) there are several ways to deal with that problem.

I wrote the following in order to validate passwords: Constraint implementation: package com.test.web.validation. User; import java.lang.annotation. Documented; import java.lang.annotation.

ElementType; import java.lang.annotation. Retention; import java.lang.annotation. RetentionPolicy; import java.lang.annotation.

Target; import javax.validation. Constraint; import javax.validation. Payload; @Target({ ElementType.

TYPE }) @Retention(RetentionPolicy. RUNTIME) @Documented @Constraint(validatedBy = PasswordsEqualConstraintValidator. Class) public @interface PasswordsEqualConstraint { String message(); Class groups() default {}; Class payload() default {}; } package com.test.web.validation.

User; import javax.validation. ConstraintValidator; import javax.validation. ConstraintValidatorContext; import com.test.logic.dto.

UserDto; public class PasswordsEqualConstraintValidator implements ConstraintValidator { @Override public void initialize(PasswordsEqualConstraint arg0) { } @Override public boolean isValid(Object candidate, ConstraintValidatorContext arg1) { UserDto user = (UserDto) candidate; return user.getPassword(). Equals(user. GetPasswordRepeat()); } } My DTO Object: package com.test.logic.

Dto; import java.io. Serializable; import java.util. ArrayList; import java.util.

Date; import java.util. List; import javax.validation.constraints. NotNull; import javax.validation.constraints.

Size; import com.esldic.web.validation.user. EmailExistsConstraint; import com.esldic.web.validation.user. PasswordsEqualConstraint; @PasswordsEqualConstraint(message = "passwords are not equal") public final class UserDto extends AbstractDto implements Serializable { private static final long serialVersionUID = 1L; private Long id; @NotNull @Size(min = 3, max = 30) @EmailExistsConstraint(message = "email is not available") private String email; private String username; @NotNull @Size(min = 2, max = 30) private String password; @NotNull @Size(min = 2, max = 30) private String passwordRepeat; ... } Finally, my controller package com.test.web.

Controllers; import java.util. Set; import javax.servlet.http. HttpServletRequest; import javax.servlet.http.

HttpServletResponse; import javax.validation. ConstraintViolation; import javax.validation. Validator; import org.springframework.beans.factory.annotation.

Autowired; import org.springframework.stereotype. Controller; import org.springframework.web.bind.annotation. ModelAttribute; import org.springframework.web.bind.annotation.

RequestMapping; import org.springframework.web.bind.annotation. RequestMethod; import org.springframework.web.bind.annotation. ResponseBody; import org.springframework.web.servlet.

ModelAndView; import com.test.logic.dto. UserDto; @Controller public final class SignupController { @Autowired private Validator validator; @RequestMapping(value = "/signup. Html", method = RequestMethod.

POST) public @ResponseBody ModelAndView handleSignupForm(@ModelAttribute UserDto candidate, HttpServletResponse response) throws ServiceException { Set> failures = validator . Validate(candidate); if (!failures.isEmpty()) { response. SetStatus(HttpServletResponse.

SC_BAD_REQUEST); return ValidationHelper. ValidationMessages(failures); } else { return userService. Create(candidate); } } Also, in google you will find a lot of samples with JSR-303 bean validation.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions