java 如何从java代码中的<select multiple>中检索多个选定的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1995793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:52:05  来源:igfitidea点击:

how to retrieve multiple selected values from <select multiple > in java code?

javajspselectservlets

提问by Maddy.Shik

code is below:

代码如下:

<select name="merTransactionTypeId" class="cbox"  multiple>
  <!--
  <option value="0" <%=request.getParameter("merTransactionTypeId")!=null?"0".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>All</option>
  --> 
  <option value="2" <%=request.getParameter("merTransactionTypeId")!=null?"2".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Reload</option>
  <option value="1" <%=request.getParameter("merTransactionTypeId")!=null?"1".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Sale</option>
  <option value="5" <%=request.getParameter("merTransactionTypeId")!=null?"5".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>CCMS_Recharge</option>             
  <option value="6" <%=request.getParameter("merTransactionTypeId")!=null?"6".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Loyalty_Award</option>      
  <option value="7" <%=request.getParameter("merTransactionTypeId")!=null?"7".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Loyalty_Redeem</option>     
  <option value="16" <%=request.getParameter("merTransactionTypeId")!=null?"16".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>FCC_Reload</option>
  <option value="11" <%=request.getParameter("merTransactionTypeId")!=null?"11".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Tracking</option>
  <option value="12" <%=request.getParameter("merTransactionTypeId")!=null?"12".equalsIgnoreCase(request.getParameter("merTransactionTypeId"))?"selected":"":""%>>Fund_Transfer_From_Card</option>                  
</select>

i am trying to retrieve values from dropdown with code in scriplet as

我正在尝试使用脚本中的代码从下拉列表中检索值

<% String[] selectedTransactionTypes = request.getParameterValues("merTransactionTypeId"); %>

...but it's returning null. Please help me out.

...但它返回空值。请帮帮我。

回答by BalusC

Apparently the listbox isn't enclosed in the same <form>, or there's even no means of a <form>, or maybe you tried to access it at the wrong moment (e.g. beforeform submit), or maybe there's a typo in the parameter name (use getParameterNames()to view them all).

显然列表框没有包含在同一个中<form>,或者甚至没有 a 的方法<form>,或者您可能在错误的时间尝试访问它(例如表单提交之前),或者参数名称中可能有拼写错误(用于getParameterNames()查看商场)。

That said, I strongly recommend you to leave the old fashioned scriptlets aside and go ahead with a servlet class to preprocess and postprocess the request and taglibs/EL to control the flow and access data in JSP. It will make your code much cleaner.

也就是说,我强烈建议您将老式的 scriptlet 放在一边,继续使用 servlet 类来预处理和后处理请求,并使用 taglibs/EL 来控制 JSP 中的流和访问数据。它会让你的代码更简洁。