1. 程式人生 > >獲取表單中的Name值-->Enumeration介紹

獲取表單中的Name值-->Enumeration介紹

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:[email protected]
 * @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 @ResponseBody
	Object 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表單中的值