读书人

新手问jsp怎么处理表单数组

发布时间: 2011-12-26 23:09:58 作者: rapoo

新手问jsp如何处理表单数组
比如表单里有以下内容:
<input type= "checkbox " name= "article_id[] " id= "article_id ">
<input type= "checkbox " name= "article_id[] " id= "article_id ">
<input type= "checkbox " name= "article_id[] " id= "article_id ">

处理页面如何处理它并把它转换成用“,”分割的字符串?

比如php是这样的
$article=$_POST[ 'article_id '];
foreach($article as $id){
$str.=$id. ", ";
}
$str=substr($str,0,-1);
$sql= "update .........where id in( ".$str. ") ";
........

[解决办法]
<input type= "checkbox " name= "article_id ">
<input type= "checkbox " name= "article_id ">
<input type= "checkbox " name= "article_id ">

jsp:

String[] params = request.getParameterValues( "article_id ");

String sql = "update .........where id in ( "+splitID(params)+ ") ";

public String splitID(String id[]){

StringBuffer str = new StringBuffer();

for(int i=0;i <id.length;i++){

if(i> 0)

str.append( ", ");

str.append(id[i]);
}

return str.toString();

}


[解决办法]
String[] params = request.getParameterValues( "article_id ");
StringBuffer str = new StringBuffer();

for(int i=0;i <params .length;i++){

if(i> 0)

str.append( ", ");

str.append(params [i]);
}

String sql = "update .........where id in ( "+str.toString()+ ") ";

读书人网 >Java Web开发

热点推荐