Invalid target for Validator in spring error?

Pulkit

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>
Pulkit

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系debugcn@gmail.com 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ReadMsgQueue返回ERROR_INVALID_PARAMETER

来自分类Dev

FindNextFileA返回6(ERROR_INVALID_HANDLE)

来自分类Dev

Error: invalid instruction suffix for `push'

来自分类Dev

CUDA invalid device symbol error

来自分类Dev

Spring-Boot如何正确注入javax.validation.Validator

来自分类Dev

Installing lattice in R: "Error : Invalid DESCRIPTION file"

来自分类Dev

Invalid Binary Error

来自分类Dev

Spring Boot和Hibernate Validator版本

来自分类Dev

Postgis : ERROR: parse error - invalid geometry

来自分类Dev

Invalid character error while using atob() method

来自分类Dev

Invalid Pointer Error when using free()

来自分类Dev

单元测试Controller时模拟Spring Validator

来自分类Dev

Covariant return type invalid conversion error

来自分类Dev

Spring MVC项目中的Hibernate Validator

来自分类Dev

Nuget 3 - Octopack - Wrong Framework Target error

来自分类Dev

Error: invalid conversion from 'const int*' to 'int*'

来自分类Dev

Makefile produces error "No rule to make target"

来自分类Dev

WinHttpSendRequest返回ERROR_INVALID_PARAMETER

来自分类Dev

如何将Spring Validator和JSR 303混合使用

来自分类Dev

Hibernate Validator + Spring:关于验证错误的错误消息

来自分类Dev

WTSEnumerateServers ERROR_INVALID_DOMAINNAME

来自分类Dev

Spring Boot和Hibernate Validator版本

来自分类Dev

Spring Hibernate Validator逐步检查

来自分类Dev

cat: write error: Invalid argument

来自分类Dev

在Spring Validator中覆盖错误消息

来自分类Dev

XText Validator在错误的行中显示Parse Error

来自分类Dev

Spring Boot: error="invalid_grant", error_description="错误的凭据"

来自分类Dev

Hibernate-Validator 6.0.3Final 兼容 spring 版本

来自分类Dev

Spring Validator 在 POST 上抛出 NotReadablePropertyException

Related 相关文章

热门标签

归档