struts2 +hibernate3.2+ spring2.0 分页


首先是后台代码:
page
public PageModel findAllUser(int offset, int pagesize) {
		String queryCountHql = "select count(*) from Users ";

		Query query = getSession().createQuery(queryCountHql);
		int total = ((Long) query.uniqueResult()).intValue();
		
		List datas = getSession().createQuery("from Users as u order by u.id")
		.setFirstResult(offset).setMaxResults(pagesize).list();
		PageModel pm = new PageModel();
		pm.setDatas(datas);
		pm.setTotal(total);
		if(total % pagesize == 0 )
		   pm.setPageTotal(total / pagesize);
		else
		   pm.setPageTotal(total / pagesize +1);
		return pm;
	}


PageModel.java
package com.test.util;

import java.util.List;

public class PageModel {
	// 总记录数
	private int total;
	// 当前页的数据集
	private List datas;
	// 一共几页
	private int pageTotal;
	// 每页多少行
	private int pageSize;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getDatas() {
		return datas;
	}

	public void setDatas(List datas) {
		this.datas = datas;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageTotal() {
		return pageTotal;
	}

	public void setPageTotal(int pageTotal) {
		this.pageTotal = pageTotal;
	}

}

Action代码:
package com.test.action;

import com.opensymphony.xwork2.ActionSupport;
import com.test.service.UsersService;
import com.test.util.PageModel;

public class ListPageAction extends ActionSupport {
	private UsersService userService;
	private PageModel pageModel;
	private int pageNow ;
	public UsersService getUserService(){ 
		return userService;
	}

	public void setUserService(UsersService userService) {
		this.userService = userService;
	}

	@Override
	public String execute() throws Exception {
		this.pageModel= userService.findAllUser(pageNow * pageModel.getPageSize(), pageModel.getPageSize());
		return SUCCESS;
	}

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		if(pageNow < 0 )
			this.pageNow = 0;
		else
		    this.pageNow = pageNow;
	}


}

applicationContext.xml
<bean id="usersDao" class="com.test.dao.impl.UsersDaoImpl" scope="singleton">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>
	<bean id="userService" class="com.test.service.impl.UsersServiceimpl">
		<property name="userDao" ref="usersDao"></property>
	</bean>
	<bean id="pageModel" class="com.test.util.PageModel" >
		<property name="pageSize" value="6"></property>
	</bean>
	<!-- Action -->
	<bean id="saveUsersAction" class="com.test.action.SaveUserAction" scope="prototype">
		<property name="usersService" ref="userService"></property>
	</bean>
	<bean id="listPageUserAction" class="com.test.action.ListPageAction" scope="prototype">
		<property name="userService" ref="userService"></property>
		<property name="pageModel" ref="pageModel"></property>
	</bean>


jsp页
<s:iterator id="iter" value="pageModel.datas" status="i">
				<tr>
					<td>
						<s:property value="#iter.id" />
					</td>
					<td>
						<s:property value="#iter.firstname" />
					</td>
					<td>
						<s:property value="#iter.lastname" />
					</td>
					<td>
						<s:property value="#iter.age" />
					</td>
					<td>
						<s:a href="deleteUser.action?users.id=%{#iter.id}">delete</s:a>
					</td>
					<td>
						<s:a href="updatePUser.action?users.id=%{#iter.id}">update</s:a>
					</td>
				</tr>
			</s:iterator>

		</table>
		<s:url id="url_first" value="listUser.action">
			<s:param name="pageNow" value="0"></s:param>
		</s:url>

		<s:url id="url_pre" value="listUser.action">
			<s:param name="pageNow" value="pageNow-1"></s:param>
		</s:url>

		<s:url id="url_next" value="listUser.action">
			<s:param name="pageNow" value="pageNow+1"></s:param>
		</s:url>
		<s:url id="url_end" value="listUser.action">
			<s:param name="pageNow" value="pageModel.pageTotal - 1"></s:param>
		</s:url>

		<s:if test="pageNow > 0">
			<s:a href="%{url_first}" disabled="pageModel.isFirstPage">首页</s:a>
		</s:if>
		<s:else>
		首页
		</s:else>
		<s:if test="pageNow > 0">
			<s:a href="%{url_pre}" disabled="pageModel.isPrev">上一页</s:a>
		</s:if>
		<s:else>
		上一页
		</s:else>
		<s:if test="pageModel.pageTotal > pageNow+1 ">
			<s:a href="%{url_next}" disabled="pageModel.isNext">下一页</s:a>
		</s:if>
		<s:else>
		下一页
		</s:else>
		<s:if test="pageModel.pageTotal > pageNow+1">
			<s:a href="%{url_end}">最后一页</s:a>
		</s:if>
		<s:else>
		最后一页
		</s:else>
		当前:
		<s:property value="pageNow+1" /> / 
		<s:property value="pageModel.pageTotal" id="i" />页
		<br><br><br>


本人测试过没有问题,
如有问题就留言,谢谢,希望提出宝贵的意见。

你可能感兴趣的:(java,xml,jsp,配置管理)