zoukankan      html  css  js  c++  java
  • Struts2,oracle实现GOOGLE的分页样式


    BAIDU的搜索

    我的实现,跟google的是一样的,没有像百度一样加载20条


    要修改的地方分为三处:Pager.java,Action,jsp

    Pager.java

    是用来存储各种页面属性以及定义输出到页面的分页样式的。

    Action

    则是将数据库里面的数据查询出来,然后将其传输到页面,当然还有Pager.java里面的输出样式也在这里面传输到页面。

    jsp

    取出数据显示


    viewSource

    pager.java

    /**
     * @author Atlas Email:generationrent@gmail.com
     *
     */
    public class Pager {
    	private int current;// 当前记录的位置
    	private int size; // 数据总记录
    	private int length;// 每页显示的记录条数
    	private String url;// URL 调用Action里分页请求的URL
    	private String pageHeader;// 导航条前文字
    
    	public Pager(int offset, int size, int length,String url,  String pageHeader) {
    		this.current = offset;
    		this.size = size;
    		this.length = length;
    		this.url = url;
    		this.pageHeader = pageHeader;
    	}
    
    	public int getOffset() {
    		return current;
    	}
    
    	public void setOffset(int offset) {
    		this.current = offset;
    	}
    
    	public int getSize() {
    		return size;
    	}
    
    	public void setSize(int size) {
    		this.size = size;
    	}
    
    	public int getLength() {
    		return length;
    	}
    
    	public void setLength(int length) {
    		this.length = length;
    	}
    
    	public String getUrl() {
    		return url;
    	}
    
    	public void setUrl(String url) {
    		this.url = url;
    	}
    
    	public String getPageHeader() {
    		return pageHeader;
    	}
    
    	public void setPageHeader(String pageHeader) {
    		this.pageHeader = pageHeader;
    	}
    
    	/**
    	 * 返回分页导航条
    	 * 
    	 */
    
    	public String getPageNavigation() {
    		String pageNavigation = ""; 
    		
    		if (size > length) {
    			String pref; // 前缀
    			if (url.indexOf("?") > -1) {
    			 
    				pref = "&";
    			} else {
    				 
    				pref = "?";
    			}
    			 
    			if (pageHeader != null && pageHeader.length() > 0) {
    				pageNavigation = pageHeader + " : ";
    			}
    			//  包含“<<”(第一页)和“<”(前一页)
    			if (current > 0) {
    				pageNavigation += "<a href='" + url + pref
    						+ "current=0'>[首页]</a>\n" + "<a href='" + url + pref
    						+ "current=" + (current - length) + "'>[<]</a>\n";
    			}
    			// 导航条中,排头的那一页的current值
    			int startOffset;
    			
    			int radius = Constants.MAX_PAGE_INDEX / 2 * length;
    			
    			if (current < radius
    					|| this.pageCount() <= Constants.MAX_PAGE_INDEX) {
    				
    				startOffset = 0;
    			} else if (current < size - radius) {
    				startOffset = current - radius;
    			} else {
    				startOffset = (size / length - Constants.MAX_PAGE_INDEX)
    						* length;
    			}
    			for (int i = startOffset; i < size
    					&& i < startOffset + Constants.MAX_PAGE_INDEX * length; i += length) {
    				if (i == current) {
    					// 当前页号,加粗显示
    					pageNavigation += "<b>" + (i / length + 1) + "</b>\n";
    				} else {
    					
    					pageNavigation += "<a href='" + url + pref + "current=" + i
    							+ "'>" + (i / length + 1) + "</a>\n";
    				}
    			}
    			//(下一页)和“>>”(最后一页)
    			if (current < size - length) {
    				pageNavigation += "<a href='" + url + pref + "current="
    						+ (current + length) + "'>[>]</a>\n" + "<a href='"
    						+ url + pref + "current=" + lastPageOffset()
    						+ "'>[尾页]</a>\n";
    			}
    			
    			return pageNavigation;
    		}
    		
    		else {
    			return "";
    		}
    		//return "";
    	}
    
    	/**
    	 * 返回分页后的总页数
    	 * 
    	
    	 */
    	public int pageCount() {
    		int pagecount = 0;
    		if (size % length == 0) {
    			pagecount = size / length;
    		} else {
    			pagecount = size / length + 1;
    		}
    		return pagecount;
    	}
    
    	/**
    	 * 返回最后一页的起始记录位置
    	
    	 */
    	public int lastPageOffset() {
    		return size - lastPageSize();
    	}
    
    	/**
    	 * 返回最后一页的记录数
    	
    	 */
    	public int lastPageSize() {
    		int lastpagesize = 0;
    		if (size % length == 0) {
    			lastpagesize = length;
    		} else {
    			lastpagesize = size % length;
    		}
    		return lastpagesize;
    	}
    }
    

    action

    /**
     * 用户基本信息管理 控制器
     * 
    
     * @author Atlas Email:generationrent@gmail.com
    * */@Controller@Scope(value = "protoType")@Action(value = "userbaseAction", results = {@Result(name = "success", location = "/jsp/user.jsp") })public class UserBaseInfoMangeAction extends ActionSupport implementsServletRequestAware {private int current;// 起始记录的位置/** * @return the current */public int getCurrent() {return current;}/** * @param current * the current to set */public void setCurrent(int current) {this.current = current;}// 获得requestprivate HttpServletRequest request;// inject beanprivate UserBase userbase;/** * @return the userbase */public UserBase getUserbase() {return userbase;}/** * @param userbase * the userbase to set */public void setUserbase(UserBase userbase) {this.userbase = userbase;}/** * */private static final long serialVersionUID = 1L;/** * 注入 service */@ResourceUserBaseService userbaseService;public void setServletRequest(HttpServletRequest request) {this.request = request;}/** * 查询所有 * * @return */public String query() {// pagination beginfinal int offset = this.getCurrent();final int pagesize = Constants.PAGE_SIZE;// pagination end// 获得所有用户信息List<UserBase> list = this.userbaseService.queryUserBaseInfos(offset,pagesize);final int size = this.userbaseService.getRsUserBaseCount();//请求地址final String url =Constants.QUERY_PATH;Pager p = new Pager(offset, size, pagesize, url, "");request.setAttribute("pager", p);request.setAttribute("userbaseInfosList", list);return SUCCESS;}
    
    Conastants.java 
    

    /**
     * @author Atlas Email:Generationrent@gmail.com
     *
     */
    public class Constants {
    
    	/**
    	 * 页面显示的数字 最大到10 1...10
    	 */
    	public static int MAX_PAGE_INDEX = 10;
    	
    
    	/**
    	 * 每页的记录数
    	 */
    	public static int PAGE_SIZE = 10;
    	/**
    	 * 分页 查询地址
    	 */
    	public static String QUERY_PATH = "http://127.0.0.1:8088/alllig/userbaseAction!query";
    }
    
    Dao

    @Repository
    public class UserBaseDaoImpl implements UserBaseDao {
    	/*
    	 * (non-Javadoc)
    	 * 
    	 * @see com.travelsky.ism.dao.UserBaseDao#queryUserBaseInfos()
    	 */
    	@Override
    	public List<UserBase> queryUserBaseInfos(int offset, int length) {
    		List<UserBase> infoList = null;
    		EntityManager em = null;
    		try {
    			em = JpaUtil.getEntityManger();
    			final String jpaQl = "select userbase from UserBase userbase";
    			Query q = em.createQuery(jpaQl);
    			// 设置起始记录数
    			q.setFirstResult(offset);
    			// 查询多少条记录
    			q.setMaxResults(length);
    			// 获得结果集
    			infoList = q.getResultList();
    
    			for (int i = 0; i < infoList.size(); i++) {
    				System.out.println(infoList.get(i).getUsername());
    			}
    
    		} finally {
    			if (null != em) {
    				em.close();
    			}
    		}
    
    		return infoList;
    	}
    
    	@Override
    	public int getRsUserBaseCount() {
    		Number count = 0;
    		EntityManager em = null;
    		try {
    			final JpaTemplate jpaTemplate = new JpaTemplate(
    					Persistence.createEntityManagerFactory("jpaname"));
    			// 获得记录条数
    			String COUNT_ALL = "select count(*) from UserBase";
    			count = (Number) jpaTemplate.find(COUNT_ALL).get(0);
    
    		} finally {
    			if (null != em) {
    				em.close();
    			}
    		}
    		return count.intValue();
    	}
    }
    
    jsp

    <%@ page import="com.travelsky.ism.util.Pager"%>
    
    <s:iterator var="pager">
    		<jsp:useBean id="pager" type="com.travelsky.ism.util.Pager"   scope="request" />
    		<jsp:getProperty name="pager" property="pageNavigation" />查询结果<p>
    		<br /> 共有记录<jsp:getProperty name="pager" property="size" />条
    		<br /> 每页有<jsp:getProperty name="pager" property="length" />条记录
    		<br />
    </s:iterator>

    REFERENCES:http://www.oschina.net/code/snippet_251973_9435


    ---------------------------------------------------------------------------------------------------------------------------------
    copyright:http://www.cnblogs.com/anee/
  • 相关阅读:
    [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
    Error: 17053 LogWriter: Operating system error 21(The device is not ready.)
    PPS2013校园招聘笔试题
    RobotFrameWork(十一)AutoItLibrary测试库在win7(64bit)下安装及简单使用
    iOS 5 故事板入门(3)
    Eclipse代码字体、颜色美化,更改字体大小、颜色
    iOS 5 故事板入门(4)
    54. 如何在测试中触发服务器上运行的代理
    【PHP SDK for OpenStack/Rackspace APIs】身份验证
    JQuery总结
  • 原文地址:https://www.cnblogs.com/anee/p/2675753.html
Copyright © 2011-2022 走看看