ServletRequest.getParameterMap() returns Map and ServletRequest.getParameter() returns String?

前端 未结 5 1689
耶瑟儿~
耶瑟儿~ 2020-12-01 09:01

Can someone explain to me why ServletRequest.getParameterMap() returns type

Map 

ServletRequest

相关标签:
5条回答
  • 2020-12-01 09:21

    In the case with multi-value controls (checkbox, multi-select, etc), the request.getParameterValues(..) is used to fetch the values.

    0 讨论(0)
  • 2020-12-01 09:21

    If you have a multi-value control like a multi-selectable list or a set of buttons mapped to the same name multiple selections will map to an array.

    0 讨论(0)
  • 2020-12-01 09:25

    It returns all parameter values for controls with the same name.

    For example:

    <input type="checkbox" name="cars" value="audi" /> Audi
    <input type="checkbox" name="cars" value="ford" /> Ford
    <input type="checkbox" name="cars" value="opel" /> Opel
    

    or

    <select name="cars" multiple>
        <option value="audi">Audi</option>
        <option value="ford">Ford</option>
        <option value="opel">Opel</option>
    </select>
    

    Any checked/selected values will come in as:

    String[] cars = request.getParameterValues("cars");
    

    It's also useful for multiple selections in tables:

    <table>
        <tr>
            <th>Delete?</th>
            <th>Foo</th>
        </tr>
        <c:forEach items="${list}" var="item">
            <tr>
                <td><input type="checkbox" name="delete" value="${item.id}"></td>
                <td>${item.foo}</td>
            </tr>
        </c:forEach>
    </table>
    

    in combination with

    itemDAO.delete(request.getParameterValues("delete"));
    
    0 讨论(0)
  • 2020-12-01 09:43
    http://foo.com/bar?biff=banana&biff=pear&biff=grape
    

    "biff" now maps to {"banana","pear","grape"}

    0 讨论(0)
  • 2020-12-01 09:47

    The real function to get all parameter values is

       request.getParameterValues();
    

    getParameter() is just a shortcut to get first one.

    0 讨论(0)
提交回复
热议问题