ssm+jsp+自定义标签实现分页,可以通用(后端实现)
从controller到mapper.xml的后端实现
只梳理分页相关代码
1 controller里面相关
ModelAndView mv = new ModelAndView("/listDesc/listingDescPage");
int pageSize = 2;
List<ListingDesc> dataDto = new ArrayList<ListingDesc>();
List<ListingDesc> totalList;
ListingDescServiceClient listingDescServiceClient = ItemTools
.getListingDescServiceClient();
// 获取总记录数,准备分页
totalList = listingDescServiceClient
.findListingDescByIListingDesc(listingDesc);
// 分页工具
PaginationList<ListingDesc> pageMode = new PaginationList<ListingDesc>();
pageMode.setPageIndex(currentPage);
pageMode.setPageSize(pageSize);
dataDto = listingDescServiceClient.findListingByCondition(
listingDesc, pageMode);
log.info("共查询出》》》》" + totalList.size() + "条数据");
this.getPage(request, pageSize, totalList.size());
private int getPage(HttpServletRequest request, Integer recordPageCount,
Integer totalCount) {
request.setAttribute("totalPageCount", totalPageCount);
request.setAttribute("totalCount", totalCount);
request.setAttribute("pageSize", 2);
return startNum;}
getpage()方法里面只需把三个参数总页数,当前页,每页显示条数封装进request即可,我这当前页在别的地方已封装过所以此处没封装,在此处封装了记录总条数,其实在自定义标签里是用不到的。
2 service
List<ListingDesc> list = null;
try{
if(listingDesc!= null){
//获取总记录数
int totalRecordCount = listingDescService.findListingDescByListingDesc(listingDesc).size();
pageMode.setTotalRecords(totalRecordCount);
//计算每页起始记录
int firstresult = (pageMode.getPageIndex()-1)*pageMode.getPageSize();
list =listingDescService.findListingByCondition(listingDesc, firstresult, pageMode.getPageSize());
logger.info("ListingDescServiceClient:findListingByCondition method end=====returnList is "+ list);
}else{
logger.info("ListingDescServiceClient:findListingByCondition method end=====params listingDesc is null.");
return null;
}
}catch(Exception e){
e.getStackTrace();
}
return (PaginationList<ListingDesc>)list;
我用的是ejb架构,在此和ssm架构稍有不同,这段代码直接拿到service下改造一下即可
3 dao相关
// 根据listingDesc,firstResult,maxResult查找并分页
@Override
public PaginationList<ListingDesc> findListingByCondition(
ListingDesc listingDesc, int firstResult, int maxResults) {
return listingDescDao.findListingByCondition(listingDesc, firstResult,
maxResults);
}
@Override
public PaginationList<ListingDesc> findListingByCondition(
ListingDesc listingDesc, int firstResult, int maxResults) {
return (PaginationList<ListingDesc>) this.findByExampleForPage(
"findListingDescByCondition", listingDesc, firstResult,
maxResults);
}
public abstract class BaseDao<T, PK> implements IDao<T, PK> {
@Autowired
private SqlSessionTemplate sqlSession;
*/
protected PaginationList findByExampleForPage(String sqlId, Object param, int firstResult, int maxResults) {
UleRowBounds rowBounds = new UleRowBounds(firstResult, maxResults, UleRowBounds.TYPE_PAGE_SELECT);
return (PaginationList) sqlSession.selectList(this.getClass().getName() + "." + sqlId, param, rowBounds);
}
4 mapper
<select id="findListingDescByCondition" resultType="ListingDesc"
parameterType="ListingDesc">
SELECT
SEQ_ID seqId,
LIST_ID listId,
DESC_NAME descName,
DESC_VALUE
descValue,
SORT_NUM sortNum,
DEL_FLAG delFlag,
CREATE_TIME createTime,
UPDATE_TIME updateTime
FROM LISTING_DESC
<where>
<if test="seqId != null"> SEQ_ID = #{seqId}</if>
<if test="listId != null"> AND LIST_ID = #{listId}</if>
<if test="descName != null and descName != ''"> AND DESC_NAME = #{descName}</if>
</where>
</select>
至此分页后端完成,关于dao层的mybatis实现自己可以查看一些相关资料。主要是dao实现类继承baseDao:
@Repository
public class ListingDescDaoImpl extends BaseDao<ListingDesc, Long> implements ListingDescDao
然后baseDao实现IDao接口
public abstract class BaseDao<T, PK> implements IDao<T, PK> {
@Autowired
private SqlSessionTemplate sqlSession;
里面可以用 SqlSessionTemplate sqlSession的api实现相关增删改查。
这是Idao里关于分页的内容:
public interface IDao<T, PK> {
/**
* 分页查询数据,返回分页结构对象PaginationList,该方法包含两次SQL查询,一次查询数据,一次统计总结果数
* @param t 查询参数对象
* @param firstResult 查询结果第一次记录的偏移量,表示从第几条数据开始返回
* @param maxResults 返回结果集大小
* @return
*/
public PaginationList findByExampleForPage(T t, int firstResult, int maxResults);
/**
* 分页查询数据,返回分页结构对象PaginationList,该方法包含两次SQL查询,一次查询数据,一次统计总结果数
* @param t 查询参数对象
* @param firstResult 查询结果第一次记录的偏移量,表示从第几条数据开始返回
* @param maxResults 返回结果集大小
* @param orders SQL排序子串
* @return
*/
public PaginationList findByExampleForPage(T t, int firstResult, int maxResults, Order... orders);
public static class Order{
private boolean ascending;
private String propertyName;
public String toString() {
return propertyName + ' ' + (ascending?"asc":"desc");
}
protected Order(String propertyName, boolean ascending) {
this.propertyName = propertyName;
this.ascending = ascending;
}
public static Order asc(String propertyName) {
return new Order(propertyName, true);
}
public static Order desc(String propertyName) {
return new Order(propertyName, false);
}
}
}
ssm+jsp+自定义标签实现分页,可以通用(后端实现)的更多相关文章
- ssm+jsp+自定义标签实现分页,可以通用(前端实现)
近期做了一些分页方面的开发,大致梳理一下 1 jsp页面上关于分页的代码 <tr> <td colspan="9"> <ule1:pagination ...
- JSP 自定义标签
0 标签技术的API继承体系 1 作用 jsp自定义标签用于移除页面中的java代码 2 实现 2.1 标签处理类ViewIPTag.java package com.zsm.util; import ...
- JSP自定义标签开发入门
一般情况下开发jsp自定义标签需要引用以下两个包 import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; 首先我们需要大致了解开发 ...
- 一个简单的jsp自定义标签
学到了一个简单的jsp自定义标签,后面有更多的例子,会更新出来: 例子1: 步骤: 1.编写标签实现类: 继承javax.servlet.jsp.tagext.SimpleTagSupport; 重写 ...
- jsp自定义标签分析
jsp自定义标签的优势体现在于jsp页面上面减少了java代码. jsp自定义标签有三大部分组成,首先是类继承TagSupport,实现doStartTag方法. public int doStart ...
- JSP自定义标签库
总所周知,JSP自定义标签库,主要是为了去掉JSP页面中的JAVA语句 此处以格式化输出时间戳为指定日期格式为例,简单介绍下JSP自定义标签的过程. 编写标签处理类(可继承自javax.servlet ...
- JSP自定义标签配置
JSP自定义标签配置 JSP自定义标签 <taglib> <taglib-uri>/WEB-INF/you.tld</taglib-uri> ...
- jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题
jsp 自定义标签解决jsp页面中int时间戳的时间格式化问题 之前在项目中根据需求,需要自定义标签,经过查询w3c文档,自己也踩了一些坑,特此记录自定义标签的步骤,下面就以我之前的一个例子中的定义一 ...
- java JSP自定义标签
来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373 http://blog.csdn.net/jiangwei09 ...
随机推荐
- java中jsoup框架解析html
今天遇到对网页内容进行操作,思考了一下,先获取连接后的html内容,然后对html文档进行操作呗.思路没有问题,但是问题还是不少.于是便找到了jsoup这个神器了... 1.什么是jsoup? 百度百 ...
- 【转】Android adb shell操作时出现“ XXX ... Read-only file system”解决办法--不错
原文网址:http://blog.csdn.net/whu_zhangmin/article/details/25364349 手机连接PC后 adb shell su rm -r /system/a ...
- SpringMVC 拦截器(interceptors)对样式(css),JavaScript(js),图片(images)链接的拦截
因为在web.xml配置了 <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pa ...
- 面试al tx
阿里: 一面: 1:写代码,给三个数组abc,每个数组若干数字,判断一个数字在不在三个数组中.用的map解决. 2:例举知道的排序,写出归并排序代码. 3:剩下的都是小题目了:包括三次握手,tc ...
- 【SSH三框架】Struts2第六章的基础:他们拦截函数的定义
干web当然,需要做的事情时,项目管理登录身份验证及其他权利.假设我们必须使用相应的登陆,未经允许是不可能的. 因此,我们需要使用拦截器,拦截功能struts2它集成.当然,有可能在Spring正在使 ...
- emacs快捷键学习(一)--Linux最强大的编辑器
emacs是一个非常强大的编辑器.经常使用的快捷键总结例如以下: 退出emacs:ctrl+x ctrl+c 移动到下一屏:ctrl+v 移动到上一屏:alt+v 将光标所在行移动到屏幕中间:ctrl ...
- Win2 Socket(套接字)相关 API
Socket(套接字) 作者信息 肖进 单位:南京中萃食品有限公司 资讯部 邮箱:xiaoj@njb.swirebev.com 电话:025-58642091 与socket有关的一些函数介绍 1.读 ...
- 如何解决svn图标不显示呢?
svn图标不显示解决 确保设置正确: 右键->TortoiseSVN->setting->Icon Overlays->Status cache->default/She ...
- C# winform xml的增删改查
代码如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using Syst ...
- 根据id查询数据(向前台返回json格式的数据)
/** *@description 根据主键查询Bean */ @RequestMapping(value="/getBean/{getId}") public void getB ...