struts2之使用oracle分页(10)

 

ToolsUtil

 

//每页显示的记录数
public static final int NUM_PER_PAGE=5;
/*
* java.util.Date转java.sql.Date
*/
public static Date utilDateToSqlDate(java.util.Date d){
Date date = new Date(d.getTime());
return date;
}
public static String getPageSQL(String tableName,String orderedColumn){
return "select * from (select t2.*,rownum r from "
+ "(select * from "+tableName+" t1 order by "+orderedColumn+") t2) t3 where r>? and r<=?";
}
//获取总页数
public static int getPageCount(int count,int NumPerPage){
return (count%NumPerPage==0)?(count/NumPerPage):(count/NumPerPage+1);
}

 

 

dao

//列表,带有分页
public List<User> findAllByPage(int pagenum);
//数据的总数
public int Count()

 

 

@Override
public List<User> findAllByPage(int pagenum) {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = ToolsUtil.getPageSQL("t_user", "id");
p = connection.prepareStatement(sql);
//pagenum 页码
//pagenum页开始的第n条数据
int begin = (pagenum-1)*ToolsUtil.NUM_PER_PAGE;
//pagenum页结束的第n条数据
int end = pagenum*ToolsUtil.NUM_PER_PAGE;
p.setInt(1, begin);
p.setInt(2, end);
set = p.executeQuery();
List<User> list = new ArrayList<User>();
while(set.next()){
User user = new User();
user.setId(set.getInt("id"));
user.setUsername(set.getString("username"));
user.setPassword(set.getString("password"));
user.setSex(set.getInt("sex"));
user.setAge(set.getInt("age"));
user.setSalary(set.getDouble("salary"));
user.setAddress(set.getString("address"));
user.setPhone(set.getString("phone"));
list.add(user);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}
@Override
public int Count() {
// TODO Auto-generated method stub
try {
connection = DBUtil.getConnection();
String sql = "select count(*) from t_user";
p = connection.prepareStatement(sql);
set = p.executeQuery();
int count = 0;
if (set.next()) {
//查询出一个int值,如果有,则set.getInt(1)赋值给num
int num = set.getInt(1);
count = num;
}
return count;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}

servlet

//列表,分页
public List<User> findAllByPage(int pagenum);
public int Count();

 

 

 

@Override
public List<User> findAllByPage(int pagenum) {
// TODO Auto-generated method stub
return userDao.findAllByPage(pagenum);
}

@Override
public int Count() {
// TODO Auto-generated method stub
return userDao.Count();
}

 

action

//全局变量,getset方法

 

//该属性用于接收前台页面提交的页码
private int pagenum = 1;
//总页数,用于传到前台进行循环,显示所有页码
private int pageCount;

 

public String listByPage(){
userlist = userService.findAllByPage(pagenum);
int totalCount = userService.Count();
pageCount = ToolsUtil.getPageCount(totalCount, ToolsUtil.NUM_PER_PAGE);
return SUCCESS;
}

 

前台

 

<!--页码
begin 从第一条数据
end 到最后一条数据
step 依次加一
var="p" 代表${pageCount } -->
<c:forEach begin="1" end="${pageCount }" step="1" var="p">
<c:if test="${pagenum==p }">
${p}
</c:if>
<c:if test="${pagenum != p }">
<a href="user_list?pagenum=${p }">${p }</a>
</c:if>
</c:forEach>
跳转到<input type="text" id="page" size="2px" value="${pagenum }"/>页
<a id="pa" onclick="pade('page','pa','user_list')">确定</a>

 

<!--pade('page','pa','user_list')

分别是input的id,a标签的id,和action-->

 

 

<script type="text/javascript">
function pade(pageid,paid,action){
var page = document.getElementById(pageid).value;
var pageCount = ${pageCount};
if(page<0){
document.getElementById(pa).href=action+"?pagenum = ${pagenum}";
}
else if(page<=pageCount){
document.getElementById(paid).href = action + "?pagenum=" + page;
}
else{
document.getElementById(pa).href=action+"?pagenum ="+pageCount;
}
}
</script>

 

posted @ 2017-11-10 15:53  但为君故L  阅读(194)  评论(0编辑  收藏  举报