论坛首页 入门技术论坛

和大家一起分享一下,我做网站时,用struts做的简单的分页

浏览 1384 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-12-19   最后修改:2008-12-20
我做的是购物网,下面是拿后台管理用户 分页的代码,
下面是struts中 Action的的代码,主要逻辑都在这里面,jsp部分主要是用struts迭代标签输出,
如果有朋友需要,可以给我留言.....,呵呵,欢迎朋友提出更好的建议.....

package com.yourcompany.struts.action;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.CachedRowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.sql.sqlbean.Bean;
import com.sun.rowset.CachedRowSetImpl;
import com.util.means.users;

public class SeeuserAction extends Action {

int page=1;  //页码的计数
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
request.setAttribute("user", "zhu");
List<users> list = new ArrayList<users>();
Bean bean = new Bean(); //连接数据的方法都封装在Bean里了,这里主要是为了调用方
users fuser = new users(); //users是自己定义的实体类
String delete = request.getParameter("delete");
bean.executeUpdate("Delete from userinfo where UserName='"+delete+"' ");  //用户的删除
System.out.println("delete=" + delete);
ResultSet rs = bean.executeQuery("select * from userinfo");
int m = getSum(rs); //总行数
int n = fuser.getPageSize();
int pageAllCount = ((m%n)==0)?(m/n):(m/n+1); //总页数
String textpage = request.getParameter("textpage"); //拿到text里的页码
int textpage2;
if(textpage!=null){
textpage2 = Integer.parseInt(textpage);
System.out.println("textpage=" + textpage2);
if(textpage2>pageAllCount){
request.setAttribute("textpageError","<script>alert('sorry!无此页面')</script>");
}else{
page=textpage2;
}
}


if("2".equals(request.getParameter("showPage"))){
page=page+1;
System.out.println("page=" + page);
if(page>pageAllCount){
page=1;
}
}
if("1".equals(request.getParameter("showPage"))){
page=page-1;
System.out.println("page=" + page);
if(page<=0){
page=pageAllCount;
}
}
request.setAttribute("page", page); //传当前页面
request.setAttribute("pageAllCount", pageAllCount); //传当前页面
fuser.setShowPage(page);
fuser.setPageAllCount(pageAllCount);

try {
int ss = fuser.getShowPage();
rs.absolute((fuser.getShowPage()-1)*fuser.getPageSize()+1);
if(ss*fuser.getPageSize()<=m){
for(int i=1;i<=fuser.getPageSize();i++)
{
users usern = new users();
usern.setUserName(rs.getString(1));
usern.setSex(rs.getString(2));
usern.setLogintime(rs.getString(11));
list.add(usern);
rs.next();
}
}else{
for(int i=1;i<=m%n;i++)
{
users usern = new users();
usern.setUserName(rs.getString(1));
usern.setSex(rs.getString(2));
usern.setLogintime(rs.getString(11));
list.add(usern);
rs.next();
}
}

} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("know",list);
return mapping.findForward("seeuser");
}

/**
* 拿到总书数
*/
public int getSum(ResultSet rs){
CachedRowSetImpl rowSet;
int m = 0;
try {
rowSet = new CachedRowSetImpl();
rowSet.populate(rs);
rowSet.last();
m = rowSet.getRow();
} catch (SQLException e) {
e.printStackTrace();
}
return m;
}
}
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics