Invalid target for Validator in spring error?
Asked Answered
M

1

8

Hi all I am getting the following error whenever I am trying to invoke validator in my spring

Servlet.service() for servlet spring threw exception: java.lang.IllegalStateException: Invalid target for Validator

Please have a look and help me out in this error, previously I user the validation for login page and it is working fine but now its not working.

Here is my code snippet .

Controller

@Controller
public class NewUserRegistration 
{
    @Autowired
    private UserService userService;    

    @Autowired
    private NewUserValidator newUserValidator;

    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
       binder.setValidator(newUserValidator);
    }

    @RequestMapping(value="/newUserAdd", method=RequestMethod.POST)
    public String addUser(@ModelAttribute("user")@Valid User user,BindingResult result, Model model)
    {
          return "NewUser";
    }

}

Validator

@Component
public class NewUserValidator  implements Validator
{
    @Override
    public boolean supports(Class<?> classz) 
    {
        return NewUserRegistration.class.equals(classz);
    }

    @Override
    public void validate(Object obj, Errors error) 
    {
    //Validation login for fields
    }
}

JSP Page

<form:form action="newUserAdd" method="POST" modelAttribute="user">
        <center>
        <table>
            <tr><td>User Id:</td><td><input name="userId" type="text" /></td><td><font color="red"><c:out value="${userIdError}" /></font> </td></tr>
            <tr><td>Password:</td><td><input name="userPassword" type="password"/></td><td><font color="red"><c:out value="${userPasswordError}" /></font></td></tr>
            <tr><td>Confirm Password:</td><td><input name="userConfirmPassword" type="password"/></td><td><font color="red"><c:out value="${userPasswordError}" /></font></td></tr>
            <tr><td>Name:</td><td><input name="userName" type="text"/></td><td><font color="red"><c:out value="${userPasswordError}" /></font></td></tr>
            <tr><td></td><td><input type="submit" value="Create"/></td></tr>

        </table>
        </center>
    </form:form>
Mozza answered 5/2, 2014 at 6:16 Comment(5)
Did you refer http://stackoverflow.com/questions/4715860/why-is-my-spring-3-validator-validating-everything-on-the-modelJustification
Or this too http://stackoverflow.com/questions/14533488/addiing-multiple-validators-using-initbinderJustification
Your validator is for NewUserRegistration classes and you try to validate a User object. Different types, hence not compatible, hence the error.Alumnus
@M.Deinum: thanx sir u are genious, I found my mistake, actually in Validator I am using NewUserRegistration object intead of User's object, thanx for pointing this....Mozza
@hrishikesh: thanx sir for these questions but I already read them before posting my question and I found my silly mistake, Anyways I really appreciate for taking your time reading and givng suggestion to my question. :)Mozza
M
21

The problem is actually in Validator class you are using NewUserRegistration's object which is wrong because you want to validate your User's object not your NewUserRegistration's object.

@Override
    public boolean supports(Class<?> classz) 
    {
        return NewUserRegistration.class.equals(classz);
    }

which should be

@Override
    public boolean supports(Class<?> classz) 
    {
        return User.class.equals(classz);
    }
Mozza answered 5/2, 2014 at 14:42 Comment(1)
which one should be used in case of requestBody?Arizona

© 2022 - 2024 — McMap. All rights reserved.