page.tld
放到WEB-INF下 tomcat是自动加载的。里面如xml。一样解释好就好了。这里有一个pager2的标签名
关联到class类的放到src里、类下。这里命名好了,前台页面调用会用名。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
Pager 1.0 core library
Pager core
1.0
fkjava
/pager-tags
pager2
link.yixing.util.tag.PagerTag2
empty
pageIndex
true
true
pageSize
true
true
recordCount
true
true
submitUrl
true
true
style
false
true
yxParam
false
true
/*这里是上面命名的标签*
* 分页标签
*/
public class PagerTag2 extends SimpleTagSupport {
/** 定义请求URL中的占位符常量 */
private static final String TAG = "{0}";
/** 当前页码 */
private int pageIndex;
/** 每页显示的数量 */
private int pageSize;
/** 总记录条数 */
private int recordCount;
/** 请求URL page.action?pageIndex={0}*/
private String submitUrl;
/** 样式 */
private String style = "sabrosus";
/** 定义总页数 */
private int totalPage = 0;
private String yxParam;
/** 在页面上引用自定义标签就会触发一个标签处理类 */
@Override
public void doTag() throws JspException, IOException {
/** 定义它拼接是终的结果 */
StringBuilder res = new StringBuilder();
/** 定义它拼接中间的页码 */
StringBuilder str = new StringBuilder();
/** 判断总记录条数 */
if (recordCount > 0){ //1499 / 15 = 100
/** 需要显示分页标签,计算出总页数 需要分多少页 */
totalPage = (this.recordCount - 1) / this.pageSize + 1;
/** 判断上一页或下一页需不需要加a标签 */
if (this.pageIndex == 1){ // 首页
str.append("上一页");
/** 计算中间的页码 */
this.calcPage(str);
/** 下一页需不需要a标签 */
if (this.pageIndex == totalPage){
/** 只有一页 */
str.append("下一页");
}else{
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
// str.append("下一页");
str.append("下一页");
}
}else if (this.pageIndex == totalPage){ // 尾页
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
//str.append("上一页");
str.append("上一页");
/** 计算中间的页码 */
this.calcPage(str);
str.append("下一页");
}else{ // 中间
String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1));
//str.append("上一页");
str.append("上一页");
/** 计算中间的页码 */
this.calcPage(str);
tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1));
//str.append("下一页");
str.append("下一页");
}
/** 拼接其它的信息 */
res.append("
" + str.toString()); res.append(" 跳转到 "); res.append(" "); res.append(" |
"); /** 开始条数 */ int startNum = (this.pageIndex - 1) * this.pageSize + 1; /** 结束条数 */ int endNum = (this.pageIndex == this.totalPage) ? this.recordCount : this.pageIndex * this.pageSize; res.append("总共"+ this.recordCount +"条记录,当前显示"+ startNum +"-"+ endNum +"条记录。"); res.append(" |
总共0条记录,当前显示0-0条记录。 |
/** setter 方法 */
public void setPageIndex(int pageIndex) {
this.pageIndex = pageIndex;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public void setSubmitUrl(String submitUrl) {
this.submitUrl = submitUrl;
}
public void setStyle(String style) {
this.style = style;
}
public void setYxParam(String yxParam){
this.yxParam = yxParam;
System.out.print("fkjava"+yxParam);
}
}
//需要调用分页的页面头加载
<%@ taglib prefix="fkjava" uri="/pager-tags" %>
//调用分页控件。
<fkjava:pager2
pageIndex="${pageModel.pageIndex}"
pageSize="${pageModel.pageSize}"
recordCount="${pageModel.recordCount}"
yxParam="main"
submitUrl="${ctx}/goods/index?pageIndex={0}&category_id=${category_id}"
style="flickr"/>
//调用分页文件的控制层。
PageModel pageModel = new PageModel();
if(pageIndex != null){
pageModel.setPageIndex(pageIndex);
}
if(shopgood.getCategory_id()!=0){ //选择分类后。param中的category_id会传递进shopgood.category_id。这里要传递到前台页面,后面查询跟上参数。当为空时,就为0。Dao层里判断为零时是不启动条件语句的。
mv.addObject("category_id",shopgood.getCategory_id());
}else{
mv.addObject("category_id",0);
}
List
List
mv.addObject("shopcategorys",shopcategorys);
mv.addObject("shopgoods",shopgoods);
mv.addObject("pageModel", pageModel);
return mv;
//服务层 注意了。这个的参数是pojo对象或者是其他。但在这里就put进Map了。
public List
// TODO Auto-generated method stub
Map
params.put("shopgood", shopgood);
int recordCount= shopGoodsDao.selectCountGoods(params);
pageModel.setRecordCount(recordCount);
if(recordCount >0){
params.put("pageModel", pageModel);
}
return shopGoodsDao.selectAllGoods(params);
}
//Dao层 这里没写接口,跳过了,这里是sql动态返回。这里把Map解析出来。再判断
public String selectWhitParam(Map
String sql = new SQL(){
{
SELECT("*");
FROM("shop_goods");
if(param.get("shopgood") != null){
ShopGoods shopgood = (ShopGoods) param.get("shopgood");
if(shopgood.getGoods_name() != null && !shopgood.getGoods_name().equals("")){
WHERE(" goods_name LIKE CONCAT ('%',#{shopgood.goods_name},'%') ");
}
if(shopgood.getCategory_id() != 0 ) {
WHERE(" category_id = " + shopgood.getCategory_id());
}
}
}
}.toString();
sql += " order by create_time desc"; //排序应该在分页之前.
if(param.get("pageModel") != null){
sql += " limit #{pageModel.firstLimitParam} , #{pageModel.pageSize} ";
}
System.out.print(sql);
return sql;
}
// 动态查询总数量
public String selectCountWhitParam(Map
return new SQL(){
{
SELECT("count(*)");
FROM("shop_goods");
if(param.get("shopgood") != null){
ShopGoods shopgood = (ShopGoods) param.get("shopgood");
if(shopgood.getGoods_name() != null && !shopgood.getGoods_name().equals("")){
WHERE(" goods_name LIKE CONCAT ('%',#{shopgood.goods_name},'%') ");
}
if(shopgood.getCategory_id() != 0 ) {
WHERE(" category_id = " + shopgood.getCategory_id());
}
}
}
}.toString();
}