How to get multiple selected values from select box in JSP?

前端 未结 4 1600
不知归路
不知归路 2020-12-05 18:21

I have a html form which have a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form me

相关标签:
4条回答
  • 2020-12-05 18:34

    Something along the lines of (using JSTL):

    <p>Selected Values:
    <ul>
      <c:forEach items="${paramValues['select2']}" var="selectedValue">
        <li><c:out value="${selectedValue}" /></li>
      </c:forEach>
    </ul>
    </p>
    
    0 讨论(0)
  • 2020-12-05 18:46

    It would seem overkill but Spring Forms handles this elegantly. That is of course if you are already using Spring MVC and you want to take advantage of the Spring Forms feature.

    // jsp form
        <form:select path="friendlyNumber" items="${friendlyNumberItems}" />
    
        // the command class
        public class NumberCmd {
          private String[] friendlyNumber;
        }
    
        // in your Spring MVC controller submit method
        @RequestMapping(method=RequestMethod.POST)
        public String manageOrders(@ModelAttribute("nbrCmd") NumberCmd nbrCmd){
    
           String[] selectedNumbers = nbrCmd.getFriendlyNumber();
    
        }
    
    0 讨论(0)
  • 2020-12-05 18:51

    Since I don't find a simple answer just adding more this will be JSP page. save this content to a jsp file once you run you can see the values of the selected displayed.

    Update: save the file as test.jsp and run it on any web/app server

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <%@ page import="java.lang.*" %>
    <%@ page import="java.io.*" %>
    <% String[] a = request.getParameterValues("multiple");
    if(a!=null)
    {
    for(int i=0;i<a.length;i++){
    //out.println(Integer.parseInt(a[i])); //If integer
    out.println(a[i]);
    }}
    %>
    <html>
    <body>
    <form action="test.jsp" method="get">
    <select name="multiple" multiple="multiple"><option value="1">1</option><option value="2">2</option><option value="3">3</option></select>
    <input type="submit">
    </form>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-05 18:52

    request.getParameterValues("select2") returns an array of all submitted values.

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