工作中遇到的问题--BindException

摘要:
org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1errorsFielderrorinobject'location'onfield'code':rejectedvalue[沪];codes[ValidLocationCode.location

org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'location' on field 'code': rejected value [沪]; codes [ValidLocationCode.location.code,ValidLocationCode.code,ValidLocationCode.java.lang.String,ValidLocationCode]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [location.code,code]; arguments []; default message [code]]; default message [Location 沪 already exists]

原因是Controller的edit方法中参数顺序错误,BindingResult需放在location后面

@RequestMapping(value="/edit/{locationId}", method = RequestMethod.PUT)
@PreAuthorize("hasRole('LOCATION_UPDATE')")
public ModelAndView edit(@Valid @ModelAttribute("location") Location location, BindingResult result, @PathVariable("locationId") Long locationId, RedirectAttributes redirectAttributes) {
if(result.hasErrors()) {
ModelAndView modelAndView = createModelAndView("location/form", locationListMenuConfig);
modelAndView.addObject("locationTypes", LocationType.values());
return modelAndView;
}
try {
locationService.edit(locationId, location);
redirectAttributes.addFlashAttribute(ALERT_MESSAGE, AlertMessage.success(UPDATE_SUCCESS));
return backToMainPage();
} catch(ValidationException e) {
ModelAndView modelAndView = createModelAndView("location/form", locationListMenuConfig);
modelAndView.addObject(ALERT_MESSAGE, AlertMessage.error(e.getErrorCode(), e.getParameters()));
modelAndView.addObject("locationTypes", LocationType.values());
return modelAndView;
}
}

免责声明:文章转载自《工作中遇到的问题--BindException》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Tensorflow:ImportError: DLL load failed: 找不到指定的模块 Failed to load the native TensorFlow runtime【论文笔记】深度人脸识别综述下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringMVC基础知识

SpringMVC SpringMVC简介: SpringMVC是一个表示层框架,搭建真实环境 SpringMVC的使用方法:注解和配置。(注解为主) SpringMVC是Spring3.x的一个模块,其实就是用MVC提供的表示层框架。 SpringMVC对视图组件没有必然要求,不一定要使用jsp,struts1,struts2规定了只能用jsp。 Spr...

Spring Boot配置全局异常捕获

1 SpringBoot配置全局的异常捕获 项目的说明 配置thymeleaf作为视图模板 ExceptionController.java模拟测试用 MyAjaxExceptionHandler.java捕获到异常以ajax形式返回 MyExceptionHandler.java捕获到异常以页面形式返回 ajaxerror.html这个是测试返回aj...

Spring MVC使用ModelAndView进行重定向

1、Servlet重定向forward与redirect: 使用servlet重定向有两种方式,一种是forward,另一种就是redirect。forward是服务器内部重定向,客户端并不知道服务器把你当前请求重定向到哪里去了,地址栏的url与你之前访问的url保持不变。redirect则是客户端重定向,是服务器将你当前请求返回,然后给个状态标示给你...

JSR303实现数据校验案例

JSR303实现数据校验案例 1、导包 <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId>...

SpringMVC入门(二)

使用注解的方式进行Handler的开发   注意:此处只介绍和方式一不同的地方 1、注解的处理器适配器  在spring3.1之前使用org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。  在spring3.1之后使用org.springfra...

SpringMVC2

1       SpringMVC架构 1.1     Spring web mvc介绍 Spring web mvc和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来: 1.2     Web MVC mvc设计模式在b/s系统下应用: 1、  用户发起request请求至控制器(Contro...