【发布时间】:2018-05-11 17:51:14
【问题描述】:
我有一个表示提交的表单对象的命令对象。如果表单输入字段中的值是空白的,我希望它是命令对象中的null。
我读到了一个不同的SO question,它显示使用InitBinder
我试了一下,但从未调用过 initBinder 方法。
我的控制器
package com.example;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import com.example.MyDTO;
@Controller
public class MyControllerImpl implements MyController {
@InitBinder("myControllerImpl")
public void initBinder(WebDataBinder binder) {
System.out.println("init binder");
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
@Override
public String someAction( MyDTO someDTO ) {
System.out.println(someDTO);
// Do something with DTO. I want blank values to be null here
return "somePage;
}
}
DTO
package com.example;
public class MyDTO {
private String name;
public String getName() {
return name;
}
public String setName() {
return name;
}
}
我也尝试将类名添加到InitBinder 注释
@InitBinder("myControllerImpl")
但它不起作用。
我缺少什么来调用初始化绑定器?
我正在使用 Java 6 和 Spring 3
【问题讨论】:
标签: java spring spring-mvc