图书管理系统(javaweb)

图书管理系统

实现登录功能

// 通过ajax实现异步登录,避免页面刷新。
<script>
        $(function () {
            //1.给登录按钮绑定单击事件
            //这里如果不使用ajax发送form表单数据,
            // 而使用form表单自己通过submit发送数据的话,会发生页面的跳转,而ajax请求不会发送页面跳转,所以下面的form表单的提交按钮记得设为button而不是submit。
            $("#btn_sub").click(function () {
                //2.发送ajax请求,提交表单数据
              $.post("loginServlet",$("#loginForm").serialize(),function (data) {
                    //data : {flag:false,errorMsg:''}
                    if(data.flag){
                        //登录成功
                        location.href="/bookServlet";
                    }else{
                        //登录失败
                        $("#errorMsg").html(data.errorMsg);
                    }
                });
            });
        });
</script>
    --------------------------------------------------
//loginservlet
		//获取用户输入的用户名以及密码
        Map<String, String[]> map = request.getParameterMap();
        //将获取到的用户名和密码封装成一个user对象
        User loginuser = new User();
        try {
            BeanUtils.populate(loginuser,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        // 调用service进行查询
        UserService service = new UserServiceImpl();
        HttpSession session = request.getSession();
        User user = service.login(loginuser);
        ResultInfo info = new ResultInfo();
        //进行判断如何user为空则用户输入的用户名或密码错误
        if (user!=null){
            //用户存在,登录成功!
            session.setAttribute("user",user);
            info.setFlag(true);
            //如果用户登陆成功,在登陆的Servlet中重定向到图书列表的Servlet
            //response.sendRedirect(request.getContextPath()+"/bookServlet");
        }else {
            //用户不存在,登陆失败!
            info.setFlag(false);
            info.setErrorMsg("用户名或密码错误");
        }
        //响应数据
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        mapper.writeValue(response.getOutputStream(),info);

实现分页功能

// jsp页面
<table cellspacing="0" cellpadding="1" rules="all"
	bordercolor="gray" border="1" id="DataGrid1"
	style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
	<tr
		style="FONT-WEIGHT: bold; FONT-SIZE: 12pt;  BACKGROUND-COLOR: #afd1f3">
		<td align="center" width="15%">	商品编号</td>
		<td align="center" width="20%">商品名称</td>
		<td align="center" width="15%">商品价格</td>
		<td align="center" width="15%">商品数量</td>
		<td width="15%" align="center">商品类别</td>
		<td width="10%" align="center">编辑</td>
		<td width="10%" align="center">删除</td>
	</tr>
	<c:forEach items="${pb.books}" var="b">
		<tr onmouseover="this.style.backgroundColor = 'white'"onmouseout="this.style.backgroundColor = '#F5FAFE';">
			<td>${b.id}</td>
			<td>${b.name }</td>
			<td>${b.price }</td>
			<td>${b.pnum }</td>
			<td>${b.category }</td>
			<td><a	href="${pageContext.request.contextPath}/findBookByIdServlet?id=${b.id }">
				<img src="${pageContext.request.contextPath}/images/3.jpg" border="0" style="CURSOR: hand"> </a>
			</td>
			<td><a	href="javascript:delBook('${b.id }','${b.name }')">
				<img src="${pageContext.request.contextPath}/images/4.jpg" width="16" height="16" border="0" style="CURSOR: hand">
			</a>
			</td>
		</tr>
	</c:forEach>	
</table>
<div class="pagination">
<ul>
<li class="nextPage"><a href="${pageContext.request.contextPath }/bookServlet?currentPage=${pb.currentPage==1?1:pb.currentPage-1}">&lt;&lt;上一页</a></li>
<li>第${pb.currentPage }/共${pb.totalPages }</li>
<li class="nextPage"><a href="${pageContext.request.contextPath }/bookServlet?currentPage=${pb.currentPage==pb.totalPages?pb.totalPages:pb.currentPage+1}">下一页&gt;&gt;</a></li>
							</ul>
						</div></td>
				</tr>
			</table>
		</td>
	</tr>
</table>
// servlet页面
//1.接收参数
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pagesize");
        //2.处理参数
        int currentPage = 0;//当前页码,如果不传递则模式显示第一页
        if (currentPageStr != null && currentPageStr.length() > 0){
                currentPage = Integer.parseInt(currentPageStr);
        }else{
            currentPage=1;
        }
            int pageSize = 0;//每页显示条数,如果不传递则默认每页显示5条
        if (pageSizeStr != null && pageSizeStr.length() > 0){
            pageSize = Integer.parseInt(pageSizeStr);
        }else{
            pageSize=5;
        }
        //3.调用Service查询pagebean对象。
        BookService service = new BookServiceImpl();

        PageBean<Book> pb = service.pageQuery(currentPage, pageSize);
        //4.将pagebean对象序列化为json,返回
        
        HttpSession session = request.getSession();
        session.setAttribute("pb",pb);
        //转发
        request.getRequestDispatcher("/product_list.jsp").forward(request,response);		

实现图书的修改和删除功能

// jsp页面就是上面那个,只是需要添加一个提示避免误删
<script>
		function delBook(id,name){
			if(confirm("是否确定删除:"+name+"?")){
				location.href="${pageContext.request.contextPath }/delBookServlet?id="+id;
			}
		}
</script>
//关于修改的跳转页面以及数据回显
<form action="${pageContext.request.contextPath}/updateBookServlet" method="post">
	<tr	style="FONT-WEIGHT: bold; FONT-SIZE: 12pt;  BACKGROUND-COLOR: #afd1f3"><!--  隐藏域 提交id-->
<input type="hidden" name="id" value="${book.id}">
        <td>
             <input type="text" class="form-control" id="id" name="id"  value="${book.id}" readonly="readonly" />
        </td>
        <td>
             <input type="text" class="form-control" id="name" name="name"  value="${book.name}" placeholder="请输入书名" />
        </td>
        <td>
             <input type="text" class="form-control" id="price" name="price"  value="${book.price}" placeholder="请输入价格" />
        </td>
        <td>
             <input type="text" class="form-control" id="pnum" name="pnum"  value="${book.pnum}" placeholder="请输入数量" />
        </td>
        <td>
             <input type="text" class="form-control" id="category" name="category"  value="${book.category}" placeholder="请输入类型" />
        </td>
</tr>
<tr style="FONT-WEIGHT: bold; FONT-SIZE: 12pt;  BACKGROUND-COLOR: #afd1f3">
         <td colspan="5">
          	<div class="form-group" style="text-align: center">
             <input class="btn btn-primary" type="submit" value="提交"/>
             <input class="btn btn-default" type="reset" value="重置" /> 
            </div>
        </td>
</tr>
</form>
//关于删除和修改的servlet
//删除
String id = request.getParameter("id");
        //创建Service对象
        BookService service = new BookServiceImpl();
        //调用方法
        service.delBook(Integer.parseInt(id));
        //重定向回去
        //request.getRequestDispatcher("/bookServlet").forward(request,response);
        //跳转回去
        response.sendRedirect(request.getContextPath()+"/bookServlet");
//修改
request.setCharacterEncoding("UTF-8");
        //获取修改页面传来的数据
        Map<String, String[]> map = request.getParameterMap();
        //将获取到的数据封装成一个book对象
        Book book = new Book();
        try {
            BeanUtils.populate(book,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用Service修改
        BookService service = new BookServiceImpl();
        service.updateBook(book);
        //跳转回去
        response.sendRedirect(request.getContextPath()+"/bookServlet");
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值