房屋管理系统简单Damo

来源:互联网 发布:网络维护和通信课程 编辑:程序博客网 时间:2024/05/07 07:32

1.登录页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <script type="text/javascript" src="js/jquery-2.1.0.js"></script>
     <script>
        $(function () {
            $("#submit").click(function () {
                var username = $("input:eq(0)");
                var password = $("input:eq(1)");
                var msg = ""
                if ($.trim(username.val()) == "") {
                    msg = "用户名不能为空!";
                    username.focus();
                } else  if ($.trim(password.val()) == "") {
                    msg = "密码不能为空!";
                    password.focus();
                }
                if (msg != "") {
                    alert(msg);
                } else {
                    // 获取表单中的参数
                    var params = $("#form").serialize();
                    
                    // 发送登录的异步请求
                    $.ajax({
                        url: "user/login",
                        data: params,
                        success: function (data) {
                            if (data == "ok") {
                                window.location.href = "house/selectAll"
                            } else {
                                alert("账号或密码错误");
                            }
                        }
                    })
                }
            });
            // 为document绑定onkeydown事件监听是否按了回车键
            $(document).keydown(function (event) {
                if (event.keyCode === 13) { // 按了回车键
                    $("#submit").trigger("click");
                }
            });
        });
    </script>
  </head>
  <body>
  <form id="form">
      用户名:<input name="userName"><br>
      密 码:<input name="userPwd"><br>
      <input value="登录" id="submit" type="button">  
  </form>
  </body>
</html>
2展示页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'house.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
<script type="text/javascript">
    $(function() {
        $("#che").click(function() {
            if (this.checked) {
                $(".in").prop("checked", true);
            } else {
                $(".in").prop("checked", false);
            }
        });
        $("#del").click(function() {
            if (confirm("是否删除")) {
                var ids = [];
                $(".in").each(function() {
                    ids.push($(this).val());
                });
                $.post("house/del", {
                    "ids" : ids.join(",")
                }, function() {
                    location.reload();
                });
            }
        });
    })
</script>
</head>

<body>
    <form action="house/selectHouseName">
        <input type="button" value="添加房屋信息"
            onclick="window.location.href='house/selectPerson'">
        <input
            type="button" value="删除房屋 信息" id="del">
             <input type="submit"  value="搜索">
            <select name="pid">
            <c:forEach items="${listPerson }" var="l">
                <option value="${l.pid}">${l.personName}</option>
            </c:forEach>
        </select>
    </form>

    <table border="1">
        <tr>
            <td><input type="checkbox" id="che"></td>
            <td>房屋名称</td>
            <td>房屋图片</td>
            <td>房屋单价</td>
            <td>房屋面积</td>
            <td>房屋总价</td>
            <td>购买日期</td>
            <td>所属户主</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${listHouse.list }" var="l">
            <tr>
                <td><input type="checkbox" value="${l.hid}" class="in"></td>
                <td>${l.houseName }</td>
                <td><img alt="" src="images/${l.houserImg}" width="30" height="30"></td>
                <td>${l.housePrice }</td>
                <td>${l.houseArea }</td>
                <td>${l.totalCount }</td>
                <td><f:formatDate value="${l.buyDate}" /></td>
                <td>${l.person.personName}</td>
                <td><span style="color:maroon;" onclick="window.location.href='house/selectById?id=${l.hid}'">修改房屋信息</span></td>
            </tr>
        </c:forEach>
    </table>
    <button
        onclick="window.location.href='${url}?pageNo=${listHouse.prePage}'">上一页</button>
    <c:forEach begin="1" end="${listHouse.lastPage}" var="l">
        <button onclick="window.location.href='${url}?pageNo=${l}'">${l}</button>
    </c:forEach>
    <button
        onclick="window.location.href='${url}?pageNo=${listHouse.nextPage}'">下一页</button>
</body>
</html>

3添加页面

