您的位置:首页 > Web前端 > JavaScript

jsp中的一个下拉列表实例

2012-10-23 10:07 330 查看
最近在写一个网站后台,用到了一个下拉列表,在这将代码贴出来供大家参考!(水平有限)
实现的效果如下:

 



 



 

 

 

 

 



 

代码只要涉及三个文件,两个servlet文件:UserListServlet.java (负责在数据库中查询全部用户记录)和UserListByPowerServlet.java(主要根据权限来查询相关记录,当权限是“全部”时,会从此文件跳转到UserListServlet文件让其执行),还有一个jsp文件,UserList.jsp(负责显示查询的数据)

 

UserListServlet.java

 

 

public class UserListServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request ,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");

String pNo=request.getParameter("pNo");

if(pNo==null || pNo.equals("") || pNo.trim().equals("0")){
pNo="1";
}
oj_UserDao ud=new oj_UserDaoImpl();
int pageSize=10;
List list=ud.getUserList(Integer.parseInt(pNo), pageSize);
int totalCount=ud.totalCount();
int pageCount=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
request.setAttribute("pNo", pNo);
request.setAttribute("totalCount", totalCount);
request.setAttribute("pageCount" ,pageCount);
request.setAttribute("userList", list);
request.setAttribute("power","全部");
request.getRequestDispatcher("tab/UserList.jsp").forward(request, response);

}

}


 

UserListByPowerServlet.java

 

public class UserListByPowerServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String power=request.getParameter("power");
if(power.equals("全部")){

request.getRequestDispatcher("userListServlet").forward(request, response);

}

String pNo=request.getParameter("pNo");

if(pNo==null || pNo.equals("") || pNo.trim().equals("0")){
pNo="1";
}
oj_UserDao ud=new oj_UserDaoImpl();
int pageSize=10;
List list=ud.getUserList2(power,Integer.parseInt(pNo), pageSize);
int totalCount=ud.totalCount2(power);
int pageCount=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
request.setAttribute("pNo", pNo);
request.setAttribute("totalCount", totalCount);
request.setAttribute("pageCount" ,pageCount);
request.setAttribute("userList", list);
request.setAttribute("power", power);
request.getRequestDispatcher("tab/UserList.jsp").forward(request, response);

}

}


 

public class UserListByPowerServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String power=request.getParameter("power");
if(power.equals("全部")){

request.getRequestDispatcher("userListServlet").forward(request, response);

}

String pNo=request.getParameter("pNo");

if(pNo==null || pNo.equals("") || pNo.trim().equals("0")){
pNo="1";
}
oj_UserDao ud=new oj_UserDaoImpl();
int pageSize=10;
List list=ud.getUserList2(power,Integer.parseInt(pNo), pageSize);
int totalCount=ud.totalCount2(power);
int pageCount=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
request.setAttribute("pNo", pNo);
request.setAttribute("totalCount", totalCount);
request.setAttribute("pageCount" ,pageCount);
request.setAttribute("userList", list);
request.setAttribute("power", power);
request.getRequestDispatcher("tab/UserList.jsp").forward(request, response);

}

}


 

UserList.jsp(没必要看所有的,只看与下拉列表相关的mygetmoredata()js函数,和select下拉列表)

 

