struts2+hibernate+spring分页实现(DAO,Service,Action三层架构搭配)
简介:
Java代码
import java.util.List;
public interface Pagination {
...
- <PRE class=java name="code"> </PRE>
- import java.util.List;
-
- public interface Pagination {
- public boolean isFirst();
- public boolean isLast();
- public boolean hasNext();
- public boolean hasPrevious();
- public int getMaxElements();
- public int getMaxPages();
- public int getNext();
- public int getPrevious();
- public int getPageSize();
- public int getPageNumber();
- public List<Object> getList();
- public void setPageSize(int pageSize);
- public void setPageNumber(int pageNumber);
- }
- <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span> </span></span></li></ol></div><pre class="java" style="display: none;" name="code"> </pre>
- import java.util.List;
-
- public interface Pagination {
- public boolean isFirst();
- public boolean isLast();
- public boolean hasNext();
- public boolean hasPrevious();
- public int getMaxElements();
- public int getMaxPages();
- public int getNext();
- public int getPrevious();
- public int getPageSize();
- public int getPageNumber();
- public List<Object> getList();
- public void setPageSize(int pageSize);
- public void setPageNumber(int pageNumber);
- }
- public interface IPaginationDao {
- public List<Object> listByPage(String hql,int pageSize, int pageNumber);
- public int listCount(String hql);
- }
- public interface IPaginationDao {
- public List<Object> listByPage(String hql,int pageSize, int pageNumber);
- public int listCount(String hql);
- }
- public class PaginationDaoImpl extends HibernateDaoSupport implements IPaginationDao{
-
- public List<Object> listByPage(final String hql, final int pageSize, final int pageNumber) {
-
- return (List<Object>)this.getHibernateTemplate().executeFind( new HibernateCallback(){
- public Object doInHibernate(Session session) throws SQLException,HibernateException {
- Query q = session.createQuery(hql);
- q.setFirstResult((pageNumber - 1) * pageSize);
- q.setMaxResults(pageSize);
- List<Object> list = (List<Object>) q.list();
- return list;
- }
- });
- }
-
- public int listCount(String hql) {
-
- return this.getHibernateTemplate().find(hql).size();
- }
-
- }
- public class PaginationDaoImpl extends HibernateDaoSupport implements IPaginationDao{
-
- public List<Object> listByPage(final String hql, final int pageSize, final int pageNumber) {
-
- return (List<Object>)this.getHibernateTemplate().executeFind( new HibernateCallback(){
- public Object doInHibernate(Session session) throws SQLException,HibernateException {
- Query q = session.createQuery(hql);
- q.setFirstResult((pageNumber - 1) * pageSize);
- q.setMaxResults(pageSize);
- List<Object> list = (List<Object>) q.list();
- return list;
- }
- });
- }
-
- public int listCount(String hql) {
-
- return this.getHibernateTemplate().find(hql).size();
- }
-
- }
- <PRE class=java name="code">public interface IPaginationServ { public Pagination getProductByPage(String hql,int pageSize, int pageNumber);}</PRE>
-
- <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">interface</span></strong></span><span> IPaginationServ { </span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> Pagination getProductByPage(String hql,</span><span class="keyword"><strong><span style="color: #7f0055;">int</span></strong></span><span> pageSize, </span><span class="keyword"><strong><span style="color: #7f0055;">int</span></strong></span><span> pageNumber);} </span></span></li></ol></div><pre class="java" style="display: none;" name="code">public interface IPaginationServ { public Pagination getProductByPage(String hql,int pageSize, int pageNumber);}</pre>
-
- public class PaginationServImpl implements IPaginationServ{
- private IPaginationDao pagingDao;
- public Pagination getProductByPage(String hql, int pageSize, int pageNumber) {
- int total =pagingDao.listCount(hql);
- List<Object> list=pagingDao.listByPage(hql, pageSize, pageNumber);
- Pagination paging=new PaginationImpl(list,total,pageSize,pageNumber);
- return paging;
- }
- public IPaginationDao getPagingDao() {
- return pagingDao;
- }
- public void setPagingDao(IPaginationDao pagingDao) {
- this.pagingDao = pagingDao;
- }
-
- }
- public class PaginationServImpl implements IPaginationServ{
- private IPaginationDao pagingDao;
- public Pagination getProductByPage(String hql, int pageSize, int pageNumber) {
- int total =pagingDao.listCount(hql);
- List<Object> list=pagingDao.listByPage(hql, pageSize, pageNumber);
- Pagination paging=new PaginationImpl(list,total,pageSize,pageNumber);
- return paging;
- }
- public IPaginationDao getPagingDao() {
- return pagingDao;
- }
- public void setPagingDao(IPaginationDao pagingDao) {
- this.pagingDao = pagingDao;
- }
-
- }
- <bean id="pagingDao"
- class="××××.dao.impl.PaginationDaoImpl">
- <property name="sessionFactory" >
- <ref bean="sessionFactory"/>
- </property>
- </bean>
- <bean id="pagingDao"
- class="××××.dao.impl.PaginationDaoImpl">
- <property name="sessionFactory" >
- <ref bean="sessionFactory"/>
- </property>
- </bean>
- <bean id="pagingServ"
- class="com.cnkf.wushi.service.impl.PaginationServImpl">
- <property name="pagingDao" >
- <ref bean="pagingDao"/>
- </property>
- </bean>
- <bean id="pagingServ"
- class="com.cnkf.wushi.service.impl.PaginationServImpl">
- <property name="pagingDao" >
- <ref bean="pagingDao"/>
- </property>
- </bean>
- <PRE class=JSP name="code"> </PRE>
- <PRE class=JSP name="code"><<A href="mailto:%@taglib">%@taglib</A> uri="<A href="http:
- <PRE class=JSP name="code"><a href="findAllDisplay.action"><h1>displayTag分页</h1></a>
- <s:iterator value="page.list" id="pr">
- <table width="300" border="1" bordercolor="000000"
- style="table-layout: fixed; word-break: break-all">
- <tr>
- <td width="100" bordercolor="ffffff">
- 编号:
- </td>
- <td width="500" bordercolor="ffffff">
- <s:property value="id" />
- </td>
- </tr>
- <tr>
- <td bordercolor="ffffff">
- 名称:
- </td>
- <td bordercolor="ffffff">
- <s:property value="name" />
- </td>
- </tr>
- <tr>
- <td bordercolor="ffffff">
- 价格:
- </td>
- <td bordercolor="ffffff">
- <s:property value="price" />
- </td>
- </tr>
- <tr>
- <td bordercolor="ffffff">
- 类型:
- </td>
- <td bordercolor="ffffff">
- <s:property value="type" />
- </td>
- </tr>
- </table>
- <br>
- </s:iterator>
- <pg:pager url="findAllPg.action"
- items="${page.maxElements}"
- maxPageItems="${pageSize}" maxIndexPages="5">
- <pg:index>
- <pg:first unless="current">
- <a href="${pageUrl}&pageNumber=${pageNumber}">首页</a>
- </pg:first>
- <pg:prev>
- <a href="${pageUrl}&pageNumber=${pageNumber}">上一页(${pageNumber})</a>
- </pg:prev>
- <pg:pages>
- <a href="${pageUrl}&pageNumber=${pageNumber}">${pageNumber}</a>
- </pg:pages>
- <pg:next>
- <a href="${pageUrl}&pageNumber=${pageNumber}">下一页(${pageNumber})</a>
- </pg:next>
- <pg:last unless="current">
- <a href="${pageUrl}&pageNumber=${pageNumber}">尾页</a>
- </pg:last>
- </pg:index>
- </pg:pager>
- </center></PRE>
-
- <PRE class=java name="code"><%@taglib uri="http://displaytag.sf.net" prefix="display"%>
- <center>
- <a href="findAllPg.action"><h1>Pager分页</h1></a>
- <display:table name="page.list" id="dt" export="true" class="" partialList="true" size="page.maxElements" pagesize="10"
- requestURI="findAllDisplay.action">
- <display:column sortable="true" property="id" title="编号">
- </display:column>
-
- <display:column property="name" title="名称">
- </display:column>
-
- <display:column property="price" title="价格">
- </display:column>
-
- <display:column property="type" title="类型">
- </display:column>
-
- <display:column title="操作">
- <a href="#">编辑</a>
- <a href="#">删除</a>
- </display:column>
- </display:table>
-
- </center></PRE>
-
- <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span><%</span><span class="annotation"><span style="color: #646464;">@taglib</span></span><span> uri=</span><span class="string"><span style="color: #0000ff;">"http://displaytag.sf.net"</span></span><span> prefix=</span><span class="string"><span style="color: #0000ff;">"display"</span></span><span>%> </span></span></li><li><span><center> </span></li><li><span> <a href=</span><span class="string"><span style="color: #0000ff;">"findAllPg.action"</span></span><span>><h1>Pager分页</h1></a> </span></li><li><span> <display:table name=</span><span class="string"><span style="color: #0000ff;">"page.list"</span></span><span> id=</span><span class="string"><span style="color: #0000ff;">"dt"</span></span><span> export=</span><span class="string"><span style="color: #0000ff;">"true"</span></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color: #0000ff;">""</span></span><span> partialList=</span><span class="string"><span style="color: #0000ff;">"true"</span></span><span> size=</span><span class="string"><span style="color: #0000ff;">"page.maxElements"</span></span><span> pagesize=</span><span class="string"><span style="color: #0000ff;">"10"</span></span><span> </span></li><li><span> requestURI=</span><span class="string"><span style="color: #0000ff;">"findAllDisplay.action"</span></span><span>> </span></li><li><span> <display:column sortable=</span><span class="string"><span style="color: #0000ff;">"true"</span></span><span> property=</span><span class="string"><span style="color: #0000ff;">"id"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"编号"</span></span><span>> </span></li><li><span> </display:column> </span></li><li><span> </span></li><li><span> <display:column property=</span><span class="string"><span style="color: #0000ff;">"name"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"名称"</span></span><span>> </span></li><li><span> </display:column> </span></li><li><span> </span></li><li><span> <display:column property=</span><span class="string"><span style="color: #0000ff;">"price"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"价格"</span></span><span>> </span></li><li><span> </display:column> </span></li><li><span> </span></li><li><span> <display:column property=</span><span class="string"><span style="color: #0000ff;">"type"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"类型"</span></span><span>> </span></li><li><span> </display:column> </span></li><li><span> </span></li><li><span> <display:column title=</span><span class="string"><span style="color: #0000ff;">"操作"</span></span><span>> </span></li><li><span> <a href=</span><span class="string"><span style="color: #0000ff;">"#"</span></span><span>>编辑</a> </span></li><li><span> <a href=</span><span class="string"><span style="color: #0000ff;">"#"</span></span><span>>删除</a> </span></li><li><span> </display:column> </span></li><li><span> </display:table> </span></li><li><span> </span></li><li><span></center> </span></li></ol></div><pre class="java" style="display: none;" name="code"><%@taglib uri="http://displaytag.sf.net" prefix="display"%>
- <center>
- <a href="findAllPg.action"><h1>Pager分页</h1></a>
- <display:table name="page.list" id="dt" export="true" class="" partialList="true" size="page.maxElements" pagesize="10"
- requestURI="findAllDisplay.action">
- <display:column sortable="true" property="id" title="编号">
- </display:column>
-
- <display:column property="name" title="名称">
- </display:column>
-
- <display:column property="price" title="价格">
- </display:column>
-
- <display:column property="type" title="类型">
- </display:column>
-
- <display:column title="操作">
- <a href="#">编辑</a>
- <a href="#">删除</a>
- </display:column>
- </display:table>
-
- </center></pre>
-
- <PRE class=java name="code">public class ProductAction extends ActionSupport {
- private static final long serialVersionUID = 3953659778944144652L;
-
- private IProductServ productServ;
-
- private IPaginationServ pagingServ;
-
- private Product pr;
-
- private List<Product> list;
-
- private Pagination page;
-
- private String pageNumber = "1";
-
- private int pageSize = 10;
-
- public String findAllDisplay() {
- String hql = "from Product p";
-
- String paramName = (new ParamEncoder("dt")
- .encodeParameterName(TableTagParameters.PARAMETER_PAGE));
- HttpServletRequest request = ServletActionContext.getRequest();
- try {
- Integer.parseInt(request.getParameter(paramName));
- pageNumber=request.getParameter(paramName);
- } catch (Exception e) {
- pageNumber = "1";
- }
- page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));
- return SUCCESS;
- }
- public String findAllPg() {
- String hql = "from Product p";
- try {
- Integer.parseInt(pageNumber);
- } catch (Exception e) {
- pageNumber = "1";
- }
- page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));
- return SUCCESS;
- }</PRE>
-
- <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">class</span></strong></span><span> ProductAction </span><span class="keyword"><strong><span style="color: #7f0055;">extends</span></strong></span><span> ActionSupport { </span></span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">static</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">final</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">long</span></strong></span><span> serialVersionUID = 3953659778944144652L; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> IProductServ productServ; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> IPaginationServ pagingServ; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> Product pr; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> List<Product> list; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> Pagination page; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> String pageNumber = </span><span class="string"><span style="color: #0000ff;">"1"</span></span><span>; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">int</span></strong></span><span> pageSize = </span><span class="number"><span style="color: #c00000;">10</span></span><span>; </span></li><li><span> </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> String findAllDisplay() { </span></li><li><span> String hql = </span><span class="string"><span style="color: #0000ff;">"from Product p"</span></span><span>; </span></li><li><span> </span><span class="comment"><span style="color: #008200;">// 这是displaytag获得pageNumber的方法,dt是displaytag 的id属性相对应的。比较特殊 </span></span><span> </span></li><li><span> String paramName = (</span><span class="keyword"><strong><span style="color: #7f0055;">new</span></strong></span><span> ParamEncoder(</span><span class="string"><span style="color: #0000ff;">"dt"</span></span><span>) </span></li><li><span> .encodeParameterName(TableTagParameters.PARAMETER_PAGE)); </span></li><li><span> HttpServletRequest request = ServletActionContext.getRequest(); </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">try</span></strong></span><span> { </span></li><li><span> Integer.parseInt(request.getParameter(paramName)); </span></li><li><span> pageNumber=request.getParameter(paramName); </span></li><li><span> } </span><span class="keyword"><strong><span style="color: #7f0055;">catch</span></strong></span><span> (Exception e) { </span></li><li><span> pageNumber = </span><span class="string"><span style="color: #0000ff;">"1"</span></span><span>; </span></li><li><span> } </span></li><li><span> page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber)); </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">return</span></strong></span><span> SUCCESS; </span></li><li><span> } </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> String findAllPg() { </span></li><li><span> String hql = </span><span class="string"><span style="color: #0000ff;">"from Product p"</span></span><span>; </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">try</span></strong></span><span> { </span></li><li><span> Integer.parseInt(pageNumber); </span></li><li><span> } </span><span class="keyword"><strong><span style="color: #7f0055;">catch</span></strong></span><span> (Exception e) { </span></li><li><span> pageNumber = </span><span class="string"><span style="color: #0000ff;">"1"</span></span><span>; </span></li><li><span> } </span></li><li><span> page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber)); </span></li><li><span> </span><span class="keyword"><strong><span style="color: #7f0055;">return</span></strong></span><span> SUCCESS; </span></li><li><span> } </span></li></ol></div><pre class="java" style="display: none;" name="code">public class ProductAction extends ActionSupport {
- private static final long serialVersionUID = 3953659778944144652L;
-
- private IProductServ productServ;
-
- private IPaginationServ pagingServ;
-
- private Product pr;
-
- private List<Product> list;
-
- private Pagination page;
-
- private String pageNumber = "1";
-
- private int pageSize = 10;
-
- public String findAllDisplay() {
- String hql = "from Product p";
-
- String paramName = (new ParamEncoder("dt")
- .encodeParameterName(TableTagParameters.PARAMETER_PAGE));
- HttpServletRequest request = ServletActionContext.getRequest();
- try {
- Integer.parseInt(request.getParameter(paramName));
- pageNumber=request.getParameter(paramName);
- } catch (Exception e) {
- pageNumber = "1";
- }
- page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));
- return SUCCESS;
- }
- public String findAllPg() {
- String hql = "from Product p";
- try {
- Integer.parseInt(pageNumber);
- } catch (Exception e) {
- pageNumber = "1";
- }
- page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));
- return SUCCESS;
- }</pre>
-
- <PRE class=java name="code"><action name="findAllDisplay" class="com.cnkf.template.action.ProductAction" method="findAllDisplay">
- <result >/welcomeDisplay.jsp</result>
- </action>
- <action name="findAllPg" class="com.cnkf.template.action.ProductAction" method="findAllPg">
- <result >/welcomepg.jsp</result>
- </action></PRE>
-
- <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span><action name=</span><span class="string"><span style="color: #0000ff;">"findAllDisplay"</span></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color: #0000ff;">"com.cnkf.template.action.ProductAction"</span></span><span> method=</span><span class="string"><span style="color: #0000ff;">"findAllDisplay"</span></span><span>> </span></span></li><li><span> <result >/welcomeDisplay.jsp</result> </span></li><li><span> </action> </span></li><li><span> <action name=</span><span class="string"><span style="color: #0000ff;">"findAllPg"</span></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color: #0000ff;">"com.cnkf.template.action.ProductAction"</span></span><span> method=</span><span class="string"><span style="color: #0000ff;">"findAllPg"</span></span><span>> </span></li><li><span> <result >/welcomepg.jsp</result> </span></li><li><span> </action> </span></li></ol></div><pre class="java" style="display: none;" name="code"><action name="findAllDisplay" class="com.cnkf.template.action.ProductAction" method="findAllDisplay">
- <result >/welcomeDisplay.jsp</result>
- </action>
- <action name="findAllPg" class="com.cnkf.template.action.ProductAction" method="findAllPg">
- <result >/welcomepg.jsp</result>
- </action></pre>
-
- 最后添加上displaytag 的jar包,还有img,css文件
- 最后添加上displaytag 的jar包,还有img,css文件