大佬教程收集整理的这篇文章主要介绍了Spring 3.0 MVC绑定枚举区分大小写,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
概括地说,你想创建一个新的propertyeditor来为你进行规范化,然后将其注册到Controller中,如下所示:@H_618_3@
@InitBinder
public voID initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Product.class,
new CaseInsensitivepropertyeditor());
}
如果我像这样在Spring控制器中有一个requestMapping &Hellip;@H_618_3@
@requestMapping(method = requestMethod.GET,value = "{product}")
public ModelAndView getPage(@PathVariable Product product)
产品是一个枚举。例如。产品首页@H_618_3@
当我请求页面时,mysite.com / home@H_618_3@
我懂了@H_618_3@
Unable to convert value "home" from type 'java.lang.String' to type 'domain.model.product.Product'; nested exception is java.lang.IllegalArgumentexception: No enum const class domain.model.product.Product.home
有没有办法让枚举类型转换器了解小写的home实际上是Home?@H_618_3@
我想保持url大小写不敏感,并且我的Java枚举使用标准大写字母。@H_618_3@
谢谢@H_618_3@
解@H_618_3@
public class ProductEnumConverter extends PropertyEditorSupport
{
@Override public void setAsText(final String text) throws IllegalArgumentexception
{
SETVALue(Product.valueOf(WordUtils.capitalizeFully(text.trim())));
}
}
注册@H_618_3@
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="domain.model.product.Product" value="domain.infrastructure.ProductEnumConverter"/>
</map>
</property>
</bean>
添加到需要特殊转换的控制器@H_618_3@
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.registerCustomEditor(Product.class,new ProductEnumConverter());
}
以上是大佬教程为你收集整理的Spring 3.0 MVC绑定枚举区分大小写全部内容,希望文章能够帮你解决Spring 3.0 MVC绑定枚举区分大小写所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。