1、sprinMvc控制層
package com.dengfeng.house.controller; import java.text.ParseException; import java.util.Date; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.dengfeng.house.pojo.District; import com.dengfeng.house.pojo.House; import com.dengfeng.house.pojo.HouseQuery; import com.dengfeng.house.pojo.Street; import com.dengfeng.house.pojo.Type; import com.dengfeng.house.pojo.User; import com.dengfeng.house.service.IDistrictService; import com.dengfeng.house.service.IHouseService; import com.dengfeng.house.service.IStreetService; import com.dengfeng.house.service.ITypeService; @Controller @RequestMapping("/house") public class HouseController { @Resource private IHouseService houseService; @Resource private ITypeService typeService; @Resource private IDistrictService districtService; @Resource private IStreetService streetService; //manage頁面全查 @RequestMapping("/findAll") @ResponseBody public PageInfo findAll(@RequestParam(defaultValue="1")int pageIndex){ PageHelper.startPage(pageIndex, 3); List<House> housList = houseService.selectHouse(); PageInfo pageInfo=new PageInfo<House>(housList); return pageInfo; } //修改渲染 @RequestMapping("/update/{id}") public String update(Model model,@PathVariable int id){ List<Type> typeList = typeService.findAllType(); List<District> districtList = districtService.findAllDistrict(); List<Street> streetList= streetService.findAllStreet(); House house = houseService.findHouseById(id); model.addAttribute("house", house); model.addAttribute("typeList", typeList); model.addAttribute("districtList", districtList); model.addAttribute("streetList", streetList); return "/page/update"; } //保存修改 @RequestMapping("/updateSave") public String update(House house){ houseService.alterHouse(house); return "redirect:/page/manage.jsp"; } //渲染增加,下拉框推薦用ajxj做,,=所有這個方法沒什么用 @RequestMapping("/add") public String add(Model model){ List<Type> typeList = typeService.findAllType(); /* List<District> districtList = districtService.findAllDistrict(); List<Street> streetList= streetService.findAllStreet();*/ model.addAttribute("typeList", typeList); /* model.addAttribute("districtList", districtList); model.addAttribute("streetList", streetList); */ return "/page/add"; } //真正保存增加的方法 @RequestMapping("/addSave") public String addSave(House house,HttpSession session){ houseService.insertHouse(house); return "redirect:/page/manage.jsp"; } /*house_list.jsp查詢。*/ @RequestMapping("/find") @ResponseBody public List<House> find(HouseQuery houseQuery,String money,String size){ //如果是選中狀態 if(money!=null&&!money.equals("")){ String[] moneyArray=money.split("-"); houseQuery.setMinPrice(Integer.parseInt(moneyArray[0])); houseQuery.setMaxPrice(Integer.parseInt(moneyArray[1])); } if(size!=null&&!size.equals("")){ String[] sizeArray=size.split("-"); houseQuery.setMinFloorAge(Integer.parseInt(sizeArray[0])); houseQuery.setMaxFloorAge(Integer.parseInt(sizeArray[1])); } List<House> list = houseService.find(houseQuery); return list; } //推薦,多條件模糊查詢 /* @RequestMapping("/findAllHouseList") @ResponseBody public PageInfo<House> findAll(HttpSession session,@RequestParam(defaultValue="1")Integer pageIndex, @RequestParam(defaultValue="")String title, @RequestParam(defaultValue="")String price, @RequestParam(defaultValue="")Integer typeId, @RequestParam(defaultValue="0")String floorage,Date beginTime,Date endTime) throws ParseException{ User user=(User)session.getAttribute("userSession"); String[] prices = price.split("-"); String[] floorages = floorage.split("-"); if(user!=null) { PageHelper.startPage(pageIndex,2); House house =new House (); house.setTitle(title); house.setMinPrice(Integer.parseInt(prices[0])); house.setMaxPrice(Integer.parseInt(prices[1])); house.setTypeId(typeId); house.setMinFloorage(Integer.parseInt(floorages[0])); house.setMaxFloorage(Integer.parseInt(floorages[1])); if(beginTime!=null && endTime!=null){ house.setBeginTime(beginTime); house.setEndTime(endTime); }else if(beginTime!=null){ house.setBeginTime(beginTime); }else if(endTime!=null){ house.setEndTime(endTime); } List<House> list = houseService.findByCondition(house); PageInfo<House> pageInfo=new PageInfo<House>(list); return pageInfo; } return new PageInfo<House>(); }*/ //刪除房子 @RequestMapping("/deleteById") @ResponseBody public String delete(int id){ houseService.deletehouse(id); return "刪除成功"; } }
2、前端頁面
<%@ page language="java" contentType="text/html;charset=UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; int total = (Integer)request.getAttribute("total"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>用戶管理</title> <%-- <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}css/style.css" /> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/function.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.min.js"></script> --%> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.min.js"></script> <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" /> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/function.js"></script> <style type="text/css"> /* .space{ 給js里的td設置字體顏色有效 color: red } */ </style> </head> <body> <input type="hidden" id="path" value="${pageContext.request.contextPath}"/> <div id="header" class="wrap"> <div id="logo"></div> <div class="search"> <label class="ui-green"><a href="${pageContext.request.contextPath }/house/add"><input type="button" name="search" value="發布房屋信息" /></a></label> <label class="ui-green"><a href="${pageContext.request.contextPath }/user/logout"><input type="button" name="search" value="退 出" /></a></label> </div> </div> <div class="main wrap"> <div id="houseArea"> <!-- <table class="house-list"> 有房屋 <tr> <td class="house-thumb"> <span> <img src="images/thumb_house.gif" /> </span> </td> <td> <dl> <dt> <a href="#">sss</a> </dt> <dd> dddd 區, ddddd 街, 10000 平米<br/> 聯系方式 ssss </dd> </dl> </td> 自己發布的信息有權限刪除 <td class="house-type"><label class="ui-green"><input type="button" name="search" value="修 改" /></label></td> <td class="house-price"><label class="ui-green"><input type="button" name="search" value="刪 除" /></label></td> 不是自己發布的信息沒有權限刪除 <td colspan="2"></td> </tr> 無房屋 <tr> <td>無房屋信息輸出</td> </tr> </table> </div --> <!-- 這個隱藏框用於分頁查詢 --> <input type="hidden" name="pageIndex" value="1"/> <table class="house-list"> <!-- <img src="../images/thumb_house.gif" /> --> </table> <div class="page-bar"> <ul class="page-num-ul clearfix"> <li>共${param.totalCount }條記錄 ${param.currentPageNo }/${param.totalPageCount }頁</li> <c:if test="${param.currentPageNo > 1}"> <a href="javascript:page_nav(document.forms[0],1);">首頁</a> <a href="javascript:page_nav(document.forms[0],${param.currentPageNo-1});">上一頁</a> </c:if> <c:if test="${param.currentPageNo < param.totalPageCount }"> <a href="javascript:page_nav(document.forms[0],${param.currentPageNo+1 });">下一頁</a> <a href="javascript:page_nav(document.forms[0],${param.totalPageCount });">最后一頁</a> </c:if> </ul> <!-- <span class="page-go-form"><label>跳轉至</label> <input type="text" name="inputPage" id="inputPage" class="page-key" />頁 <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button> </span> --> </div> <!-- <div class="pager"> <ul> <li class="current"> <a href="#">首頁</a> </li> <li> <a href='#'>上一頁</a> </li> <li> <a href='#'>下一頁</a> </li> <li> <a href='#'>末頁</a> </li> </ul> <span class="total">共 頁</span> </div> --> </div> <script type="text/javascript" src="${pageContext.request.contextPath}/scripts/manage.js"></script> </body> </html>
3、JS代碼
<script type="text/javascript">
var path; $(function(){ alert("1") path=$("#path").val(); findHouse(); }); /* 頁面一加載完就調用走這個函數 */ //這個方法用於查找所有 function findHouse(){ /*alert("2")*/ $.post(path+"/house/findAll",{"pageIndex":$("[name='pageIndex']").val()},function(data){ /* 每次獲取用戶列表,都需要將上一次的結果清除 */ /*alert("3")*/ $("tr").remove(); $(data.list).each(function(i,e){ /*alert("6")*/ var content="<tr>"; content+="<td class=\"house-thumb\"><span><img src=\"../images/thumb_house.gif\" /></span></td>"; content+="<td class=\"space\"><dl><dt><a href=\"#\">"+e.title+"</a></dt>"; content+="<dd>"+e.districtName+"區"+""; content+="<span style=\"margin-left:20px\"></span>"; content+=""+e.streetName+"街"+""; content+="<span style=\"margin-left:20px\"></span>"; content+=""+"面積"+e.floorage+"平米"+"<br/>"; content+=""+"聯系方式:"+e.contact+"</dd></dl></td>"; /* this指a標簽自己 ,javascript:void(0)的意思是讓這個標簽有a標簽的樣式,但沒有a標簽的功能,找到a標簽自己把父節點刪除,tr*/ content+="<td class=\"house-type\"><label class=\"ui-green\"><a href=\""+path+"/house/update/"+e.id+"\" class=\"modifyBill\"><input type=\"button\" name=\"search\" value=\"修 改\" />"+"</a></label></td>"+ "<td class=\"house-price\"><label class=\"ui-green\"><input type=\"button\" name=\"search\" onclick=\"deleteHouse("+e.id+",this)\" value=\"刪 除\" />"+"</label></td>"; content+="</tr>"; $("table").append(content); }); //開始覆蓋分頁節點 $(".page-num-ul li").html("共"+data.total+"條記錄 "+data.pageNum+"/"+data.pages+"頁"); //每次添加上一頁、下一頁都要把上一次的結果清除,否則會累加 $(".page-num-ul a").remove(); //動態的添加上一頁、下一頁 if(!data.isFirstPage){ $(".page-num-ul").append("<a href=\"javascript:jump(1)\">首頁</a>"+ "<a href=\"javascript:jump("+(data.pageNum-1)+")\">上一頁</a>"); } if(!data.isLastPage){ $(".page-num-ul").append("<a href=\"javascript:jump("+(data.pageNum+1)+")\">下一頁</a>"+ "<a href=\"javascript:jump("+(data.pages)+")\">末頁</a>"); } },"json"); } function jump(pageNum){ //動態的修改當前頁數 $("[name='pageIndex']").val(pageNum); //提交 findHouse(); } function update(id){ alert("3") /* 上面a標簽點擊事件調用方法時候 把商品的id傳了過來,再把這個商品的id傳到后台*/ $.post(path+"/house/update",{"id":id},function(data){ /*alert(data);*/ /*onclick=\"update("+e.id+")\" */ },"text"); } function deleteHouse(id,a){ /* 上面a標簽點擊事件調用方法時候 把商品的id傳了過來,再把這個商品的id傳到后台*/ $.post(path+"/house/deleteById",{"id":id},function(data){ //現在后台已經刪除成功了,前台頁面也需要從對應的節點刪掉 alert(data); $(a).parents("tr").remove(); },"text"); } //時間函數,從數據庫拿的時間需要處理一下才能正確顯示在前端,直接在ajax調用這個函數即可 function getDateTime(obj){ var date = new Date(parseInt(obj,10)); //月份得+1,且只有個位數時在前面+0 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; //日期為個位數時在前面+0 var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //拼接日期顯示格式 var hours = date.getHours()<10?"0"+date.getHours():date.getHours(); var minutes = date.getMinutes()<10?"0"+date.getMinutes():date.getMinutes(); var seconds = date.getSeconds()<10?"0"+date.getSeconds():date.getSeconds(); var newTime = date.getFullYear() + "-" + month + "-" + currentDate+" "+hours+":"+minutes+":"+seconds; return newTime; }
</script>