<%@ page language="java" import="java.util.*" pageEncoding="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 + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'add.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
<script type="text/javascript">
    $(function() {
        var s0 = false;
        var s1 = false;
        var s2 = false;
        var s3 = false;

        $("input:eq(0)").blur(function() {
            var houseName = $(this).val();
            $("#id0").empty();
            if (houseName == null || houseName == "") {
                s0 = false;
                $(".c:eq(0)").after("<span id='id0'>输入为空</span>");
            } else {
                $.ajax({
                    url : "house/selectByName",
                    data : {
                        "houseName" : houseName
                    },
                    success : function(data) {
                        if (data == "ok") {
                            s0 = true;
                            $(".c:eq(0)").after("<span id='id0'>输入正确</span>");
                        } else {
                            s0 = false;
                            $(".c:eq(0)").after("<span id='id0'>用户已存在</span>");
                        }
                    }
                });
            }
        });
        $("input:eq(1)").blur(function() {
            var name = $(this).val();
            $("#id1").empty();
            if (name == null || name == "") {
                s1 = false;
                $(".c:eq(1)").after("<span id='id1'>输入为空</span>");
            } else {
                s1 = true;
                $(".c:eq(1)").after("<span id='id1'>输入正确</span>");
            }
        });
        $("input:eq(2)").blur(function() {
            var name = $(this).val();
            $("#id2").empty();
            if (name == null || name == "") {
                s2 = false;
                $(".c:eq(2)").after("<span id='id2'>输入为空</span>");
            } else {
                s2 = true;
                $(".c:eq(2)").after("<span id='id2'>输入正确</span>");
            }
        });
        $("input:eq(3)").blur(function() {
            var name = $(this).val();
            $("#id3").empty();
            if (name == null || name == "") {
                s3 = false;
                $(".c:eq(3)").after("<span id='id3'>请上传图片</span>");
            } else {
                s3 = true;
                $(".c:eq(3)").after("<span id='id3'>上传成功</span>");
            }
        });
        $(":submit").click(function() {
            if (s0 & s1 & s2 & s3) {
                $(this).submit();
            } else {

                return false;
            }

        });

    });
</script>
</head>

<body>
    <form action="house/addHouse" enctype="multipart/form-data" method="post">
        房屋名称:<input name="houseName"><span class="c"></span><br>
        房屋单价:<input name="housePrice"><span class="c"></span><br>
        房屋面积:<input name="houseArea"><span class="c"></span><br>
        房屋图片:<input type="file" name="file"><span class="c"></span><br>
        房屋户主:<select name="person.pid">
            <c:forEach items="${list}" var="l">
                <option value="${l.pid}">${l.personName}</option>
            </c:forEach>
        </select><br>
        <input type="submit" value="添加" >
    </form>
</body>
</html>
4.修改页面

<%@ page language="java" import="java.util.*" pageEncoding="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 + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'add.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript" src="js/jquery-2.1.0.js"></script>

</head>

<body>
    <form action="house/updateHouse" enctype="multipart/form-data" method="post">
    <input name="hid" value="${house.hid}" hidden>
        房屋名称:<input name="houseName" value="${house.houseName}"><span class="c"></span><br>
        房屋单价:<input name="housePrice" value="${house.housePrice}"><span class="c"></span><br>
        房屋面积:<input name="houseArea" value="${house.houseArea}"><span class="c"></span><br>
        房屋图片:<input type="file" name="file" ><%-- <img  src="${house.houseImg}"> --%><span class="c"></span><br>
        房屋户主:<select name="person.pid">
            <c:forEach items="${list}" var="l">
            <c:choose>
                <c:when test="${l.pid==house.person.pid}">
                <option value="${l.pid}" selected="selected">${l.personName}</option>
                </c:when>
                <c:otherwise>
                <option value="${l.pid}">${l.personName}</option>
                </c:otherwise>
            </c:choose>
            </c:forEach>
        </select><br>
        <input type="submit" value="修改" >
    </form>
</body>
</html>

控制器

package com.bw.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.spi.http.HttpContext;

