详解SpringMVC 自动封装枚举类的方法
springmvc默认无法自动封装枚举类,解决方法如下:
1.枚举类
publicenumGoodsPromoteEnum{ /** *0精品 */ fine("精品",0), /** *1限时购 */ limit("限时购",1), /** *2特价 */ cheap("特价",2); privateStringvalue; privateintindex; privateGoodsPromoteEnum(Stringvalue,intindex){ this.value=value; this.index=index; } publicstaticGoodsPromoteEnumget(Stringvalue){ for(GoodsPromoteEnump:GoodsPromoteEnum.values()){ if(p.getValue().equals(value)){ returnp; } } returnnull; } publicstaticGoodsPromoteEnumget(intindex){ for(GoodsPromoteEnump:GoodsPromoteEnum.values()){ if(p.getIndex()==index){ returnp; } } returnnull; } publicStringgetValue(){ returnvalue; } publicvoidsetValue(Stringvalue){ this.value=value; } publicintgetIndex(){ returnindex; } publicvoidsetIndex(intindex){ this.index=index; } }
2.编写自定义处理类,继承Converter接口
publicclassStringToGoodsConverterimplementsConverter{ @Override publicGoodsPromoteEnumconvert(Stringvalue){ if(StringUtils.isBlank(value)){ returnnull; } returnGoodsPromoteEnum.get(value); } }
3.在springmvc配置文件里配置
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。