jsp实现数据表的分页查询效果相关截图:
相关代码如下:
studentList.jsp相关代码:
<%@page import="cn.utils.Page"%>
<%@page import="cn.pojo.*"%>
<%@page import="java.util.*"%>
<%@page import="cn.services.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();//获得项目的路径
String basePath = request.getScheme() + "://" + request.getServerName() + ":"//
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html >
<html>
<head>
<base href=<%=basePath%>>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>studentList.jsp</title>
<link rel="stylesheet" href="css/studentList.css">
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script type="text/javascript" src="js/demo.js"></script>
</head>
<body>
<table class="table">
<tr>
<th>序号</th>
<th>学生姓名</th>
<th>学生密码</th>
<th>学生性别</th>
<th>手机号码</th>
<th>住址</th>
<!-- <th>生日</th> -->
<!-- <th>身份证号</th> -->
<!-- <th>图片</th> -->
<th>中心</th>
<th>驾校</th>
<th>班级</th>
<!-- <th>入学时间</th> -->
</tr>
<%
request.setCharacterEncoding("utf-8");
String str = request.getParameter("pageNum");
Integer pageNum=0;
if(str==null){
pageNum=1;
}else{
pageNum=Integer.parseInt(str);
}
Page<Student> p=new StudentServicesImp().getAllByPagName(pageNum);
List<Student> list=p.getList();
for (int i = 0; i < list.size(); i++) {
Student s = list.get(i);
%>
<tr>
<td><%=(i + 1)%></td>
<td><%=s.getStuName()%></td>
<td><%=s.getStuPassWord()%></td>
<td><%=s.getStuGender()%></td>
<td><%=s.getStuPhone()%></td>
<td><%=s.getStuAddress()%></td>
<%-- <td><%=s.getStuBornDate()%></td> --%>
<%-- <td><%=s.getStuIdentityCard()%></td> --%>
<%-- <td><%=s.getStuPicture()%></td> --%>
<td><%=s.getCenterId()%></td>
<td><%=s.getCoachId()%></td>
<td><%=s.getGradeId()%></td>
<%-- <td><%=s.getStuEnterTime()%></td> --%>
<td><a href="toStudentUpdate.jsp?studentId=<%=s.getStudentId()%>">修改</a></td>
<td><a href="toStudentDelete.jsp?studentId=<%=s.getStudentId()%>">删除</a></td>
<td><a href="showUStudentDetail.jsp?studentId=<%=s.getStudentId()%>">查看详情</a></td>
</tr>
<%
}
%>
</table>
<div class="box">
<div id="lt">
<span> 共有 <%=p.getTotalCount() %> 条记录,当前第 <%=p.getPageNum() %>/<%=p.getTotalPage() %> 页</span>
</div>
<div id="rt">
<a href="studentList.jsp?pageNum=1">首页</a>
<a href="studentList.jsp?pageNum=<%=p.getPageNum()-1%>">上一页</a>
<a href="studentList.jsp?pageNum=<%=p.getPageNum()+1%>">下一页</a>
<a href="studentList.jsp?pageNum=<%=p.getTotalPage()%>">尾页</a>
<form action="studentList.jsp" method="post" id="pageForm">
<span>转到第 </span>
<input type="number" name="pageNum" id="pageInput" value="<%=p.getPageNum()%>"/>
<span>页</span>
<input type="submit" value="GO" id="sub"/>
</form>
</div>
</div>
</body>
</html>
=============================================实现数据表中修改操作,对学生表进行想关修改设置;
tostudentuodate.jsp相关代码
<%@page import="cn.pojo.*"%>
<%@page import="java.util.List"%>
<%@page import="cn.services.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();//获得项目的路径 =/java48_jsp_1
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html >
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script src="js/jquery-1.12.4.js"></script>
<script src="laydate/laydate.js"></script>
<link rel="stylesheet" href="css/userRegister.css" >
<script src="js/userRegister.js"></script>
<body>
<%
Student u= null;
try {
String st = request.getParameter("studentId");
Integer studentId = Integer.parseInt(st);
u = new StudentServicesImp().getStudentById(studentId);
out.print(u);
} catch (Exception e) {
e.printStackTrace();
}
%>
<div class="box">
<h2>用户修改</h2>
<form action="doStudentUpdate.jsp" method="post">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>用户编号:</td>
<td><input type="text" name="studentId" value="<%=u.getStudentId() %>" readonly="readonly" /></td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="stuName" value="<%=u.getStuName() %>" placeholder="输入用户名" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="stuPassWord" value="<%=u.getStuPassWord() %>"/></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="stuGender" value="<%=u.getStuGender() %>" /></td>
</tr>
<tr>
<td>电话:</td>
<td><input type="text" name="stuPhone" value="<%=u.getStuPhone() %>" /></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="stuAddress" value="<%=u.getStuAddress() %>" /></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="text" name="stuBornDate" value="<%=u.getStuBornDate() %>" id="bornDate" /></td>
</tr>
<tr>
<td>身份证号:</td>
<td><input type="text" name="stuIdentityCard" value="<%=u.getStuIdentityCard() %>" id="stuIdentityCard" /></td>
</tr>
<tr>
<td>学员照片:</td>
<td><input type="text" name="stuPicture" value="<%=u.getStuPicture() %>" id="stuPicture" /></td>
</tr>
<tr>
<td>中心编号:</td>
<td>
<select name="centerId">
<%
CenterServices ct=new CenterServicesImp();
List<Center> centerList=ct.getAllCenter();
for(int i=0;i<centerList.size();i++){
Center center=centerList.get(i);
if(u.getCenterId()==center.getCenterId()){
%>
<option value="<%=center.getCenterId()%>" selected>
<%=center.getCenterName() %></option>
<%
}else{
%>
<option value="<%=center.getCenterId()%>">
<%=center.getCenterName() %></option>
<% }
}
%>
</select>
</td>
</tr>
<tr>
<td>教练编号:</td>
<td>
<select name="coachId">
<%
CoachServices ch = new CoachServicesImp();
List<Coach> coachList=ch.getAllCoach();
for( int i = 0 ;i < coachList.size(); i++ ){
Coach coach = coachList.get(i);
if(u.getCoachId()==coach.getCoachId()){
%>
<option value="<%=coach.getCoachId()%>" selected><%=coach.getCoaName()%></option>
<%
}else{
%>
<option value="<%=coach.getCoachId()%>" ><%=coach.getCoaName()%></option>
<%
}
}
%>
</select>
</td>
</tr>
<tr>
<td>班级编号:</td>
<td>
<select name="gradeId">
<%
GradeServices cs = new GradeServicesImp();
List<Grade> gradeList=cs.getAllGrade();
for( int i = 0 ;i < gradeList.size(); i++ ){
Grade grade = gradeList.get(i);
if(u.getGradeId()==grade.getGradeId()){
%>
<option value="<%=grade.getGradeId()%>" selected><%=grade.getGradeName()%></option>
<%
}else{
%>
<option value="<%=grade.getGradeId()%>" ><%=grade.getGradeName()%></option>
<%
}
}
%>
</select>
</td>
</tr>
<tr>
<td>班级名称:</td>
<td><input type="text" name="gradeName" value="<%=u.getGradeName() %>" id="gradeName" /></td>
</tr>
<tr>
<td>教练姓名:</td>
<td><input type="text" name="coachName" value="<%=u.getCoachName() %>" id="coachName" /></td>
</tr>
<tr>
<td>入学时间:</td>
<td><input type="text" name="stuEnterTime" value="<%=u.getStuEnterTime() %>" id="stuEnterTime" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="修改" /></td>
</tr>
<tr>
<td></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
</body>
</html>
=============================================
由tostudentUpdate.jsp级联跳转至dostudentUpdate .jsp,以实现studentListbiao中的添加操作.相关代码如下:
<%@page import="cn.services.StudentServicesImp"%>
<%@page import="cn.pojo.Student"%>
<%@page import="cn.utils.PackObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();//获得项目的路径 =/java48_jsp_1
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html >
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>doStudentUpdate.jsp</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
Student u = PackObject.getObject(request, Student.class);
int i = new StudentServicesImp().updateStudentById(u);
response.sendRedirect("studentList.jsp");
%>
</body>
</html>
========================================================
相关删除操作代码()如下:
<%@page import="cn.services.StudentServicesImp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();//获得项目的路径 =/java48_jsp_1
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html >
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>toStudentDelete.jsp</title>
</head>
<body>
<%
try{
request.setCharacterEncoding("UTF-8");
String st = request.getParameter("studentId");
Integer studentId = Integer.parseInt(st);
int i =new StudentServicesImp().deletStudentById(studentId);
//使用重定向:好处是刷新页面时,不会重复提交表单
response.sendRedirect("studentList.jsp");
}catch(Exception e){
}
%>
</body>
</html>
===============================================
查看详情(showStudentDetail.jsp)相关代码:
<%@page import="cn.pojo.Student"%>
<%@page import="cn.services.StudentServicesImp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!Student u= null;%>
<%
Student u= null;
try {
String st = request.getParameter("studentId");
Integer studentId = Integer.parseInt(st);
u = new StudentServicesImp().getStudentById(studentId);
} catch (Exception e) {
e.printStackTrace();
}
%>
<table>
学员姓名<tr><%=u.getStuName()%></tr>
学员密码<tr><%=u.getStuPassWord()%></tr>
学员性别<tr><%=u.getStuGender()%></tr>
学员电话<tr><%=u.getStuPhone()%></tr>
学员地址<tr><%=u.getStuAddress()%></tr>
学员生日<tr><%=u.getStuBornDate()%></tr>
身份证号<tr><%=u.getStuIdentityCard()%></tr>
学员照片<tr><%=u.getStuPicture()%></tr>
中心编号<tr><%=u.getCenterId()%></tr>
教练编号<tr><%=u.getCoachId()%></tr>
班级编号<tr><%=u.getGradeId()%></tr>
</table>
</body>
</html>
================================================
studentList.css相关代码如下:
@charset "UTF-8";
.table{
border=1px solid black;
cellspacing="0" ;
cellpadding="0"
cellpadding:0;
cellspacing:0;
width:80%;
text-align: center;
margin:10px auto;
}
.box{
position:relative;
width:80%;
margin:0 auto;
border:1px solid black;
}
#rt{
position: absolute;
right:0;
top:0;
}
#pageForm{
display: inline-block;
}
==============================================
demo.js相关代码如下:
$(function(){
$("tr:odd").css("background","grey");
});
=================================================
分页(page.java)跳转的相关代码:
package cn.utils;
import java.util.List;
public class Page<T> {
private Integer pageNum;// 当前的页数,第几页
private Integer totalCount;// 总条数,总共有多少记录
private Integer rowNum; // 当前行数
public static final Integer pageCount = 4;// 每页4条
private Integer totalPage;// 总页数,通过总条数计算的总页数
private List<T> list;// 没一页的数据
public Integer getPageNum() {
return pageNum;
}
public Integer getTotalCount() {
return totalCount;
}
public Integer getRowNum() {
return rowNum;
}
public Integer getTotalPage() {
return totalPage;
}
public List<T> getList() {
return list;
}
// 总条数,总共有多少记录
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
// 总页数,通过总条数计算的总页数
public void setTotalPage() {
this.totalPage = (totalCount % pageCount == 0) ? (totalCount / pageCount) : (totalCount / pageCount + 1);
}
// 当前的页数
public void setPageNum(Integer pageNum) {
// 为空或者小于0
// 就是查找当前页数,
// 需要查找的是 空值、0、负数,显示的都为第一页
// 需要查找的 大于现存的页数,则显示为最后一页
if (pageNum == null) {
this.pageNum = 1;
}
if (pageNum > totalPage) {
this.pageNum = totalPage;
} else {
this.pageNum = pageNum;
}
if (pageNum <= 0) {
this.pageNum = 1;
}
}
// 当前行号
// 当前页的起始行,也就是从第几行开始
public void setRowNum() {
this.rowNum = pageCount * (pageNum - 1);
}
public void setList(List<T> list) {
this.list = list;
}
@Override
public String toString() {
return "Page [totalCount=" + totalCount + ", rowNum=" + rowNum + ", totalPage=" + totalPage + ", list=" + list
+ "]";
}
}