推荐于2016-01-12
展开全部
我现在也在做这个,给你个代码看看
//实现分页用到三个类,分别为NewsDao.java(接口),NewsDaoImpl.java(实现),ListAction.java(调用方法)。
//当然还有配好连接池的Mysql连接辅助类。在此不做过多介绍。
NewsDao.java
import java.util.List;
import com.sy.vo.News;
public interface NewsDao {
public List<News> queryByPage(int pageSize,int pageNow);
public int count();
}
NewsDaoImpl.java
public class NewsDaoImpl implements NewsDao {
//获取分页新闻列表
@SuppressWarnings("unchecked")
public List<News> queryByPage(int i,int pageSize){
List<News> newss=new ArrayList();
PreparedStatement pstmt = null ;
String sql = null ;
ResultSet rs = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
sql = "select * from struts2new order by id asc limit " + i + "," + pageSize;
try
{
pstmt = dbc.getConnection().prepareStatement(sql);
rs = pstmt.executeQuery() ;
while(rs.next())
{
News news=new News();
news.setId(rs.getInt("id"));
news.setName(rs.getString("name"));
news.setTitle(rs.getString("title"));
news.setDate(rs.getString("date"));
news.setEmail(rs.getString("email"));
news.setContent(rs.getString("content"));
i++;
newss.add(news);
}
rs.close() ;
pstmt.close() ;
}
catch(Exception e)
{
System.out.println(e) ;
}
finally
{
dbc.close();
}
return newss;
}
//查询总行数 查询到总行数。
public int count() {
int intRowCount = 0;//总行数
PreparedStatement pstmt = null ;
String sql = null ;
ResultSet rs = null ;
DataBaseConnection dbc = null ;
dbc = new DataBaseConnection() ;
sql = "select count(id) from struts2new order by id asc";
try
{
pstmt = dbc.getConnection().prepareStatement(sql);
rs = pstmt.executeQuery();
rs.next();//游标指向第一行
intRowCount=rs.getInt(1);//取得总行数
rs.close() ;
pstmt.close() ;
}
catch(Exception e)
{
System.out.println(e) ;
}
finally
{
dbc.close();
}
return intRowCount;
}
}
ListAction.java
public class ListAction extends ActionSupport {
private static final long serialVersionUID = 1L;
int i=1;//中间变量
private int k;//储存最大页面数
private int pageNow=1; //页码数,初始为1
private int pageSize = 5 ; //页面行数
private int intRowCount;//总行数
private int intPageCount;//总页数
private Admin admin;
private List<Admin> Adminss;
private News news;
@SuppressWarnings("unchecked")
private List<News> Newss;
private int id;
private int aid;
@Override
//显示新闻列表
public String execute() throws Exception {
NewsDao npage=new NewsDaoImpl();
intRowCount=npage.count();
k=(intRowCount + pageSize - 1) / pageSize;
intPageCount = (intRowCount + pageSize - 1) / pageSize;//计算出总页数
if(pageNow<1){
pageNow=1;
}
if(pageNow > intPageCount)
pageNow=intPageCount;
i = (pageNow -1)*pageSize;
NewsDao nlist=new NewsDaoImpl();
if(null!=nlist.queryByPage(i,pageSize)){
Newss = nlist.queryByPage(i,pageSize);
return SUCCESS;
}else{
return "failure";
}
}
..
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="news" extends="struts-default">
..
<!-- 查看新闻列表 -->
<action name="list" class="com.sy.action.ListAction">
<result>/listNews.jsp</result>
<result name="failure">/Showfailure.jsp</result>
</action>
</package>
</struts>
显示页面listNews.jsp
共<s:property value="intRowCount"/>条记录
第<s:property value="pageNow"/>页
<s:url id="url_pre" value="list.action">
<s:param name="pageNow" value="pageNow-1"></s:param>
</s:url>
<s:url id="url_next" value="list.action">
<s:param name="pageNow" value="pageNow+1"></s:param>
</s:url>
<s:iterator value="Newss" status="status">
<s:url id="url" value="list.action">
<s:param name="pageNow" value="pageNow"/>
</s:url>
</s:iterator>
<s:if test="pageNow==1">
<s:a href="%{url_pre}">最前一页</s:a>
</s:if>
<s:else>
<s:a href="%{url_pre}">上一页</s:a>
</s:else>
<s:if test="pageNow==k">
<s:a href="%{url_next}">最后一页</s:a>
</s:if>
<s:else>
<s:a href="%{url_next}">下一页</s:a>
</s:else>
更多追问追答
追问
这样的话,要是你在浏览过程又增加了10条新闻,下一页又会显示当前页得了
追答
在浏览过程又增加了10条新闻是后台增加的,又不是前台。他是插入到数据库的数据行最后边的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询