import org.apache.commons.io.FileUtils;
import org.jboss.weld.servlet.ServletApiAbstraction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bw.pojo.House;
import com.bw.pojo.Person;
import com.bw.pojo.PersonExample;
import com.bw.service.HouseService;
import com.bw.service.PersonService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("house")
public class HouseController {

    @Autowired
    private HouseService houseService;
    @Autowired
    private PersonService personService;

    @SuppressWarnings("resource")
    @RequestMapping("selectAll")
    public ModelAndView selectAll(ModelAndView modelAndView,
        PersonExample example, Integer pageNo, Integer pageSize) {
        Jedis jedis = new Jedis("192.168.134.10", 6379);
        String string = jedis.get("listPerson");
        List<Person> listPerson;
        if (string == null) {
            listPerson = personService.selectByExample(example);
            jedis.set("listPerson", JSON.toJSONString(listPerson));
        } else {
            listPerson = JSON.parseArray(string, Person.class);
        }
        modelAndView.addObject("listPerson", listPerson);
        if (pageNo == null || pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<House> listHouse = houseService.selectAll();
        PageInfo<House> list = new PageInfo<House>(listHouse);
        modelAndView.addObject("url", "house/selectAll");
        modelAndView.addObject("listHouse", list);
        modelAndView.setViewName("house");

        return modelAndView;

    }

    @RequestMapping("selectPerson")
    public ModelAndView selectPerson(ModelAndView modelAndView,
            PersonExample example) {
        Jedis jedis = new Jedis("192.168.134.10", 6379);
        String string = jedis.get("listPerson");
        List<Person> listPerson;
        if (string == null) {
            listPerson = personService.selectByExample(example);
            jedis.set("listPerson", JSON.toJSONString(listPerson));
        } else {
            listPerson = JSON.parseArray(string, Person.class);
        }
        modelAndView.addObject("list", listPerson);
        modelAndView.setViewName("add");
        return modelAndView;
    }

    @RequestMapping("selectByName")
    @ResponseBody
    public String selectByName(String houseName) {
        House list = houseService.selectByHouseName(houseName);
        if (list == null) {
            return "ok";
        } else {
            return "error";
        }
    }

    @RequestMapping("addHouse")
    public String addHouse(House house, MultipartFile file,
            HttpServletRequest request) throws IllegalStateException,
            IOException {
        String filename = file.getOriginalFilename();
        String suffixName = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID() + suffixName;
        // File file = new
        // File("D:\\javawab\\apache-tomcat-7.0.62\\webapps\\zyc20171025\\images");
        String realPath = request.getSession().getServletContext()
                .getRealPath("/images");
        File filePath = new File(realPath + File.separator + filename);
        file.transferTo(filePath);
        house.setBuyDate(new Date());
        house.setHouserImg(filename);
        house.setTotalCount(house.getHouseArea() * house.getHousePrice());
        houseService.addHouse(house);
        return "redirect:/house/selectAll";
    }
    @RequestMapping("del")
    @ResponseBody
    public String del(String ids){
        houseService.deleteByIds(ids) ;
        return "ok";
        
    }
    private  int i;
    @RequestMapping("selectHouseName")
    public ModelAndView selectHouseName(ModelAndView modelAndView,
            PersonExample example, Integer pageNo, Integer pageSize,Integer pid) {
            if (pid!=null) {
                    i=pid;
            }
            Jedis jedis = new Jedis("192.168.134.10", 6379);
            String string = jedis.get("listPerson");
            List<Person> listPerson;
            if (string == null) {
                listPerson = personService.selectByExample(example);
                jedis.set("listPerson", JSON.toJSONString(listPerson));
            } else {
                listPerson = JSON.parseArray(string, Person.class);
            }
            modelAndView.addObject("listPerson", listPerson);
            if (pageNo == null || pageNo <= 0) {
                pageNo = 1;
            }
            if (pageSize == null) {
                pageSize = 2;
            }
            PageHelper.startPage(pageNo, pageSize);
            List<House> listHouse = houseService.selectByPId(i);
            PageInfo<House> list = new PageInfo<House>(listHouse);
            modelAndView.addObject("url", "house/selectHouseName");
            modelAndView.addObject("listHouse", list);
            modelAndView.setViewName("house");

            return modelAndView;

        }
    @RequestMapping("selectById")
    public ModelAndView selectById(ModelAndView modelAndView,Integer id,PersonExample example){
        Jedis jedis = new Jedis("192.168.134.10", 6379);
        String string = jedis.get("listPerson");
        List<Person> listPerson;
        if (string == null) {
            listPerson = personService.selectByExample(example);
            jedis.set("listPerson", JSON.toJSONString(listPerson));
        } else {
            listPerson = JSON.parseArray(string, Person.class);
        }
        House house = houseService.selectById(id);
        modelAndView.addObject("list", listPerson);
        modelAndView.addObject("house", house);
        modelAndView.setViewName("update");
        return modelAndView;
        
    }
    @RequestMapping("updateHouse")
    public String  updateHouse(House house,MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
        String filename = file.getOriginalFilename();
        String suffixName = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID() + suffixName;
        // File file = new
        // File("D:\\javawab\\apache-tomcat-7.0.62\\webapps\\zyc20171025\\images");
        String realPath = request.getSession().getServletContext()
                .getRealPath("/images");
        File filePath = new File(realPath + File.separator + filename);
        file.transferTo(filePath);
        house.setTotalCount(house.getHousePrice()*house.getHouseArea());
        house.setHouserImg(filename);
        houseService.updateHouse(house);
        return "redirect:/house/selectAll";
        
    }

    
}

4houseMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bw.dao.HouseMapper" >
 <resultMap id="rm" type="com.bw.pojo.House" >
    <id column="hid" property="hid" jdbcType="INTEGER" />
    <result column="house_name" property="houseName" jdbcType="VARCHAR" />
    <result column="houser_img" property="houserImg" jdbcType="VARCHAR" />
    <result column="house_area" property="houseArea" jdbcType="INTEGER" />
    <result column="house_price" property="housePrice" jdbcType="DOUBLE" />
    <result column="total_count" property="totalCount" jdbcType="DOUBLE" />
    <result column="buy_date" property="buyDate" jdbcType="DATE" />
       <association property="person"  javaType="person">
              <id column="pid" property="pid" jdbcType="INTEGER" />
    <result column="person_name" property="personName" jdbcType="VARCHAR" />
       </association>
  </resultMap>
 <select id="selectAll" resultMap="rm">
     select * from house h,person p where h.person_id= p.pid
 </select>
  <select id="selectByName" parameterType="string" resultMap="rm">
     select * from house h,person p where h.person_id= p.pid and p.person_name=#{value}
 </select>
  <select id="selectByHouseName" parameterType="string" resultMap="rm">
     select * from house h,person p where h.person_id= p.pid and h.house_name=#{value}
 </select>
  <delete id="deleteByIds" parameterType="string">
     delete from house where hid in (${value})
 </delete>
 <select id="selectById" parameterType="integer" resultMap="rm">
     select * from house h,person p where h.person_id= p.pid and h.hid = #{value}
 </select>
 <select id="selectByPId" parameterType="integer" resultMap="rm">
     select * from house h,person p where h.person_id= p.pid and h.person_id = #{value}
 </select>
 <update id="updateHouse" parameterType="house">
     update house set house_name=#{houseName},houser_img=#{houserImg},house_area=#{houseArea},house_price=#{housePrice},total_count=#{totalCount},person_id=#{person.pid} where hid = #{hid}
 </update>
 <insert id="addHouse" parameterType="house">
     insert into house(house_name,houser_img,house_area,house_price,total_count,buy_date,person_id) values(#{houseName},#{houserImg},#{houseArea},#{housePrice},#{totalCount},#{buyDate},#{person.pid});
 </insert>
</mapper>

原创粉丝点击