个人作业二
多条件查询的实现
后台
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); dao yidao=new dao(); List<info>list = new ArrayList<info>(); String p[] = new String[10];//获得条件 String s[] = new String[10];//获得内容 String g[] = new String[10];//获得关系 String f[] = new String[10];//获得精确或者模糊 String number = null; //String number1 = null; number=request.getParameter("number1"); //number1=request.getParameter("number2"); int n = Integer.parseInt(number);//接收条件的个数 System.out.println("条件数"+n); //int n1 = Integer.parseInt(number1); for(int i=1;i<=n;i++) { p[i]= (String) request.getParameter("s"+i); s[i]=(String)request.getParameter("shuru"+i); f[i]=(String)request.getParameter("c"+i); System.out.println("精确还是模糊"+f[i]); System.out.println("条件"+p[i]); System.out.println("输入"+s[i]); if(p[i].equals("论文编号")){ p[i] = "workId"; }else if(p[i].equals("题目")){ p[i] = "head"; }else if(p[i].equals("关键词")){ p[i] = "word"; }else if(p[i].equals("作者")){ p[i] = "writer"; } } for(int i=1;i<n;i++) { g[i]=(String) request.getParameter("g"+i); if(g[i].equals("且")) { g[i]="and"; }else if(g[i].equals("或")) { g[i]="or"; } System.out.println("且或"+g[i]); } String sql="where "; for(int i=1;i<n;i++) { if(s[i].equals("")) { continue; } if(f[i].equals("精确")) { sql=sql+p[i]+"='"+s[i]+"'"+" "+g[i]+" "; }else if(f[i].equals("模糊")) { sql=sql+p[i]+" "+"LIKE"+" "+"'%"+s[i]+"%'"+" "+g[i]+" "; } } if(f[n].equals("精确")) { sql = sql + p[n]+"="+"'"+s[n]+"'"; }else { sql = sql + p[n]+" "+"LIKE"+" "+"'%"+s[n]+"%'"; } if(n==1&&s[1].equals("")) { System.out.println("什么也没传!"); sql=""; } list = yidao.loadview(sql); if(list.size()!=0) { Gson gson=new Gson(); String json=gson.toJson(list); System.out.print(json); response.getWriter().write(json); request.setAttribute("course11",list); request.setAttribute("message", "查询成功"); System.out.print("查询 成功"); request.getRequestDispatcher("main.jsp").forward(request,response); } else { request.setAttribute("message", "查询失败"); request.getRequestDispatcher("main.jsp").forward(request,response); } }
前端
<script> var i=1; var j=1; document.getElementById('number1').value=i; document.getElementById('number2').value=j; function demoDisplay(){ if(document.getElementById("p2").style.display=="none"){ i++; j++; document.getElementById("p2").style.display="inline";//inline是显示出来内容,none是不显示出来内容 document.getElementById('number1').value=i; document.getElementById('number2').value=j; // alert(i); // alert(j); return; }else if(document.getElementById("p3").style.display=="none") { i++; j++; document.getElementById("p3").style.display="inline"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; // alert(i); // alert(j); return; }else if(document.getElementById("p4").style.display=="none") { i++; j++; document.getElementById("p4").style.display="inline"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; // alert(i); // alert(j); return; }else if(document.getElementById("p5").style.display=="none") { i++; j++; document.getElementById("p5").style.display="inline"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; // alert(i); // alert(j); return; } else if(document.getElementById("p6").style.display=="none") { i++; j++; document.getElementById("p6").style.display="inline"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; // alert(i); // alert(j); return; } else if(document.getElementById("p7").style.display=="none") { i++; j++; document.getElementById("p7").style.display="inline"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; // alert(i); // alert(j); return; } } function demoVisibility(){ if(document.getElementById("p7").style.display=="inline") { i--; j--; document.getElementById("p7").style.display="none"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; //System.out.println("i:"+i+"j"+j); return; } else if(document.getElementById("p6").style.display=="inline") { i--; j--; document.getElementById("p6").style.display="none"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; //System.out.println("i:"+i+"j"+j); return; } else if(document.getElementById("p5").style.display=="inline") { i--; j--; document.getElementById("p5").style.display="none"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; //System.out.println("i:"+i+"j"+j); return; }else if(document.getElementById("p4").style.display=="inline") { i--; j--; document.getElementById("p4").style.display="none"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; //System.out.println("i:"+i+"j"+j); return; }else if(document.getElementById("p3").style.display=="inline") { i--; j--; document.getElementById("p3").style.display="none"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; //System.out.println("i:"+i+"j"+j); return; }else if(document.getElementById("p2").style.display=="inline") { i--; j--; document.getElementById("p2").style.display="none"; document.getElementById('number1').value=i; document.getElementById('number2').value=j; //System.out.println("i:"+i+"j"+j); return; } } </script>
//其中之一 <div id="p1"> <select name="g22" style="visibility:hidden" class="slect"> <option value="且">且</option> <option value="或">或</option> </select> <select name="s1" class="option"> <option>题目</option> <option>作者</option> <option>论文编号</option> <option>关键词</option> </select> <input type="text" name="shuru1" class="input" value="" /> <select name="c1" class="option"> <option>精确</option> <option>模糊</option> </select> <button type="button" onclick="demoDisplay()" class="button"><i class="layui-icon">+</i></button> <button type="button" onclick="demoVisibility()" class="button"><i class="layui-icon">-</i></button> <br> </div>
<thead> <tr> <th scope="col">题目</th> <th scope="col">作者</th> <th scope="col">发表日期</th> <th scope="col">原文链接 </th> </tr> </thead> <tbody id="list"> <c:forEach items="${course11}" var="course1"> <tr> <th>${course1.head }</th> <th>${course1.writer }</th> <th>${course1.date }</th> <th><a href="${course1.line}">${course1.line}</a></th> </tr> </c:forEach> </tbody>