<%@ page language="java" import="java.util.*,model.oj_user" pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style type="text/css">
<!--
body {
margin-left: 3px;
margin-top: 0px;
margin-right: 3px;
margin-bottom: 0px;
}
.STYLE1 {
color: #e1e2e3;
font-size: 12px;
}
.STYLE6 {color: #000000; font-size: 12; }
.STYLE10 {color: #000000; font-size: 12px; }
.STYLE19 {
color: #344b50;
font-size: 12px;
}
.STYLE21 {
font-size: 12px;
color: #3b6375;
}
.STYLE22 {
font-size: 12px;
color: #295568;
}

  a:link { text-decoration: none;color: blue}
     a:active { text-decoration:blink}
     a:hover { text-decoration:underline;color: red}
     a:visited { text-decoration: none;color: green}
img {border:0px}
-->
</style>
<script>

function doLogin(){
var form=document.getElementById("Form");
form.submit();
}
function checkAll(e, itemName)
{
var aa = document.getElementsByName(itemName);
for (var i=0; i<aa.length; i++)
aa[i].checked = e.checked;
}

function checkItem(e, allName)
{
var all = document.getElementsByName(allName)[0];
if(!e.checked) all.checked = false;
else
{
var aa = document.getElementsByName(e.name);
for (var i=0; i<aa.length; i++)
if(!aa[i].checked) return;
all.checked = true;
}
}

function deleChecked(){
var url1="deleteManyUserServlet?";
var users1=document.getElementsByName("mm");
for(var i=0;i<users1.length;i++){
if(users1[i].checked){
url1=url1+"uid="+users1[i].value;
if(i!=users1.length-1){
url1=url1+"&";
}
}
}
if(url1=="deleteManyUserServlet?"){
alert("请选择删除记录");
return ;
}
window.location=url1;
}
function mygetmoredata()
{

myGetMoreDataForm.submit();
}
</script>

</head>

<body>

<%
List<oj_user> userList=(List<oj_user>)request.getAttribute("userList");
int totalCount=Integer.parseInt(request.getAttribute("totalCount").toString());
String s=request.getAttribute("pageCount").toString();
int pageCount=Integer.parseInt(s);
String power=request.getAttribute("power").toString();
int pNo=Integer.parseInt(request.getAttribute("pNo").toString());
%>

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="24" bgcolor="#353c44"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<form method="POST" name="myGetMoreDataForm" action="userListByPowerServlet">
<tr>
<td width="6%" height="19" valign="bottom"><div align="center"><img src="images/tb.gif" width="14" height="14" /></div></td>
<td width="80%" valign="bottom"><span class="STYLE1">  用户基本信息列表</span></td>
<td width="94%" valign="bottom"><span class="STYLE1">  按权限查找</span></td>
<td width="94%" valign="bottom"><span class="STYLE1">

<select size="1" name="power" style="font-family: 宋体; color: #0000FF; font-size: 15px" onChange="javascript:mygetmoredata()">

<%

if(power.equals("全部")){
%>

<option value="超级管理员" >超级管理员</option>
<option value="普通管理员">普通管理员</option>
<option value="普通用户">普通用户</option>
<option value="全部" selected>全部</option>
</select>

<%
}else if(power.equals("超级管理员")){
%>
<option value="超级管理员" selected>超级管理员</option>
<option value="普通管理员">普通管理员</option>
<option value="普通用户">普通用户</option>
<option value="全部" >全部</option>
</select>

<%} else if(power.equals("普通管理员")){
%>
<option value="超级管理员" >超级管理员</option>
<option value="普通管理员" selected>普通管理员</option>
<option value="普通用户">普通用户</option>
<option value="全部" >全部</option>
</select>

<%} else if(power.equals("普通用户")){
%>
<option value="超级管理员" selected>超级管理员</option>
<option value="普通管理员">普通管理员</option>
<option value="普通用户" selected>普通用户</option>
<option value="全部" >全部</option>
</select>

<%} %>

</span></td>
</tr><input type="hidden" name="tpa" value="0"></form>
</table></td>
<td><div align="right"><span class="STYLE1">
<input type="checkbox" name=mmAll id="checkbox11" onclick="checkAll(this, 'mm')"/>
全选         <img src="images/add.gif" width="10" height="10" /> 添加    <img src="images/del.gif" width="10" height="10" /> <a href="#" onclick="deleChecked()">删除</a>          </span><span class="STYLE1">  </span></div></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#a8c7ce">
<tr>
<td width="4%" height="20" bgcolor="d3eaef" class="STYLE10"><div align="center">
<input type="checkbox" name="checkbox" id="checkbox" />
</div></td>
<td width="5%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">序号</span></div></td>
<td width="10%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">用户名</span></div></td>
<td width="15%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">用户角色</span></div></td>
<td width="14%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">密码</span></div></td>
<td width="16%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">总提交数</span></div></td>
<td width="22%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">通过数</span></div></td>
<td width="21%" height="20" bgcolor="d3eaef" class="STYLE6"><div align="center"><span class="STYLE10">基本操作</span></div></td>
</tr>
<%
if(userList!=null){
for(int i=0;i<userList.size();i++){
oj_user user=userList.get(i);
%>

<tr>
<td height="20" bgcolor="#FFFFFF"><div align="center">
<input type=checkbox name=mm value=<%=user.getId() %> onclick="checkItem(this, 'mmAll')">
</div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE6"><div align="center"><span class="STYLE19"><%=i+1%></span></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE6"><div align="center"><span class="STYLE19"><%= user.getName()%></span></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center"><%=user.getPower() %></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center"><%=user.getPassword() %></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center"><%=user.getTotal() %></div></td>
<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center"><%=user.getAccept() %></div></td>
<td height="20" bgcolor="#FFFFFF"><div align="center" class="STYLE21"><a href="deleteOneUserServlet?id=<%=user.getId() %>">删除</a> |  编辑  | 查看</div></td>
</tr>
<%
}}
%>

</table></td>
</tr>
<tr>
<td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="33%"><div align="left"><span class="STYLE22">    共有<strong> <%=totalCount %></strong> 条记录,当前第<strong> <%=pNo %></strong> 页,共 <strong><%=pageCount %></strong> 页</span></div></td>
<td width="67%"><table width="312" border="0" align="right" cellpadding="0" cellspacing="0">
<form action="userListServlet" method="post" id="Form">
<tr>
<td width="49"><div align="center"><a href="userListServlet?pNo=1"><img src="images/first.gif" width="40" height="15" /></a></div></td>
<td width="49"><div align="center"><a href="userListServlet?pNo=<%=pNo>1?pNo-1:1 %>"><img src="images/back.gif" width="45" height="15" /></a></div></td>
<td width="49"><div align="center"><a href="userListServlet?pNo=<%=pNo>=pageCount?pageCount:pNo+1 %>"><img src="images/next.gif" width="45" height="15" /></a></div></td>
<td width="49"><div align="center"><a href="userListServlet?pNo=<%=pageCount %>"><img src="images/last.gif" width="40" height="15" /></a></div></td>
<td width="37" class="STYLE22"><div align="center">转到</div></td>
<td width="22"><div align="center">
<input type="text" name="textfield" id="textfield"  style="width:20px; height:12px; font-size:12px; border:solid 1px #7aaebd;"/>
</div></td>
<td width="22" class="STYLE22"><div align="center">页</div></td>
<td width="35"><img src="images/go.gif" width="30" height="15" onclick="doLogin()"/></td>
</tr>
</form>
</table></td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>


 

 

他们之间的关系大致为,当第一次从别的网页要跳到userList.jsp时,首先跳到UserListServlet.jsp查询全部的数据,当在jsp页面按权限查找时,跳到UserListByPowerServlet之后,查询数据,再将数据返回到jsp页面进行显示,当在UserlistByPower遇到的权限是“全部”时,此servlet不进行查询,跳到userlistservlet后,再查询!!!

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp class div border string list