spring Security的小细节:Field error in object ‘modelAndView‘ on field ‘status‘

背景:我在用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中的属性名也改过来

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值