图书管理系统
实现登录功能
<script>
$(function () {
$("#btn_sub").click(function () {
$.post("loginServlet",$("#loginForm").serialize(),function (data) {
if(data.flag){
location.href="/bookServlet";
}else{
$("#errorMsg").html(data.errorMsg);
}
});
});
});
</script>
--------------------------------------------------
Map<String, String[]> map = request.getParameterMap();
User loginuser = new User();
try {
BeanUtils.populate(loginuser,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
UserService service = new UserServiceImpl();
HttpSession session = request.getSession();
User user = service.login(loginuser);
ResultInfo info = new ResultInfo();
if (user!=null){
session.setAttribute("user",user);
info.setFlag(true);
}else {
info.setFlag(false);
info.setErrorMsg("用户名或密码错误");
}
ObjectMapper mapper = new ObjectMapper();
response.setContentType("application/json;charset=utf-8");
mapper.writeValue(response.getOutputStream(),info);
实现分页功能
<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}"><<上一页</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}">下一页>></a></li>
</ul>
</div></td>
</tr>
</table>
</td>
</tr>
</table>
String currentPageStr = request.getParameter("currentPage");
String pageSizeStr = request.getParameter("pagesize");
int currentPage = 0;
if (currentPageStr != null && currentPageStr.length() > 0){
currentPage = Integer.parseInt(currentPageStr);
}else{
currentPage=1;
}
int pageSize = 0;
if (pageSizeStr != null && pageSizeStr.length() > 0){
pageSize = Integer.parseInt(pageSizeStr);
}else{
pageSize=5;
}
BookService service = new BookServiceImpl();
PageBean<Book> pb = service.pageQuery(currentPage, pageSize);
HttpSession session = request.getSession();
session.setAttribute("pb",pb);
request.getRequestDispatcher("/product_list.jsp").forward(request,response);
实现图书的修改和删除功能
<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>
String id = request.getParameter("id");
BookService service = new BookServiceImpl();
service.delBook(Integer.parseInt(id));
response.sendRedirect(request.getContextPath()+"/bookServlet");
request.setCharacterEncoding("UTF-8");
Map<String, String[]> map = request.getParameterMap();
Book book = new Book();
try {
BeanUtils.populate(book,map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
BookService service = new BookServiceImpl();
service.updateBook(book);
response.sendRedirect(request.getContextPath()+"/bookServlet");