简体   繁体   中英

Spring MVC form valiadtion with @NotEmpty annotation, one POJO with Multiple forms

I am working on spring mvc with annotations.

I have a POJO with three fields userName, password and mailId which are annotated as @NotEmpty . I have two forms :

  1. Loginform.jsp : contains two input fields userName and password.
  2. Forgotpassword.jsp : contains only one input field mailId.

The problem is if I use the same pojo for both jsp forms it's giving me binding result errors, as for login.jsp I don't need mailId, and for forgotpassword.jsp I don't need userName and password, but all these fields are annotated as @NotEmpty in my POJO,so gives validation error.

Q: How to deal with the situation? Do I need to create separate POJO for each Form?

According to me, login and forgot-password are two very different business usecases, and as such deserve separate POJOs. Doing so would also, in future, guard you against accidently exposing the password in forgot-password page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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