« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 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); }} |
|
|