欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

输出导航条 博客分类: hibernate HibernateJSP 

程序员文章站 2024-03-16 09:49:28
...
/**
	 * <p>
	 * 输出导航条
	 * </p>
	 * 
	 * @author chenwei
	 * @param ses:org.hibernate.Session对象
	 * @param HQLsql:HQL语句
	 * @param page:当前页数
	 * @param pageSize:每页的记录数
	 * @param webpage:显示的页面[若带多个参数,可以这样写 a.jsp?r=XX&t=xx ]
	 * @return java.lang.String
	 * @throws SessionFactoryException
	 */
	public String turnpageBar(Session ses, String HQLsql, int page,
			int pageSize, String webpage) {
		// 总页数
		int pageCount = 0;
		// 总记录数
		int recordCount = 0;

		// 是否带多个参数
		boolean moreParam = webpage.indexOf("?") == -1 ? false : true;

		StringBuffer scriptStr = new StringBuffer();
		scriptStr.append("<script language=\"javascript\">");
		scriptStr.append("function change(objid){");
		scriptStr.append("var p=document.getElementById(objid).value;");
		if (!moreParam)
			scriptStr.append("window.location=\"" + webpage + "?page=\"+p;");
		else
			scriptStr.append("window.location=\"" + webpage + "&page=\"+p;");
		scriptStr.append("}</script>");

		StringBuffer sb = new StringBuffer(scriptStr.toString());

		Transaction tran = null;

		tran = ses.beginTransaction();
		java.util.List list = ses.createQuery(HQLsql).list();
		tran.commit();
		ses.close();
		if (list != null && list.size() > 0)
			recordCount = list.size();
		if (recordCount == 0)
			return "<font color=\"red\">没有记录</font>";
		else {
			pageCount = recordCount % pageSize == 0 ? recordCount / pageSize
					: recordCount / pageSize + 1;
			sb.append("第" + page + "页/共" + pageCount + "页&nbsp;&nbsp;");
			sb.append("共" + recordCount + "条记录&nbsp;每页" + pageSize
					+ "条记录&nbsp;&nbsp;");
			if (!moreParam)
				sb.append("<a href=\"" + webpage + "?page=1\">首页</a>&nbsp;");
			else
				sb.append("<a href=\"" + webpage + "&page=1\">首页</a>&nbsp;");
			if (page > 1) {
				if (!moreParam)
					sb.append("<a href=\"" + webpage + "?page=" + (page - 1)
							+ "\">上一页</a>&nbsp;");
				else
					sb.append("<a href=\"" + webpage + "&page=" + (page - 1)
							+ "\">上一页</a>&nbsp;");
			} else
				sb.append("上一页&nbsp;");
			if (page < pageCount) {
				if (!moreParam)
					sb.append("<a href=\"" + webpage + "?page=" + (page + 1)
							+ "\">下一页</a>&nbsp;");
				else
					sb.append("<a href=\"" + webpage + "&page=" + (page + 1)
							+ "\">下一页</a>&nbsp;");
			} else
				sb.append("下一页&nbsp;");
			
			if(!moreParam)
			sb.append("<a href=\"" + webpage + "?page=" + pageCount
					+ "\">尾页</a>&nbsp;&nbsp;");
			else
				sb.append("<a href=\"" + webpage + "&page=" + pageCount
						+ "\">尾页</a>&nbsp;&nbsp;");

			StringBuffer select = new StringBuffer("转到第");
			select.append("<select id=\"sel\" onchange=\"change('sel')\">");
			for (int i = 1; i <= pageCount; i++) {
				if (page == i)
					select.append("<option value=\"" + i
							+ "\" selected=\"selected\">" + i + "</option>");
				else
					select.append("<option value=\"" + i + "\">" + i
							+ "</option>");
			}
			select.append("</select>页");

			sb.append(select.toString());
			return sb.toString();
		}
	}
相关标签: Hibernate JSP