您的位置:首页 > 编程语言 > Java开发

el表达式 分页 Struts2后台

2017-01-22 17:03 274 查看
效果图:


这边和上一篇使用的同一个分页写法,所以效果是一样的,只是后台有所不同,记录后台的写法而已,这篇的后台是ssh的框架,没什么重点,这次稍微记录的详细一点,顺便记录下搜索方法,只要下载一个js,下载地址:http://download.csdn.net/detail/qq_34117825/9743492,上干货:

jsp:

<div style="padding-top: 10px;">
<input type="hidden" id="basePath" value="<%=basePath%>"/>
<input type="hidden" id="totalPage" value="${totalPage }"/>
<input type="hidden" id="dqpageNo" value="${pageNo}"/>
<button id="search" class="btn btn-primary">查询</button>
<button id="reset" class="btn btn-primy">重置</button>
</div>


<div class="pagema">
<b>共<a>${totalPage }</a>页</b>
<b>当前为第<a>${pageNo}<
4000
;/a>页</b>
</div>
<ul class="pagination pagination-centered" id="paginations"></ul>


js:

var bathPath=$("#basePath").val();
var totalPage=$("#totalPage").val();

/** 查询点击 */
$("#search").click(function(event) {
/* Act on the event */
window.location.href = "findEquipmentInfo.action?equipmentInfo.deviceName=" + $("#search_equipment_name").val() + "&equipmentInfo.deviceIP=" + $("#search_equipment_ip").val() + "&equipmentInfo.deviceSN=" + $("#search_equipment_sn").val() + "&equipmentInfo.devicePN=" + $("#search_equipment_pn").val() + "&equipmentInfo.inTime=" + $("#input_date").val();
});
/**
* 点击分页A标签事件
*/
$("#paginations").on("click","li",function(){
var pageNo=$(this).find("a").text();
// var url=""+bathPath+"/njbh/company/getCompanyList.do?pageNo=pageNos&pageSize=10&companyName="+companyName;
var url="findEquipmentInfo.action?pageNo=pageNos&pageSize=5";
if(pageNo=='...'){
return;
}else{
showPage(pageNo,url,totalPage);
}
});

struts.xml:

<action name="findEquipmentInfo"  class="equipmentInfoAction" method="findEquipmentInfo">
<result name="SUCCESS">/configurationmanagement-bs/configurationmanagement.jsp</result>
</action>


action  :

public String findEquipmentInfo(){
Integer pageNoNow= Integer.valueOf(pageNo) == null?1:Integer.valueOf(pageNo);
Integer pageSizeNow= Integer.valueOf(pageSize) == null?5:Integer.valueOf(pageSize);
//条件查询   分页
Page page = new Page();
page.setStart((pageNoNow-1)*pageSizeNow);
page.setLimit(pageSizeNow);
Map<String,Object> map=service.findEquipmentInfo_bs(equipmentInfo,page);
List<EquipmentInfo> list =  (List<EquipmentInfo>) map.get("list");
//查询总条数
Integer totalCount=(Integer) map.get("count");
//计算总页数
Integer totalPage=totalCount%pageSizeNow==0?totalCount/pageSizeNow:totalCount/pageSizeNow+1;
ServletActionContext.getRequest().setAttribute("list", list);
ServletActionContext.getRequest().setAttribute("totalPage", totalPage);
ServletActionContext.getRequest().setAttribute("pageNo", pageNoNow);
ServletActionContext.getRequest().setAttribute("pageSize", pageSizeNow);
return SUCCESS;
}


home:

public Map<String,Object>  findEquipmentInfo_bs(EquipmentInfo equipmentInfo,Page page){
StringBuffer sbf =new StringBuffer("from EquipmentInfo");
if(null!=equipmentInfo){
sbf.append(" where 1=1 ");
String deviceName=equipmentInfo.getDeviceName();
if(null!=deviceName&&!"".equals(deviceName)){
sbf.append(" and device_Name like '%"+deviceName+"%' ");
}
String deviceIP=equipmentInfo.getDeviceIP();
if(null!=deviceIP&&!"".equals(deviceIP)){
sbf.append(" and device_IP like '%"+deviceIP+"%' ");
}
String deviceSN=equipmentInfo.getDeviceSN();
if(null!=deviceSN&&!"".equals(deviceSN)){
sbf.append(" and device_SN like '%"+deviceSN+"%' ");
}
String devicePN=equipmentInfo.getDevicePN();
if(null!=devicePN&&!"".equals(devicePN)){
sbf.append(" and device_PN like '%"+devicePN+"%' ");
}
String inTime=equipmentInfo.getInTime();
if(null!=inTime&&!"".equals(inTime)){
sbf.append(" and in_Time like '%"+inTime+"%' ");
}
}
Query query =sessionFactory.getCurrentSession().createQuery(sbf.toString());
if (null != page)
{
page.setTotalCount(count(sbf.toString(),true));
query.setFirstResult(page.getStart());
query.setMaxResults(page.getLimit());
}
List<EquipmentInfo> list=query.list();
Map<String, Object> map=new HashMap<String, Object>();
map.put("list", list);
map.put("count", page.getTotalCount());
return map;
}

private Integer count(String hql,boolean isHql) {
int index = hql.indexOf("from");
hql = hql.substring(index);
StringBuilder countHql = new StringBuilder("SELECT COUNT(*) ");
countHql.append(hql);
Query query;
if(isHql){
query = this.sessionFactory.getCurrentSession().createQuery(
countHql.toString());
}else{
query = this.sessionFactory.getCurrentSession().createSQLQuery(
countHql.toString());
}

List result = query.list();
if(isHql){
Long count = (Long) result.get(0);
return count.intValue();
}else{
BigDecimal count = (BigDecimal) result.get(0);
return count.intValue();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: