本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:邢红瑞的blog
日志总数:523
评论数量:1142
留言数量:0
访问次数:9692007
建立时间:2004年12月20日




[java语言]spring如何处理多选框
文章收藏,  软件技术

邢红瑞 发表于 2005/10/12 13:41:00

写一个propertyEditor ,可以解决有效的解决这个问题import org.springframework.util.StringUtils;import org.springframework.util.NumberUtils; import java.beans.PropertyEditorSupport;import java.util.*;import java.text.NumberFormat; /**  * 使用了 CustomNumberEditor *可以转换以逗号分隔的List.  *允许用户自己定义数据格式 * */public class CollectionOfNumbersEditor extends PropertyEditorSupport {   private final Class numberClass;   private final Collection collectionInstance;   private final NumberFormat numberFormat;   public static final String separator = ",";    public CollectionOfNumbersEditor( Class collectionClass, Class numberClass) {      super();      collectionInstance = checkClasses(numberClass, collectionClass);      this.numberClass = numberClass;      this.numberFormat = null;   }    public CollectionOfNumbersEditor(Class collectionClass, Class numberClass, NumberFormat numberFormat) {      super();      collectionInstance = checkClasses(numberClass, collectionClass);      this.numberClass = numberClass;      this.numberFormat = numberFormat;   }    private Collection checkClasses(Class numberClass, Class collectionClass) throws IllegalArgumentException{      Collection collectionInstance;      // Ensure we have a subclass of Number      if (numberClass == null || !Number.class.isAssignableFrom(numberClass)) {         throw new IllegalArgumentException("Number class must be a subclass of "+Number.class.getName());      }      // Ensure we have a subclass of Collection      if (collectionClass == null || !Collection.class.isAssignableFrom(collectionClass)) {         throw new IllegalArgumentException("Collection class must be a subclass of "+Collection.class.getName());      }      // Ensure we have a instantiatable subclass of Collection      try {         collectionInstance = (Collection) collectionClass.newInstance();      } catch (InstantiationException e) {         throw new IllegalArgumentException("Could no instantiate "+collectionClass.getName()+".\n"+               "The Collection class must be instantiatable.");      } catch (IllegalAccessException e) {         throw new IllegalArgumentException("Could no instantiate "+collectionClass.getName()+".\n"+               "The Collection class must be instantiatable.");      }      return collectionInstance;   }    /**    * This method takes a CSV list of values as input and creates a collection of the specified collection type,    * containing elements of the specified numberType    */   public void setAsText(String string) throws IllegalArgumentException {      // Set the Value.  We start off with an empty collection, and      // can continue working with the collection instance.      setValue(collectionInstance);      if(!StringUtils.hasText(string))return;      for (StringTokenizer tk = new StringTokenizer(string, separator); tk.hasMoreTokens();) {         Number n = parseNumber(tk.nextToken());         if(n == null)continue;         collectionInstance.add( n );      }   }    /**    * This method creates a CSV of all the values in the underlying collection    */   public String getAsText() {      if(getValue()==null) return "";      StringBuilder sb = new StringBuilder();      for (Iterator iterator = ((Collection)getValue()).iterator(); iterator.hasNext();) {         if(numberFormat==null)            sb.append(iterator.next());         else            sb.append(numberFormat.format(iterator.next()));         if(iterator.hasNext()) sb.append(separator);      }      return sb.toString();   }    private Number parseNumber(String text){      if(!StringUtils.hasText(text)) return null;      if(numberFormat==null)         return NumberUtils.parseNumber(text, this.numberClass);      return NumberUtils.parseNumber(text, this.numberClass, numberFormat);   }}


阅读全文(5722) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.582 second(s), page refreshed 144764664 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号