问题描述
这确实很简单,但是我找不到很好的例子:
It's something really simple but I couldn't find a good example:
我有一个自定义数据类型,希望绑定到SpringMVC复选框,它看起来像这样:是/否:
I have a custom data type that I'd like to bind to a SpringMVC checkbox, it looks like this: YES/NO:
public enum YesNoDataType {
YES("Yes"),
NO("No");
}
SpringMVC复选框自动映射为布尔值,现在我需要映射Selected->是的,Empty-> NO。
SpringMVC checkboxes auto-map to Booleans, now I need to map Selected->YES, Empty->NO.
我知道我必须实现这4个PropertyEditorSupport方法之一,但是哪种方法又如何实现?
I know I have to implement one of these 4 PropertyEditorSupport methods, but which ones, and how?
<form:checkbox path="testYesNo"></form:checkbox>
模型
private YesNoDataType testYesNo;
控制器
binder.registerCustomEditor(YesNoDataType.class, new PropertyEditorSupport() {
// Which ones to override?
@Override
public void setValue(Object value) {
// TODO Auto-generated method stub
super.setValue(value);
}
@Override
public Object getValue() {
// TODO Auto-generated method stub
return super.getValue();
}
@Override
public String getAsText() {
// TODO Auto-generated method stub
return super.getAsText();
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
// TODO Auto-generated method stub
super.setAsText(text);
}
});
推荐答案
我尝试定义和注册一些转换器(YesNoDataType / Boolean ),但我在SpringMVC的CheckboxTag.java中看到它们都是无用的。没有任何转换器或绑定调整将起作用,因为该标记仅显式检查布尔值和字符串:
I tried defining and registering some Converters (YesNoDataType / Boolean), but I see in SpringMVC's CheckboxTag.java that they're all useless. No converters or binding tweaks will work because the tag explicitly checks for Booleans and Strings only:
protected void writeTagDetails(TagWriter tagWriter) throws JspException {
tagWriter.writeAttribute("type", getInputType());
Object boundValue = getBoundValue();
Class<?> valueType = getBindStatus().getValueType();
if (Boolean.class.equals(valueType) || boolean.class.equals(valueType)) {
// the concrete type may not be a Boolean - can be String
if (boundValue instanceof String) {
boundValue = Boolean.valueOf((String) boundValue);
}
Boolean booleanValue = (boundValue != null ? (Boolean) boundValue : Boolean.FALSE);
renderFromBoolean(booleanValue, tagWriter);
}
else {
Object value = getValue();
if (value == null) {
throw new IllegalArgumentException("Attribute 'value' is required when binding to non-boolean values");
}
Object resolvedValue = (value instanceof String ? evaluate("value", value) : value);
renderFromValue(resolvedValue, tagWriter);
}
}
字符串绑定与我无关。在 getValue()
字符串绑定(第2条)中,如果其 value =
属性被选中匹配模型中的字符串。我需要的是True / False布尔绑定,但是我的Converter需要插入第1章,才能从自定义类型获取布尔值。当您尝试超出常见的狭窄参数范围时,Spring如此严格的限制令我非常沮丧。问题仍然悬而未决。
The String binding is irrelevant to me. In the getValue()
String binding (Clause #2), a checkbox is selected if its value=""
attribute matches the string in the model. What I need is a True/False boolean binding, but my Converter needs to be plugged into Clause #1 to obtain a Boolean from a custom type. Just very frustrated that Spring is so restrictive as soon as you try to go outside the narrow parameters of what's common. The issue is still outstanding.
这篇关于SpringMVC复选框:自定义绑定/ PropertyEditor对自定义类型的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!