jsp多链接分页
jsp多链接分页
1.实现效果:
2.实现代码详解与分析
//<%Java代码必须在这里面写%>,带参数类型<%=参数名%>
<body>
<%
ArticleDao artDao = new ArticleDao(); //方法类(方法都写在这里面,在后面会显示这个类的代码)
String getType = request.getParameter("type");//获得你点击的链接的参数type
String getSort = request.getParameter("sort");//sort
int pageNow=1;//设置当前第1页
int pagecount=0;//设置一共有几页,初始化为0
//获取当前是第几页,获得的是String类型,要转成Int型
String s_pageNow=request.getParameter("pageNow");
if(s_pageNow!=null){
pageNow=Integer.parseInt(s_pageNow);
}
%>
<div class="main">
<div class="title">
<font>文章列表</font>
<div class="title_div_Style">
</div>
</div>
<div style="width: 100%;height: 80%;">
<%
List<String> typeList = new ArrayList<String>();//定义List数组,用来存获得的数据
typeList = artDao.getAllType();//获得数据type(中心介绍那一行的数据)
if (typeList.size() > 0) {
%>
<div class="sort_Style">
<%
if (getType == null) {
%>
<a>全部</a>
<%
} else {
%>
<a href="textList.jsp">全部:</a>
<%
}
%>
<%
for (int i = 0; i < typeList.size(); i++) {
String type = typeList.get(i);
if (type.equals(getType)) {
%>
<a><%=type%></a> // //处理数据,链接显示形式(可点,不可点),这里不可点
<%
} else {
%>
<a href="textList.jsp?type=<%=type%>"><%=type%></a> //链接可点
<%
}
}
%>
</div>
<%
}
%>
<%
if (getType != null) {
List<String> sortList = new ArrayList<String>();
sortList = artDao.getSortByType(getType); //获得type下的类型(研究平台,研究成果那一行)
if (sortList.size() > 0) {
%>
<div class="sort_Style">
<span style="float: left;">选择类型:</span>
<%
for (int i = 0; i < sortList.size(); i++) {
String sort = sortList.get(i);
if (sort.equals(getSort)) {
%>
<a><%=sort%></a>//处理数据
<%
} else {
%>
<a href="textList.jsp?type=<%=getType%>&sort=<%=sort%>"><%=sort%></a>
<%
}
}
%>
</div>
<%
}
else{
System.out.println("\n出错了!!");
}
}
List<Article> artList = new ArrayList<Article>();
//type和sort都为Null,getArt(pageNow)获得数据,pagecount获得此链接下总页数
if (getType == null && getSort == null) {
artList = artDao.getArt(pageNow);
pagecount=artDao.getArtcount();
}
//type不为null,sort为Null,getArt(getType,pageNow)重载获得数据,pagecount获得此链接下总页数
if (getType != null && getSort == null) {
artList = artDao.getArt(getType,pageNow);
pagecount=artDao.getArtcountByType(getType);
}
//type和sort都不为Null,getArt(getType, getSort,pageNow)重载获得数据,pagecount获得此链接下总页数
if (getType != null && getSort != null) {
artList = artDao.getArt(getType, getSort,pageNow);
pagecount=artDao.getArtcount(getType, getSort);
}
//获取所需的记录数(这里仅仅是显示出来数据,下一步链接未做)
if (artList.size() != 0) {
%>
<ul class="notice_ul">
<%
for (int i = 0; i < artList.size(); i++) {
Article art = artList.get(i);
%>
<li><a href="article.jsp?id=<%=art.getId()%>"> <%=art.getTitle()%>
</a> <span><a href="textServlet?action=delete&id=<%=art.getId()%>">删除</a></span>
<span>编辑</span> <%
if (0 == art.getIsTop()) {
%> <span style="width: 60px;text-align: right;"><a href="textServlet?action=setTop&id=<%=art.getId()%>">置顶</a></span> <%
} else {
%>
<span style="width: 60px;text-align: right;"><a href="textServlet?action=updateTop&id=<%=art.getId()%>">取消置顶</a></span> <%
}
%> <span style="margin-right: 20px;"><%=art.getDate().toString()%></span>
<span><%=art.getAuthor()%></span></li>
<%
}
%>
</ul>
<%
} else {
%>没有内容!<%
}
%>
</div>
<div class="page_Style">
//下面这一段代码实现([首页][下一页].....)那一行
<%
//type和sort都为null
if (getType == null && getSort == null) {
%>
<a href="textList.jsp?pageNow=1">[首页]</a> //首页,pageNow=1
<%
if(pageNow!=1){%>
<a href="textList.jsp?pageNow=<%=pageNow-1%>">[上一页]</a>//上一页,如果当前页是第一页,就不显示此链接,链接的参数pageNow=<%=pageNow-1%>
<%
}
for(int i=1;i<=pagecount;i++){
if(i==pageNow){
%>
[<%=i%>] //i=pageNow(当前页),链接不跳转
<%
}else{
%>
<a href="textList.jsp?pageNow=<%=i%>">[<%=i%>]</a>//显示页数链接
<%
}
}
if(pageNow!=pagecount){%>
<a href="textList.jsp?pageNow=<%=pageNow+1%>">[下一页]</a> //下一页,如果当前页最后一页,就不显示此链接,链接的参数pageNow=<%=pageNow+1%>
<%}
%>
<a href="textList.jsp?pageNow=<%=pagecount%>">[尾页]</a>//尾页,pageNow=pagecount(总页数,之前方法中获得数值)
<%}
//下面和上面雷同,区别在于<a>便签后面链接中多加了参数传输
if (getType != null && getSort == null) {
%>
<a href="textList.jsp?pageNow=1&type=<%=getType%>">[首页]</a>
<%
if(pageNow!=1){
%>
<a href="textList.jsp?pageNow=<%=pageNow-1 %>&type=<%=getType%>">[上一页]</a>
<%}
for(int i=1;i<=pagecount;i++){
if(i==pageNow){
%>
[<%=i %>]
<%}else{
%>
<a href="textList.jsp?pageNow=<%=i%>&type=<%=getType%>">[<%=i%>]</a>
<%
}}
if(pageNow!=pagecount){
%>
<a href="textList.jsp?pageNow=<%=pageNow+1 %>&type=<%=getType%>">[下一页]</a>
<%
}
%>
<a href="textList.jsp?pageNow=<%=pagecount %>&type=<%=getType%>">[尾页]</a>
<% }
if (getType != null && getSort != null) {
%>
<a href="textList.jsp?pageNow=1&type=<%=getType%>&sort=<%=getSort%>">[首页]</a>
<%
if(pageNow!=1){
%>
<a href="textList.jsp?pageNow=<%=pageNow-1 %>&type=<%=getType%>&sort=<%=getSort%>">[上一页]</a>
<%
}
for(int i=1;i<=pagecount;i++){
if(i==pageNow){
%>
[<%=i %>]
<% }else{%>
<a href="textList.jsp?pageNow=<%=i%>&type=<%=getType%>&sort=<%=getSort%>">[<%=i%>]</a>
<%}
}
if(pageNow!=pagecount){
%>
<a href="textList.jsp?pageNow=<%=pageNow+1 %>&type=<%=getType%>&sort=<%=getSort%>">[下一页]</a>
<%
}
%>
<a href="textList.jsp?pageNow=<%=pagecount%>&type=<%=getType%>&sort=<%=getSort%>">[尾页]</a>
<% }
}
%>
</div>
<div class="button_left">
<form action="editorPart.jsp">
<button>添加文章</button>
</form>
</div>
</div>
</body>
下面显示方法类中的代码(数据库方面的就不详细介绍了):
public class ArticleDao {
private static Connection conn;
private static PreparedStatement stmt;
private static ResultSet result;
private int pageSize=3;//希望一页有多少条数据
private int pageCount=0;//总共有多少页
private int rowCount=0;//一共有多少条数据,决定了有多少页
/**
* 获得所有的类型
* @return type类型列表
*/
public List<String> getAllType(){
List<String> typeList=new ArrayList<String>();
String strSql="select type FROM article group by type";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
result = stmt.executeQuery();
while (result.next()) {
String type=result.getString("type");
typeList.add(type);
}
} catch (SQLException e) {
System.out.println("查询分类失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return typeList;
}
/**
* 获得对应type的类型
* @return sort类型列表
*/
public List<String> getSortByType(String type){
List<String> sortList=new ArrayList<String>();
String strSql="select sort FROM article where type=? group by sort";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
stmt.setString(1, type);
result = stmt.executeQuery();
while (result.next()) {
String sort=result.getString("sort");
sortList.add(sort);
}
} catch (SQLException e) {
System.out.println("查询对应类型失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return sortList;
}
/**
* 获得type和sort都不为空下的数据
*这里分页sql语句用的是limit,如果数据库不支持的话可以用这一句
* SELECT TOP 页大小 *
FROM table
WHERE id NOT IN
(
SELECT TOP 页大小*(页数-1) id FROM table ORDER BY id
)
* @return artlist类型列表
*/
public List<Article> getArt(String type,String sort,int pageNow){
List<Article> artList=new ArrayList<Article>();
String strSql="select * FROM article WHERE type=? and sort=? order by isTop desc,textDate desc LIMIT "+pageSize * (pageNow-1)+","+pageSize+"";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
stmt.setString(1, type);
stmt.setString(2, sort);
result = stmt.executeQuery();
while (result.next()) {
Article article=new Article();
article.setId(result.getInt("id"));
article.setTitle(result.getString("title"));
article.setAuthor(result.getString("author"));
article.setContent(result.getString("content"));
article.setType(result.getString("type"));
article.setSort(result.getString("sort"));
article.setDate(result.getDate("textDate"));
article.setIsTop(result.getInt("isTop"));
artList.add(article);
}
} catch (SQLException e) {
System.out.println("查询对应类型失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return artList;
/**
* 查询type和sort不为空下的页总数
* @param type 类型,sort分类
* @return pagecount
*/
public int getArtcount(String type,String sort){
String strSql="select count(*) FROM article where type=? and sort=? ";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
stmt.setString(1, type);
stmt.setString(2, sort);
result = stmt.executeQuery();
while (result.next()) {
rowCount=result.getInt(1);
}
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}
else{
pageCount=(rowCount/pageSize)+1;
}
} catch (SQLException e) {
System.out.println("查询对应类型失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return pageCount;
}
/**
* 获得type不为空和sort为空下的数据
*这里分页sql语句用的是limit,如果数据库不支持的话可以用这一句
* SELECT TOP 页大小 *
FROM table
WHERE id NOT IN
(
SELECT TOP 页大小*(页数-1) id FROM table ORDER BY id
)
* @return artlist类型列表
*/
public List<Article> getArt(String type,int pageNow){
List<Article> artList=new ArrayList<Article>();
String strSql="select * FROM article WHERE type= ? order by isTop desc,textDate desc LIMIT "+pageSize * (pageNow-1)+","+pageSize+"";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
stmt.setString(1, type);
result = stmt.executeQuery();
while (result.next()) {
Article article=new Article();
article.setId(result.getInt("id"));
article.setTitle(result.getString("title"));
article.setAuthor(result.getString("author"));
article.setContent(result.getString("content"));
article.setType(result.getString("type"));
article.setSort(result.getString("sort"));
article.setDate(result.getDate("textDate"));
article.setIsTop(result.getInt("isTop"));
artList.add(article);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return artList;
}
/**
* 查询type不为空,sort为空下的页总数
* @param type 类型
* @return pagecount
*/
public int getArtcountByType(String type){
String strSql="select count(*) FROM article where type=? ";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
stmt.setString(1, type);
result = stmt.executeQuery();
while (result.next()) {
rowCount=result.getInt(1);
}
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}
else{
pageCount=(rowCount/pageSize)+1;
}
} catch (SQLException e) {
System.out.println("查询对应类型失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return pageCount;
}
/**
* 获得type,sort都为空下的数据
*这里分页sql语句用的是limit,如果数据库不支持的话可以用这一句
* SELECT TOP 页大小 *
FROM table
WHERE id NOT IN
(
SELECT TOP 页大小*(页数-1) id FROM table ORDER BY id
)
* @return artlist类型列表
*/
public List<Article> getArt(int pageNow){
List<Article> artList=new ArrayList<Article>();
String strSql="select * FROM article order by isTop desc,textDate desc LIMIT "+pageSize * (pageNow-1)+","+pageSize+"";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
result = stmt.executeQuery();
while (result.next()) {
Article article=new Article();
article.setId(result.getInt("id"));
article.setTitle(result.getString("title"));
article.setAuthor(result.getString("author"));
article.setContent(result.getString("content"));
article.setType(result.getString("type"));
article.setSort(result.getString("sort"));
article.setDate(result.getDate("textDate"));
article.setIsTop(result.getInt("isTop"));
artList.add(article);
}
} catch (SQLException e) {
System.out.println("查询对应类型失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return artList;
}
/**
* 查询type,sort都为空下的页总数
* @param
* @return pagecount
*/
public int getArtcount(){
String strSql="select count(*) FROM article ";
try {
conn = JdbcUtil.getConnection();
stmt = conn.prepareStatement(strSql);
result = stmt.executeQuery();
while (result.next()) {
rowCount=result.getInt(1);
}
if(rowCount%pageSize==0){
pageCount=rowCount/pageSize;
}
else{
pageCount=(rowCount/pageSize)+1;
}
} catch (SQLException e) {
System.out.println("查询对应类型失败!");
e.printStackTrace();
} finally {
JdbcUtil.close(result, stmt, conn);
}
return pageCount;
}
}