获取表单中的Name值-->Enumeration介绍

来源:互联网 发布:阿里云备案订单号查询 编辑:程序博客网 时间:2024/05/22 17:22

Enumeration接口 
Enumeration接口本身不是一个数据结构。但是,对其他数据结构非常重要。 Enumeration接口定义了从一个数据结构得到连续数据的手段。例如,Enumeration定义了一个名为nextElement的方法,可以用来从含有多个元素的数据结构中得到的下一个元素。 
Enumeration接口提供了一套标准的方法,由于Enumeration是一个接口,它的角色局限于为数据结构提供方法协议。

实现该接口的对象由一系列的元素组成,可以连续地调用nextElement()方法来得到 Enumeration枚举对象中的元素。Enumertion接口中仅定义了下面两个方法。 

·boolean hasMoreElemerts() 

测试Enumeration枚举对象中是否还含有元素,如果返回true,则表示还含有至少一个的元素。 
·Object nextElement() 

如果Bnumeration枚举对象还含有元素,该方法得到对象中的下一个元素。

大家可以看一下源码就只有2个方法。

/* * Copyright (c) 1994, 2005, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */package java.util;/** * An object that implements the Enumeration interface generates a * series of elements, one at a time. Successive calls to the * <code>nextElement</code> method return successive elements of the * series. * <p> * For example, to print all elements of a <tt>Vector<E></tt> <i>v</i>: * <pre> *   for (Enumeration<E> e = v.elements(); e.hasMoreElements();) *       System.out.println(e.nextElement());</pre> * <p> * Methods are provided to enumerate through the elements of a * vector, the keys of a hashtable, and the values in a hashtable. * Enumerations are also used to specify the input streams to a * <code>SequenceInputStream</code>. * <p> * NOTE: The functionality of this interface is duplicated by the Iterator * interface.  In addition, Iterator adds an optional remove operation, and * has shorter method names.  New implementations should consider using * Iterator in preference to Enumeration. * * @see     java.util.Iterator * @see     java.io.SequenceInputStream * @see     java.util.Enumeration#nextElement() * @see     java.util.Hashtable * @see     java.util.Hashtable#elements() * @see     java.util.Hashtable#keys() * @see     java.util.Vector * @see     java.util.Vector#elements() * * @author  Lee Boynton * @since   JDK1.0 */public interface Enumeration<E> {    /**     * Tests if this enumeration contains more elements.     *     * @return  <code>true</code> if and only if this enumeration object     *           contains at least one more element to provide;     *          <code>false</code> otherwise.     */    boolean hasMoreElements();    /**     * Returns the next element of this enumeration if this enumeration     * object has at least one more element to provide.     *     * @return     the next element of this enumeration.     * @exception  NoSuchElementException  if no more elements exist.     */    E nextElement();}
根据需求自己封装了一个可以获得表单中的Name值的工具类。

request.getParameterNames()方法是将发送请求页面中form表单里所有具有name属性的表单对象获取(包括button).返回一个Enumeration类型的枚举.
通过Enumeration的hasMoreElements()方法遍历.再由nextElement()方法获得枚举的值.此时的值是form表单中所有控件的name属性的值.
最后通过request.getParameter()方法获取表单控件的value值.

package com.base.util;import java.util.Enumeration;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;/** *  * @author limingxing * @Date:2016-1-7上午10:57:58 * @email:limingxing_aqgy@sina.com * @version:1.0 */public class SearchConditionUtil {  public static <T> Map<String, Object> packageSearchCondion(HttpServletRequest request) {    Map<String, Object> searchCondionMap = new HashMap<String, Object>();    Enumeration<String> paramNames = request.getParameterNames();    while (paramNames.hasMoreElements()) {      String paramName = (String) paramNames.nextElement();      String[] paramValues = request.getParameterValues(paramName);      if (paramValues.length == 1) {        String paramValue = paramValues[0];        if (paramValue.length() != 0) {          searchCondionMap.put(paramName, paramValue);        }      }    }    return searchCondionMap;  }}
在Spring Mvc的Controller中使用

@RequestMapping("/list")public @ResponseBodyObject list(HttpServletRequest request,@RequestParam(defaultValue = "1", value = "pageNum") int pageNum,@RequestParam(defaultValue = "15", value = "pageSize") int pageSize) {Map<String, Object> searchCondionMap = SearchConditionUtil.packageSearchCondion(request);PageInfo<Role> page = roleService.findRoleList(searchCondionMap,pageNum, pageSize);return PageUtil.convertGrid(page);}

searchCondionMap里面就是含有Form表单中的值


0 0