您的位置:程序门 -> java -> web 开发



新手问jsp如何处理表单数组


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


新手问jsp如何处理表单数组[已结贴,结贴人:justsosos]
发表于:2007-09-07 22:59:52 楼主
比如表单里有以下内容:
<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. ") ";
........
发表于:2007-09-07 23:31:231楼 得分:15
<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();
   
    }

发表于:2007-09-07 23:51:592楼 得分:0
我一看就知道原理正确正是我想要的。不过测试发现出错,不知道是什么原因?本人刚学java,还是停留在php的函数编程思路上。这段代码是不是不能直接放入jsp页面?报错如下:
an   error   occurred   at   line:   4   in   the   jsp   file:   /admin/unit/list_update.jsp
generated   servlet   error:
c:\tomcat5\work\catalina\localhost\long\org\apache\jsp\admin\unit\list_005fupdate_jsp.java:84:   illegal   start   of   expression
private   string   splitid(string   id[]){
^
发表于:2007-09-08 00:17:433楼 得分:0
楼上正解。
<input   type= "checkbox "   name= "article_id[] "   id= "article_id ">
“article_id[]”为什么会有“【】”表示数组吗??
发表于:2007-09-08 08:39:354楼 得分:5
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()+ ") ";
   
发表于:2007-09-08 23:49:525楼 得分:0
出现这个错误,是什么原因?其他jsp页面都运行好好的
description   the   server   encountered   an   internal   error   ()   that   prevented   it   from   fulfilling   this   request.

exception  

org.apache.jasper.jasperexception
org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:372)
org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:292)
org.apache.jasper.servlet.jspservlet.service(jspservlet.java:236)
javax.servlet.http.httpservlet.service(httpservlet.java:802)


root   cause  

java.lang.nullpointerexception
org.apache.jsp.admin.unit.list_005fupdate_jsp._jspservice(list_005fupdate_jsp.java:85)
org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:94)
javax.servlet.http.httpservlet.service(httpservlet.java:802)
org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:324)
org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:292)
org.apache.jasper.servlet.jspservlet.service(jspservlet.java:236)
javax.servlet.http.httpservlet.service(httpservlet.java:802)

发表于:2007-09-09 00:11:566楼 得分:0
结贴,没看仔细,form里边还是用的article_id[],原来jsp与php最大的不同就是 <input   type= "checkbox "   name= "article_id "> 与
<input   type= "checkbox "   name= "article_id[] "   id= "article_id ">


快速检索

最新资讯
热门点击