⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.jsp

📁 看见网上有很多人提出不知道如何实现Jsp的分页勾选状态的保存
💻 JSP
字号:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="java.sql.Connection"/>
<jsp:directive.page import="java.sql.DriverManager"/>
<jsp:directive.page import="java.sql.PreparedStatement"/>
<jsp:directive.page import="java.sql.ResultSet"/>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Test.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
var G_Str="";
   function chk()
   {
     G_Str="";
     for(var i = 0;i<document.all.G.length;i++)
     {
      if(document.all.G[i].checked == true)
      {
         G_Str += document.all.G[i].value+":";
      }
     }
     if(G_Str=="")
     document.all.dv.value = "N";
     else
     document.all.dv.value = G_Str;
   }
   
   function init()
   {
     document.all.page1.value = document.all.page.value;
   }
</script>
</head>

<body onload = "init()">
<%
        Connection con = null;
        PreparedStatement pstmt = null;
        HashMap htmp = null;
        //记录所在页当前页码:    
        int Curp = 0;
     
      try{
        //pages记录请求的页码:
        int pages = Integer.parseInt(request.getParameter("P"));
        
        //////////////////////////////////////////////////////////
        //因为每一次请求的执行完成之后切换到新的页面,所以当前页应该是pages
        //该值用来存放到hashmap当中去,作为键
        //////////////////////////////////////////////////////////
        Curp = pages;
             
       if(request.getParameter("P1")!=null&&request.getParameter("P1").toString()!=""){
           Curp = Integer.parseInt(request.getParameter("P1"));
       }
     
       if(session.getAttribute("opt")==null){
         HashMap hm = new HashMap();
         session.setAttribute("opt",hm);
         htmp = hm;
       }
       else{
             htmp = (HashMap)session.getAttribute("opt");
       }
      
       String str = null;
           
       if(request.getParameter("choosen")!=null){
         str = request.getParameter("choosen").toString();
         //if(str.length()==0)
         //str = ":";           
        }
           
       if(str != null)
       if(htmp!=null&&str.length()>0){
           htmp.put(Curp,str);
       }
     
     
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:tst");
             
        pstmt = con.prepareStatement("select top 10 * from sh where tid not in(select top "+(11*pages)+" tid from sh)");
        ResultSet rst = pstmt.executeQuery();
    
        int TP = 0;
    
        
         out.println("<form id='myf' action='Test.jsp' method='post'>");
         out.println("<table bgcolor='#cccccc' width='300'>");
         out.println(" <input id='dv' type='input' name='choosen'>");
         out.println(" <input id='page1' name='P1' type='input'>");
         out.println(" <input type='input' id='page' name='P' value='"+pages+"'>");   
       
         //勾选状态字符串数组;
         String []pages_Str = null;

         if(htmp.get(pages)!=null){
          pages_Str = htmp.get(pages).toString().split(":"); //把勾选状态字符串转化成数组
        
         }
             System.out.println(htmp.get(pages));
        while(rst.next()){ 
          String tmp = rst.getString(1);
                  
          ////////////////////////////////////////////////////////////////
          // 验证对应页面的勾选状态字符串是否正确传入,没有则正常输出所有的checkbox
          // (预防没有正常传入的情况,也能输出界面)      
          ////////////////////////////////////////////////////////////////
         if(pages_Str!=null){
           boolean flg = false; //是否和checkbox的值匹配的开关值
         
           for(int j = 0;j<pages_Str.length;j++){
              if(pages_Str[j].equals((""+tmp))){ //如果相同则打开开关
                flg = true;
                break; //跳出遍历模式匹配
               }
           }
             
              if(flg == true){//如果匹配开关已经打开则勾选checkbox
               out.print("<tr><td onmouseover='this.style.backgroundColor=\"#bbbbcc\";' onmouseout='this.style.backgroundColor=\"#cccccc\"'><input type='checkbox' name='G' onclick='chk();' checked value="+tmp+">"+tmp+"<br></td></tr>");
               flg = false;//复位开关变量
              }
              else{//正常输出checkbox(未勾选)
              out.print("<tr><td onmouseover='this.style.backgroundColor=\"#bbbbcc\";' onmouseout='this.style.backgroundColor=\"#cccccc\"'><input type='checkbox' name='G' onclick='chk();' value="+tmp+">"+tmp+"<br></td></tr>");
              }
           }
           else{
               //异常情况下正常输出checkbox
               out.print("<tr><td onmouseover='this.style.backgroundColor=\"#bbbbcc\";' onmouseout='this.style.backgroundColor=\"#cccccc\"'><input type='checkbox' name='G' onclick='chk();' value="+tmp+">"+tmp+"<br></td></tr>");
           }    
        }
             
        out.println("</table>");
        
        pstmt = con.prepareStatement("select count(*) from sh");
        ResultSet rst_C = pstmt.executeQuery();
        
        while(rst_C.next()){
           TP = rst_C.getInt(1); 
        }
        
        //////////////////////////////////////////////////////////////
        out.println("<table><tr>");
       // out.print("<td><a href='javascript:myf.submit()' onmousemove='document.all.myf.page.value=this.innerText'>"+j+"</a></td>");
        for(int ii = 0,j=0;ii<TP-10;ii+=10,j++){
            out.print("<td><a href='javascript:myf.submit()' onmousemove='document.all.myf.page.value=this.innerText'>"+j+"</a></td>");//
        }
       // out.print("<td><a href='javascript:myf.submit()' onmousemove='document.all.myf.page.value=this.innerText'>"+j+"</a></td>");
        out.println("</tr></table>");
        out.println("</form>");
       }
       //////////////////////////////////////////////////////////////////
       catch(Exception exc){
          exc.printStackTrace();
       }finally{ 
           if(pstmt!=null){
             pstmt.close();
             con.close();
             }
      }
%>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -