欢迎来到新闻系统的连续版本!!!
继上次的分享之后又来到了我们的admin界面,又来了一个新的界面,我们的index界面,这个是我们用户界面,只能观看的。
一、admin界面的模糊查询分页
前面我们写好了模糊查询现在我们就来写一下模糊查询的分页,而什么情况下我们会用到分页呢?当然是数据太多在一页展现不过来的时候,这个时候我们就会用到分页,而模糊查询的分页也是如此,就是在模糊查询找到数据之后进行的分页查看。
代码展示:
<ul class="classlist">
<%
//设置编码-设置请求对象的编号
request.setCharacterEncoding("utf-8");
//获取模糊查询的关键字
String str = request.getParameter("str");
if (str == null) {
str = "";
}
//连接数据库,查询新闻
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
Connection con = DriverManager.getConnection(url, "scott", "1234");
//求出数据总条数
PreparedStatement ps = con.prepareStatement("select count(*) from news");
ResultSet rs = ps.executeQuery();
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
int pageIndex = 1;//页码
//接收页码
String index = request.getParameter("pageIndex");
//如果接收到页面,就给页面pageIndex赋值
if (index != null) {
pageIndex = Integer.valueOf(index);
}
int pageSize = 5;//每页的数据条数
//计算最大页码
int maxPage = count / pageSize;
//判断能不能整除,不能整除说明还有不够一页的数据
if (count % pageSize != 0) {
maxPage++;
}
int start = (pageIndex - 1) * pageSize + 1;
int end = pageIndex * pageSize;
String sql = "select * from(select a.*,rownum mid from news a where ntitle like '%" + str
+ "%')b where mid>=? and mid<=?";
ps = con.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while (rs.next()) {
%>
<li><a href="newspages/read_news.jsp?nid=<%=rs.getInt(1)%>">
<%=rs.getString("ntitle")%>
</a> <span> 作者:<%=rs.getString("nzz")%>     
<a href='newspages/update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a>
     <a
href='dodelnews.jsp?nid=<%=rs.getInt(1)%>'
onclick='return clickdel()'>删除</a>
</span></li>
<li class='space'></li>
<%
}
%>
ps:在我们上次模糊查询的基础上加上分页;而分页的重点就在于start和end的计算格式;
重点:在我们点击下一页的时候需要把str关键字传过去
代码展示:
<p align="right">
<a href="admin.jsp">首页</a> <a
href="admin.jsp?pageIndex=<%=pageIndex > 1 ? pageIndex - 1 : 1%><%if (str != null) {
out.print("&str=" + str);
}%>">上一页</a>
当前页数:[<%=pageIndex%>/<%=maxPage%>] <a
href="admin.jsp?pageIndex=<%=pageIndex < maxPage ? pageIndex + 1 : maxPage%><%if (str != null) {
out.print("&str=" + str);
}%>
">下一页</a>
<a href="admin.jsp?pageIndex=<%=maxPage%>">末页</a>
</p>
?:只有一个参数时来拼接
&:当有多个参数时来拼接
admin界面源代码展示:
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加主题--管理后台</title>
<link href="CSS/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%
//判断用户是否为空
Object obj = session.getAttribute("uname");
if (obj == null) {
out.print("<script>alert('暂未登录,请先登录');location.href='login.jsp'</script>");
}
%>
<div id="header">
<div id="welcome">欢迎使用新闻管理系统!</div>
<div id="nav">
<div id="logo">
<img src="images/logo.jpg" alt="新闻中国" />
</div>
<div id="a_b01">
<img src="images/a_b01.gif" alt="" />
</div>
</div>
</div>
<div id="admin_bar">
<div id="status">
管理员<%=session.getAttribute("uname")%>
     <a href="#">login out</a>
