有没有让jsp接收复选框的参数值更方便的方案呢?
在一个页面中有一组同名复选框,我在另一个页面中得到这组复选框提交的多选参数值
例如:<form name="form1" action="xxx.jsp" method="post">
<input type="checkbox" name="checkbox2" value="1">
<input type="checkbox" name="checkbox2" value="2">
<input type="checkbox" name="checkbox2" value="3">
</form>
假如我选中了第一个和第二个,我在xxx.jsp中得到他们的值
那么checkbox2里面就包括了"1","2",它们是可以通过函数分开的,只是我也只是在ASP里边用过,对于JSP我还是新手
String[] checkbox2 = request.getParameterValues("checkbox2");
String[] result = request.getParameterValues("checkbox2");
for (int i = 0; result != null && i < result.length; i++){
String resultStr = reault[i];
}jsp有没提供更好的方案来解决这个问题呢?
在一个页面中有一组同名复选框,我在另一个页面中得到这组复选框提交的多选参数值
例如:<form name="form1" action="xxx.jsp" method="post">
<input type="checkbox" name="checkbox2" value="1">
<input type="checkbox" name="checkbox2" value="2">
<input type="checkbox" name="checkbox2" value="3">
</form>
假如我选中了第一个和第二个,我在xxx.jsp中得到他们的值
那么checkbox2里面就包括了"1","2",它们是可以通过函数分开的,只是我也只是在ASP里边用过,对于JSP我还是新手
String[] checkbox2 = request.getParameterValues("checkbox2");
String[] result = request.getParameterValues("checkbox2");
for (int i = 0; result != null && i < result.length; i++){
String resultStr = reault[i];
}jsp有没提供更好的方案来解决这个问题呢?
* Gets a list of long parameters.
* @param request The HttpServletRequest object, known as "request" in a
* JSP page.
* @param name The name of the parameter you want to get
* @param defaultNum The default value of a parameter, if the parameter
* can't be converted into a long.
*/
public static long[] getLongParameters(HttpServletRequest request,
String name, long defaultNum)
{
String[] paramValues = request.getParameterValues(name);
if (paramValues == null) {
return null;
}
if (paramValues.length < 1) {
return new long[0];
}
long[] values = new long[paramValues.length];
for (int i=0; i<paramValues.length; i++) {
try {
values[i] = Long.parseLong(paramValues[i]);
}
catch (Exception e) {
values[i] = defaultNum;
}
}
return values;
}