Struts2中实现自定义分页标签2

UserDAOImpl.java

public List allFindUser(String userID,int currentPage,int lineSize) {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		// sql = "select * from users where userID like '%" + userID
		// + "%' order by userID asc";
		sql = "select * from users where userID like ? order by ID asc " +
				"limit " + ((currentPage-1)*lineSize) + "," + lineSize;
		try {
			conn = new DBConnection();
			prst = conn.getConnectionDB().prepareStatement(sql);
			prst.setString(1, "%" + userID + "%");
			rs = prst.executeQuery();

			while (rs.next()) {
				User user = new User();
				user.setID(rs.getInt(1));
				/* 返红*/
				if (!"".equals(userID)) {
					String userID2 = rs.getString(2);
					userID2 = userID2.replaceAll(userID,
							"<font color=\"#ff0000\">" + userID + "</font>");
					user.setUserID(userID2);
				} else
					user.setUserID(rs.getString(2));

				user.setUserPassword(rs.getString(3));
				user.setName(rs.getString(4));
				user.setCompany(rs.getString(5));
				user.setDepart(rs.getString(6));
				user.setEmail(rs.getString(7));
				user.setFields(rs.getString(8));
				user.setSystemRole(rs.getString(9));
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("allUser1持久层数据库出错");
		} finally {
			try {
				rs.close();
				prst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("allUser2持久层数据库出错");
			}
			conn.close();
		}
		return list;
	}

 UserAction.java

private List listUser;//数据列表
private String url;// 页面连接路径
private String currentPage;//定义一个当前是第几页
private String pageSize;// 计算出总页数

public String allFindUser() {
		// 页面连接路径
		// String actionUrl = "allUserList.action";
		// 定义如下分页变量
		int lineSize = 5;// 1、定义没页要显示的记录数,默认是10条每页
		// int currentPage3 = 1;// 2、定义一个当前是第几页
		// int pageSize3 = 0;// 计算出总页数
		// int allRecorders = 0;// 总记录数 / 每页显示的记录数
		String userID;
		WorkGroupFacade wg = new WorkGroupFacadeImpl();
		HttpServletRequest request = ServletActionContext.getRequest();

		userID = request.getParameter("userId");
		if (userID == null)
			userID = "";
		pageSize = request.getParameter("pageSize");
		if (pageSize == null)
			pageSize = String
					.valueOf((userFacede.userCount(userID) + lineSize - 1)
							/ lineSize);
		currentPage = request.getParameter("cpage");
		if (currentPage == null)
			currentPage = "1";
		url = "allUserList.action";
		listUser = new ArrayList();
		List listUserTemp;
		listUserTemp = userFacede.allFindUser(userID, Integer
				.parseInt(currentPage), lineSize);
		Iterator it = listUserTemp.iterator();
		while (it.hasNext()) {
			User user = (User) it.next();
			user.setListGroup(wg.selectWorkGroup(user.getFields()));
			listUser.add(user);
		}
		return "success";
	}

userList.jsp

<tr>
					<td colspan="10" align="right">
						<tangs:pages cpage="%{currentPage}" total="%{pageSize}"
							url="%{url}" />
						第
						<s:property value="currentPage" />
						页 共
						<s:property value="pageSize" />
						页
					</td>
				</tr>

 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值