</div>
<div id="channel"></div>
</div>
<div id="main">
<div id="opt_list">
<ul>
<li><a href="newspages/add_news.jsp">添加新闻</a></li>
<li><a href="#">编辑新闻</a></li>
<li><a href="#">查找新闻</a></li>
<li><a href="newspages/add_sub.jsp">添加主题</a></li>
<li><a href="newspages/update_sub.jsp">编辑主题</a></li>
</ul>
</div>
<div id="opt_area">
<script language="javascript">
function clickdel() {
return confirm("删除请点击确认");
}
</script>
<form action="admin.jsp" align="center">
<input type="text" name="str"> <input type="submit"
value="查询">
</form>
<ul class="classlist">
<%
//设置编码-设置请求对象的编号
request.setCharacterEncoding("utf-8");
//获取模糊查询的关键字
String str = request.getParameter("str");
if (str == null) {
str = "";
}
//连接数据库,查询新闻
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:ORCL";
Connection con = DriverManager.getConnection(url, "scott", "1234");
//求出数据总条数
PreparedStatement ps = con.prepareStatement("select count(*) from news");
ResultSet rs = ps.executeQuery();
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
int pageIndex = 1;//页码
//接收页码
String index = request.getParameter("pageIndex");
//如果接收到页面,就给页面pageIndex赋值
if (index != null) {
pageIndex = Integer.valueOf(index);
}
int pageSize = 5;//每页的数据条数
//计算最大页码
int maxPage = count / pageSize;
//判断能不能整除,不能整除说明还有不够一页的数据
if (count % pageSize != 0) {
maxPage++;
}
int start = (pageIndex - 1) * pageSize + 1;
int end = pageIndex * pageSize;
String sql = "select * from(select a.*,rownum mid from news a where ntitle like '%" + str
+ "%')b where mid>=? and mid<=?";
ps = con.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while (rs.next()) {
%>
<li><a href="newspages/read_news.jsp?nid=<%=rs.getInt(1)%>">
<%=rs.getString("ntitle")%>
</a> <span> 作者:<%=rs.getString("nzz")%>     
<a href='newspages/update_news.jsp?nid=<%=rs.getInt(1)%>'>修改</a>
     <a
href='dodelnews.jsp?nid=<%=rs.getInt(1)%>'
onclick='return clickdel()'>删除</a>
</span></li>
<li class='space'></li>
<%
}
%>
<p align="right">
<a href="admin.jsp">首页</a> <a
href="admin.jsp?pageIndex=<%=pageIndex > 1 ? pageIndex - 1 : 1%><%if (str != null) {
out.print("&str=" + str);
}%>">上一页</a>
当前页数:[<%=pageIndex%>/<%=maxPage%>] <a
href="admin.jsp?pageIndex=<%=pageIndex < maxPage ? pageIndex + 1 : maxPage%><%if (str != null) {
out.print("&str=" + str);
}%>
">下一页</a>
<a href="admin.jsp?pageIndex=<%=maxPage%>">末页</a>
</p>
</ul>
</div>
</div>
<div id="site_link">
<a href="#">关于我们</a><span>|</span> <a href="#">Aboue Us</a><span>|</span>
<a href="#">联系我们</a><span>|</span> <a href="#">广告服务</a><span>|</span>
<a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span>
<a href="#">招聘信息</a><span>|</span> <a href="#">网站地图</a><span>|</span>
<a href="#">留言反馈</a>
</div>
<div id="footer">
<p class="">
24小时客户服务热线:010-68988888      <a href="#">常见问题解答</a>
     新闻热线:010-627488888<br />
文明办网文明上网举报电话:010-627488888      举报邮箱:<a href="#">jubao@jb-aptech.com.cn</a>
</p>
<p class="copyright">
Copyright © 1999-2009 News China gov, All Right Reserver<br />
新闻中国 版权所有
</p>
</div>
</body>
</html>
二、index页面主题分页
index页面展示:
index界面的主题分页与模糊查询的分页相似,首先中间界面的上面是我们的主题,下面是我们的新闻标题,需要点击主题找到我们的新闻标题,然后再根据查询到的新闻数据分页。
源代码如下:
<ul class="class_date">
<li id='class_month'>
<%
Class.forName("oracle.jdbc.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection con = DriverManager.getConnection(url, "scott", "1234");
//查询主题
PreparedStatement ps = con.prepareStatement("select * from subject");
ResultSet rs = ps.executeQuery();
while(rs.next()){
%>
<a href='index.jsp?tid=<%=rs.getInt(1)%>'><b> <%=rs.getString(2) %> </b></a>
<%} %>
</li>
</ul>
<ul class="classlist">
<%
//接收新闻主题
String id = request.getParameter("tid");
String countSql = "select count(*) from news";
String pageSql = "select * from (select a.*,rownum mid from news a)b where mid>=? and mid<=?";
int tid = 1;
if(id!=null){//要执行 根据主题查询 的 分页
tid = Integer.valueOf(id);
countSql = "select count(*) from news where tid="+tid;
pageSql = "select * from (select a.*,rownum mid from news a where tid="+tid+")b where mid>=? and mid<=?";
}
//查询新闻总条数
ps = con.prepareStatement(countSql);
rs = ps.executeQuery();
int count = 0;
if(rs.next()){
count = rs.getInt(1);
}
//获取页面
String index = request.getParameter("pageIndex");
int pageIndex = 1;
if(index!=null){
pageIndex = Integer.valueOf(index);
}
//查询新闻
int pageSize = 5;
//求出最大页码
int maxPage = count/pageSize;
if(count%pageSize != 0){
maxPage++;
}
int start = (pageIndex-1)*pageSize+1;
int end = pageIndex*pageSize;
ps = con.prepareStatement(pageSql);
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while(rs.next()){
%>
<li>
<a href='newspages/news_add.html'><%=rs.getString("ntitle") %> </a>
<span> <%=rs.getDate("ndate") %> </span>
</li>
<li class='space'></li>
<%} %>
<p align="right">
<a href="index.jsp<%
if(id!=null){
out.print("?tid="+tid);
}
%>">首页</a>
<a href="index.jsp?pageIndex=<%=pageIndex>1?pageIndex-1:1%><%
if(id!=null){
out.print("&tid="+tid);
}
%>">上一页</a>
当前页数:[<%=pageIndex %>/<%=maxPage %>]
<a href="index.jsp?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage%><%
if(id!=null){
out.print("&tid="+tid);
}
%>">下一页</a>
<a href="index.jsp?pageIndex=<%=maxPage%><%
if(id!=null){
out.print("&tid="+tid);
}
%>">末页</a>
</p>
再来分享一个小知识点:在我们登录添加新闻或者修改新闻时,我们只需要找到他的路径在浏览器登录就OK了。但是在我们应用的过程中,我们需要先登录在一步一步的打开,so我们需要展示的界面前加上如下代码即可:
<%
//判断用户是否为空
Object obj = session.getAttribute("uname");
if (obj == null) {
out.print("<script>alert('暂未登录,请先登录');location.href='login.jsp'</script>");
}
%>
ps:用到我们之前内置对象session;判断用户是否为空,如果用户为空来个提示语句然后就自动跳转到登录界面。
分享结束到此结束,白白啦!!!