当前位置: 移动技术网 > IT编程>开发语言>Java > 详解SpringMVC 自动封装枚举类的方法

详解SpringMVC 自动封装枚举类的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

米折网登录,深圳阳光医院怎么样,异界药皇

springmvc默认无法自动封装枚举类,解决方法如下:

1.枚举类

public enum goodspromoteenum {

  /**
   * 0 精品
   */
  fine("精品",0),
  /**
   * 1 限时购
   */
  limit("限时购",1), 
  /**
   * 2 特价
   */
  cheap("特价",2);
  
  private string value;

  private int index;

  private goodspromoteenum(string value, int index) {
    this.value = value;
    this.index = index;
  }
  
  public static goodspromoteenum get(string value){
    for (goodspromoteenum p : goodspromoteenum.values()) {
      if (p.getvalue().equals(value)) {
        return p;
      }
    }
    return null;
  }
  
  public static goodspromoteenum get(int index){
    for (goodspromoteenum p : goodspromoteenum.values()) {
      if (p.getindex() == index) {
        return p;
      }
    }
    return null;
  }

  public string getvalue() {
    return value;
  }

  public void setvalue(string value) {
    this.value = value;
  }

  public int getindex() {
    return index;
  }

  public void setindex(int index) {
    this.index = index;
  }
}

2.编写自定义处理类,继承converter接口

public class stringtogoodsconverter implements converter<string, goodspromoteenum> {

  @override
  public goodspromoteenum convert(string value) {
    if (stringutils.isblank(value)) {
     return null;
    }
    return goodspromoteenum.get(value);
  }

}

3.在springmvc配置文件里配置

  <!--自定义枚举类封装 -->
  <bean id="conversionservice" class="org.springframework.format.support.formattingconversionservicefactorybean">
    <property name="converters">
     <set>
      <bean class="com.tentcoo.zbh.util.stringtogoodsconverter" />
     </set>
    </property>
   </bean>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网