zoukankan      html  css  js  c++  java
  • 工作中遇到的问题--BindException

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

  • 相关阅读:
    Eclipse常用快捷键
    Kali Linux安装Google中文输入法(只需5步)
    Kali Linux 更新源 操作完整版教程
    Oracle存储过程的异常处理
    Eclipse调试DEBUG时快速查看某个变量的值的快捷键、快速跳转到某行的快捷键
    oracle listagg和wm_concat函数
    ORACLE分页查询SQL语法——最高效的分页
    Mock拦截请求URL返回模板数据
    前端安全之XSS攻击及防御
    Sublime Text3注册码,亲测可用
  • 原文地址:https://www.cnblogs.com/ly-radiata/p/4798299.html
Copyright © 2011-2022 走看看