繁体   English   中英

在spring mvc中自定义绑定请求参数和filed在控制器方法bean的接受范围内

[英]customize binding request parameters and fileds inside the accepted to controller method bean in spring mvc

我有以下课程:

public class MyDTO { 
       @NotEmpty      
       private String isKiosk;
       ...
}

和以下网址:

http://localhost:1234/mvc/controllerUrl?isKiosk=false

和以下控制器方法:

@RequestMapping(method = RequestMethod.GET, produces = APPLICATION_JSON)
@ResponseBody
public ResponseEntity<List<?>> getRequestSupportKludge(@Valid final MyDTO myDTO, BindingResult bindingResult) {
    ...
}

当我停止在getRequestSupportKludge方法的调试时,我看到myDTO.isKiosk等于null。

我无法更改请求网址。

在哪里可以为我的请求参数配置映射?

添加以下活页夹后它正在工作:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(String.class, "isKiosk", new PropertyEditorSupport() {
        public void setAsText(String name) {
            setValue(name);
        }
    });
}

您需要使用@QueryParam来获取控制器中的值。 什么是将isKiosk绑定到myDTO? 像上面那样请求URL时没有任何反应。 如果您使用某种视图技术和表单来提交数据,则将表单变量绑定到对象很重要。

另一种方法是您可以将myDTO公开为ModelAttribute并使用

public xxxx controllerMethod(@ModelAttribute("myDTO") MyDTO myDTO, ...) {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM