背景:我在用Spring Security 做一个用户添加功能时,需要在表单上传递一个参数名为status的参数到服务端的Controller处理,进行持久化存储,controller接受参数有ModelAndView 以及一个pojo(UserInfo),提交表单后报错。
报错信息:
Field error in object 'modelAndView' on field 'status': rejected value [1]; codes [typeMismatch.modelAndView.status,typeMismatch.status,typeMismatch.org.springframework.http.HttpStatus,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [modelAndView.status,status]; arguments []; default message [status]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.http.HttpStatus' for property 'status'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.lang.Nullable org.springframework.http.HttpStatus] for value '1'; nested exception is java.lang.IllegalArgumentException: No enum constant org.springframework.http.HttpStatus.1]
根据报错信息,可以看到ModelAndView里面确实有一个status
而我提交的表单也有一个名为status的参数
就会把我提交表单的String类型status与ModelAndView里面的status匹配。由于没有类型转换器(converter),就会报错。
为了不冲突,解决办法有:
1、 如果要往jsp页面传参,不使用ModelAndView,改用Model即可。如果不用传参,那么把ModeAndView变量删除即可,String作为返回值来实现页面跳转。
2、 继续使用ModelAndView,但是要把提交的表单属性名改过来,对应的pojo中的属性名也改过来