简体   繁体   English

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

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

I have the following class: 我有以下课程:

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

and following url: 和以下网址:

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

and following controller method: 和以下控制器方法:

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

When I stop in debug at getRequestSupportKludge method I see that myDTO.isKiosk equals null. 当我停止在getRequestSupportKludge方法的调试时,我看到myDTO.isKiosk等于null。

I cannot change the request url. 我无法更改请求网址。

Where can I configure mapping for my request parameter ? 在哪里可以为我的请求参数配置映射?

it is working after adding following binder: 添加以下活页夹后它正在工作:

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

You need to use @QueryParam to fetch the value in controller. 您需要使用@QueryParam来获取控制器中的值。 What is binding isKiosk to myDTO? 什么是将isKiosk绑定到myDTO? Nothing when you are requesting the URL like above. 像上面那样请求URL时没有任何反应。 If you are using some view technology and form to submit data then it is important to bind the form variables to the object. 如果您使用某种视图技术和表单来提交数据,则将表单变量绑定到对象很重要。

The other way is you can expose myDTO as a ModelAttribute and use 另一种方法是您可以将myDTO公开为ModelAttribute并使用

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

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

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