一.前端jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>房源管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="${path}/public/css/commonpc.css" rel="stylesheet" type="text/css" />
<link href="${path}/public/css/admin.css" rel="stylesheet" type="text/css" />
<link href="${path}/public/css/bootstrap/bootstrap.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="${path}/public/css/compiled/user-list.css" type="text/css" media="screen" />
<script type="text/javascript" src="${path}/public/js/jquery-1.4.4.min.js"></script>
<script>
function searchHS(){
var housenum=$("#housenum").val();
var housename=$("#housename").val();
var para = "housenum=" + housenum ;
para += "&housename=" + housename ;
para = para.replace(/[\r\n]/g,"").replace(/undefined/g,"").replace(/[]/g,"");
window.location.href=encodeURI("${path}/house/list?" + para);
}
</script>
<style>
label {
display: block;
margin-bottom: 5px;
display: inline-table;
}
.input-btn {
width: 130px;
}
.mr5 {
margin-right: 5px;
}
.ss-btn {
border: 1px solid #1ba794;
background-color: #1ba794;
margin-top: 0px;
height: 24px;
color: #fff;
font-size: 14px;
}
input[type="text"]{
height: 23px;
margin-bottom: 0px;
}
</style>
</head>
<body >
<div class="rygl-tit">
<span class="rygl fl">房源列表</span>
<div class="fr pdr80 pdt20">
<button class="tjdr-btn mr30" onclick="window.location.href='${path}/house/add'">添加</button>
</div>
</div>
<p ></p>
<div >
<div class="fl">
<input type="hidden" id="pageNo" name="pageNo" />
<label>房源编号:</label>
<input class="input-btn mr5" type="text" id="housenum" name="housenum" value="${housenum}"/>
<label>房源名称:</label>
<input class="input-btn mr5" type="text" id="housename" name="housename" value="${housename}"/>
</div>
<input class="ss-btn" value="搜索" type="button" onclick="searchHS()"/>
</div>
<%-- 加载分页时请求路径 及 请求方式--%>
<form id="queryForm" name="queryForm" action="${path}/house/list" method="post">
<input type="hidden" id="pageNo" name="pageNo" />
</form>
<div class="table-style">
<table cellspacing="0">
<thead>
<tr>
<th>房源编号</th>
<th>房源名称</th>
<th>创建人</th>
<th>操作</th>
<th >${userid}</th>
</tr>
</thead>
<c:forEach items="${houselist}" var="list">
<tbody>
<tr class="bg-con">
<td class="ddbh">${list.houseNum }</td>
<td class="ddyzf">${list.houseName}</td>
<td class="ddyzf" >${list.createPeople}</td>
<td class="cz">
<a class="ckxq" href="${path}/house/look/?id=${list.houseresid}">查看</a>
<a class="ckxq" href="${path}/house/edit?id=${list.houseresid}">修改</a>
<a class="ckxq" href="${path}/house/del?id=${list.houseresid}" confirm('确定删除吗?')" >删除</a>
</td>
</tr>
</tbody>
</c:forEach>
</table>
</div>
${houseRec.pageNoDisp }
</body>
</html>
二.后台代码
/////////////////////////////////1.业务控制层(controller层)////////////////////////////
package com.sykj.haoguanjia.controller.system;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sykj.haoguanjia.model.HouseRes;
import com.sykj.haoguanjia.service.HouseResService;
/**
* @author hykang
* @date 2017-05-09
*
*/
@Controller
@RequestMapping("/house")
public class HouseController {
@Autowired
private HouseResService houseResService;
@RequestMapping("/list")
public String houseList(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,String housenum,String housename,Model model){
List<HouseRes> pagelist = null;
try {
HouseRes pageRec = new HouseRes();
pageRec.setPageNo(pageNo);
pageRec.setHouseNum(housenum);
if(!"".equals(housename) && housename != null){
housename=new String(housename.getBytes("ISO-8859-1"),"UTF-8");
//housename = java.net.URLDecoder.decode(housename, "utf-8");
}
pageRec.setHouseName(housename);
pageRec.setDeleteFlag(0);
pagelist = houseResService.findPageData(pageRec);
model.addAttribute("houselist", pagelist);
model.addAttribute("houseRec", pageRec);
model.addAttribute("housenum", housenum);
model.addAttribute("housename", housename);
} catch (Exception e) {
e.printStackTrace();
}
return "house/list";
}
}
////////////////////////////////2.service层//////////////////////////////////////////
package com.sykj.haoguanjia.service;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import com.sykj.haoguanjia.dao.HouseResMapper;
import com.sykj.haoguanjia.model.HouseRes;
/**
* @author hekang
*
*/
@Service
public class HouseResService {
@Autowired
private HouseResMapper houseResMapper;
public List<HouseRes> findPageData(HouseRes record) throws DataAccessException{
return houseResMapper.findPageData(record);
}
}
////////////////////////////////////////3.DAO层///////////////////////////////////////
package com.sykj.haoguanjia.dao;
import java.util.List;
import com.sykj.haoguanjia.model.HouseRes;
public interface HouseResMapper {
List<HouseRes> findPageData(HouseRes record);
}
////////////////////////////////////////4.Mapper.xml/////////////////////////////////////
<resultMap id="BaseResultMap" type="com.sykj.haoguanjia.model.HouseRes">
<id column="houseresid" jdbcType="BIGINT" property="houseresid" />
<result column="house_num" jdbcType="VARCHAR" property="houseNum" />
<result column="house_name" jdbcType="VARCHAR" property="houseName" />
<result column="create_people" jdbcType="BIGINT" property="createPeople" />
</resultMap>
<select id="findPageData" parameterType="com.sykj.haoguanjia.model.HouseRes" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from tb_house_res
<include refid="selectWhereClause"/>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
////////////////////////////////////5.房源实体类////////////////////////////////////////////////////
package com.sykj.haoguanjia.model;
import java.math.BigDecimal;
import java.util.Date;
import com.sykj.haoguanjia.page.PageHelper;
public class HouseRes extends PageHelper{
private Long houseresid;
private String houseNum;
private String houseName;
private Long createPeople;
public Long getHouseresid() {
return houseresid;
}
public void setHouseresid(Long houseresid) {
this.houseresid = houseresid;
}
public String getHouseNum() {
return houseNum;
}
public void setHouseNum(String houseNum) {
this.houseNum = houseNum == null ? null : houseNum.trim();
}
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName == null ? null : houseName.trim();
}
public Long getCreatePeople() {
return createPeople;
}
public void setCreatePeople(Long createPeople) {
this.createPeople = createPeople;
}
}
//////////////////////////////////6.分页类///////////////////////////////////////////
package com.sykj.haoguanjia.page;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* 封装分页数据
*/
public class PageHelper {
private static final Logger logger = Logger.getLogger(PageHelper.class);
private static ObjectMapper mapper = new ObjectMapper();
public static String DEFAULT_PAGESIZE = "10";
private int pageNo; // 当前页码
private int pageSize; // 每页行数
private int totalRecord; // 总记录数
private int totalPage; // 总页数
private Map<String, String> params; // 查询条件
private Map<String, List<String>> paramLists; // 数组查询条件
private String searchUrl; // Url地址
private String pageNoDisp; // 可以显示的页号(分隔符"|",总页数变更时更新)
private String orderByClause;
public PageHelper() {
pageNo = 1;
pageSize = Integer.valueOf(DEFAULT_PAGESIZE);
totalRecord = 0;
totalPage = 0;
params = Maps.newHashMap();
paramLists = Maps.newHashMap();
searchUrl = "";
pageNoDisp = "";
}
public static PageHelper newBuilder(int pageNo, int pageSize, String url) {
PageHelper page = new PageHelper();
page.setPageNo(pageNo);
page.setPageSize(pageSize);
page.setSearchUrl(url);
return page;
}
/**
* 查询条件转JSON
*/
public String getParaJson() {
Map<String, Object> map = Maps.newHashMap();
for (String key : params.keySet()) {
if (params.get(key) != null) {
map.put(key, params.get(key));
}
}
String json = "";
try {
json = mapper.writeValueAsString(map);
} catch (Exception e) {
logger.error("转换JSON失败", e);
}
return json;
}
/**
* 数组查询条件转JSON
*/
public String getParaListJson() {
Map<String, Object> map = Maps.newHashMap();
for (String key : paramLists.keySet()) {
List<String> lists = paramLists.get(key);
if (lists != null && lists.size() > 0) {
map.put(key, lists);
}
}
String json = "";
try {
json = mapper.writeValueAsString(map);
} catch (Exception e) {
logger.error("转换JSON失败", e);
}
return json;
}
/**
* 总件数变化时,更新总页数并计算显示样式
*/
private void refreshPage() {
// 总页数计算
totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : (totalRecord / pageSize + 1);
// 防止超出最末页(浏览途中数据被删除的情况)
if (pageNo > totalPage && totalPage != 0) {
pageNo = totalPage;
}
pageNoDisp = computeDisplayStyleAndPage();
}
/**
* 计算页号显示样式 这里实现以下的分页样式("[]"代表当前页号),可根据项目需求调整 [1],2,3,4,5,6,7,8..12,13
* 1,2..5,6,[7],8,9..12,13 1,2..6,7,8,9,10,11,12,[13]
*/
private String computeDisplayStyleAndPage() {
StringBuffer buffer = new StringBuffer();
List<Integer> pageDisplays = Lists.newArrayList();
if (totalPage <= 11) {
for (int i = 1; i <= totalPage; i++) {
pageDisplays.add(i);
}
} else if (pageNo < 7) {
for (int i = 1; i <= 8; i++) {
pageDisplays.add(i);
}
pageDisplays.add(0);// 0 表示 省略部分(下同)
pageDisplays.add(totalPage - 1);
pageDisplays.add(totalPage);
} else if (pageNo > totalPage - 6) {
pageDisplays.add(1);
pageDisplays.add(2);
pageDisplays.add(0);
for (int i = totalPage - 7; i <= totalPage; i++) {
pageDisplays.add(i);
}
} else {
pageDisplays.add(1);
pageDisplays.add(2);
pageDisplays.add(0);
for (int i = pageNo - 2; i <= pageNo + 2; i++) {
pageDisplays.add(i);
}
pageDisplays.add(0);
pageDisplays.add(totalPage - 1);
pageDisplays.add(totalPage);
}
buffer.append("<div style='text-align:right;padding-right:20px;'>");
if (pageNo==1) {
buffer.append("首页");
buffer.append("上一页");
}else{
buffer.append("<a class='tcdNumber' href='javascript:_doQuery(1);'>首页</a>");
buffer.append("<a class='tcdNumber' href='javascript:_doQuery("+(pageNo-1)+");'>上一页</a>");
}
for(Integer i:pageDisplays){
if(i==0){
buffer.append("<a class='tcdNumber' >"+"..."+"</a>");
}else if( i == pageNo ) {
buffer.append("<a class='curNumber' href='javascript:_doQuery("+i+");'>"+i+"</a>");
} else {
buffer.append("<a class='tcdNumber' href='javascript:_doQuery("+i+");'>"+i+"</a>");
}
}
if (pageNo==totalPage) {
buffer.append("下一页");
buffer.append("尾页");
}else{
buffer.append("<a class='tcdNumber' href='javascript:_doQuery("+(pageNo+1)+");'>下一页</a>");
buffer.append("<a class='tcdNumber' href='javascript:_doQuery("+totalPage+");'>尾页</a>");
}
buffer.append("</div>");
buffer.append("<script type='text/javascript'>");
buffer.append("function _doQuery(pageNo){document.queryForm.pageNo.value=pageNo;document.queryForm.submit();}");
buffer.append("</script>");
return buffer.toString();
}
public String getHTML(){
return null;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
refreshPage();
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public Map<String, String> getParams() {
return params;
}
public void setParams(Map<String, String> params) {
this.params = params;
}
public Map<String, List<String>> getParamLists() {
return paramLists;
}
public void setParamLists(Map<String, List<String>> paramLists) {
this.paramLists = paramLists;
}
public String getSearchUrl() {
return searchUrl;
}
public void setSearchUrl(String searchUrl) {
this.searchUrl = searchUrl;
}
public String getPageNoDisp() {
return pageNoDisp;
}
public void setPageNoDisp(String pageNoDisp) {
this.pageNoDisp = pageNoDisp;
}
public String getOrderByClause() {
return orderByClause;
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
}