如何将uri字符串转换为复杂的对象类型在春天
我只是想知道,我可以将uri字符串转换为另一种对象类型吗?如何将uri字符串转换为复杂的对象类型在春天
@RequestMapping(value="/key/{keyDomain}", method=RequestMethod.GET)
public String propertyEditor(@PathVariable(value="keyDomain") KeyDomain key, Model model){
model.addAttribute("key", key);
return "propertyEditor";
}
,在这里我的配置
<beans:bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<beans:property name="customEditors">
<beans:map>
<!-- <beans:entry key="com.template.baseline.domain.KeyDomain" value="com.template.baseline.propertyEditor.KeyPropertyEditor"/> -->
<beans:entry key="com.template.baseline.domain.KeyDomain">
<beans:ref bean="keyDomainPropertyEditor" />
</beans:entry>
</beans:map>
</beans:property>
</beans:bean>
<!-- key domain property editor bean -->
<beans:bean id="keyDomainPropertyEditor" class="com.template.baseline.propertyEditor.KeyPropertyEditor">
<beans:property name="keyDomain">
<beans:bean class="com.template.baseline.domain.KeyDomain" />
</beans:property>
</beans:bean>
和属性编辑器类:
public class KeyPropertyEditor extends PropertyEditorSupport{
private KeyDomain keyDomain;
/**
* example : 10435
* - 10 will be keyId
* - 435 will be baseOfficeId
*/
@Override
public void setAsText(String text) throws IllegalArgumentException{
KeyDomain keyDomain = new KeyDomain();
keyDomain.setKeyId(Integer.parseInt(text.substring(0,1)));
keyDomain.setBaseOfficeId(Integer.parseInt(text.substring(2,4)));
setValue(keyDomain);
}
@Override
public String getAsText() {
KeyDomain value = (KeyDomain) getValue();
return (value != null ? value.toString() : "");
}
public void setKeyDomain(KeyDomain keyDomain) {
this.keyDomain = keyDomain;
}
}
我认为我可以使用属性编辑器来转换我的URI字符串成为合适的对象类型。我已经做了一个实现并配置了CustomEditorConfigurer,但我总是得到ConversionNotSupportedException。
,如果我在我的控制器添加initBinder,一切都将只是罚款:
@InitBinder
public void setBinder(WebDataBinder dataBinder) {
dataBinder.registerCustomEditor(KeyDomain.class, new KeyPropertyEditor());
}
,我也得到警告这样的事情
警告:org.springframework.beans.factory.config.CustomEditorConfigurer - 将PropertyEditor实例传递到CustomEditorConfigurer已弃用:改为使用PropertyEditorRegistrars或PropertyEditor类名称。违规密钥[com.template.baseline.domain.KeyDomain;有问题的编辑器实例:[email protected]
感谢您的回答。
PS:webBindingInitalizer注入上AnnotationMethodHandlerAdapter上
<beans:bean id="AnnotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<beans:property name="webBindingInitializer">
<beans:bean class="com.template.baseline.initialize.CustomWebBindingInitializer" />
</beans:property>
</beans:bean>
与实现
public class CustomWebBindingInitializer implements WebBindingInitializer {
public CustomWebBindingInitializer(){
System.out.println("******** constructor *********");
}
public void initBinder(WebDataBinder binder, WebRequest request) {
System.out.println("******** initBinder *********");
binder.registerCustomEditor(KeyDomain.class, new KeyDomainPropertyEditor());
}
}
CustomEditorConfigurer
无关与web请求数据绑定。
如果你想注册PropertyEditor
globablly,你需要实现WebBindingInitializer
,并用它提供AnnotationMethodHandlerAdapter
:
<bean
class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<proeprty name = "webBindingInitializer">
<bean class = "MyWebBindingInitializer" />
</property>
</bean>
另一种选择是实现转换的逻辑为Formatter
,并通过FormattingConversionServiceFactoryBean
和<mvc:annotation-driven>
配置,见mvc-showcase sample。
你正朝着正确的方向前进,`PropertyEditor`是一条走向。向我们展示编辑器,以及如何配置它。 – skaffman 2010-11-28 12:35:26
嗨skaffman,我只是发现costumEditorConfigurer并不完全注册成本编辑器时创建bean。在春季参考中告诉“另一个更方便的机制是使用一个名为CustomEditorConfigurer的特殊bean工厂后处理器”。它怎么会发生?一个错误?还是我的错误理解? – 2010-11-28 13:38:01