网上图书商城项目学习笔记-024后台模块介绍及管理员登录
一、流程分析
1.后台页面和模块
2.管理员
二、代码
1.view层
(1)loigin.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <title>管理员登录页面</title> 8 9 <meta http-equiv="pragma" content="no-cache"> 10 <meta http-equiv="cache-control" content="no-cache"> 11 <meta http-equiv="expires" content="0"> 12 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 13 <meta http-equiv="description" content="This is my page"> 14 <!-- 15 <link rel="stylesheet" type="text/css" href="styles.css"> 16 --> 17 <script type="text/javascript" src="<c:url value='/jquery/jquery-1.5.1.js'/>"></script> 18 <script type="text/javascript"> 19 function checkForm() { 20 if(!$("#adminname").val()) { 21 alert("管理员名称不能为空!"); 22 return false; 23 } 24 if(!$("#adminpwd").val()) { 25 alert("管理员密码不能为空!"); 26 return false; 27 } 28 return true; 29 } 30 </script> 31 </head> 32 33 <body> 34 <h1>管理员登录页面</h1> 35 <hr/> 36 <p style="font-weight: 900; color: red">${msg }</p> 37 <form action="<c:url value='/AdminServlet'/>" method="post" onsubmit="return checkForm()"> 38 <input type="hidden" name="method" value="login"/> 39 管理员账户:<input type="text" name="adminname" value="" id="adminname"/><br/> 40 密 码:<input type="password" name="adminpwd" id="adminpwd"/><br/> 41 <input type="submit" value="进入后台"/> 42 </form> 43 </body> 44 </html>
2.servlet层
(1)AdminServlet.java
1 public class AdminServlet extends BaseServlet { 2 private AdminService service = new AdminService(); 3 4 public String login(HttpServletRequest req, HttpServletResponse resp) 5 throws ServletException, IOException { 6 Admin formAdmin = CommonUtils.toBean(req.getParameterMap(), Admin.class); 7 Admin admin = service.login(formAdmin); 8 if(admin == null) { 9 req.setAttribute("code", "error"); 10 req.setAttribute("msg", "用户名或密码错误!"); 11 return "f:/adminjsps/login.jsp"; 12 } 13 req.getSession().setAttribute("admin", admin); 14 return "f:/adminjsps/admin/index.jsp"; 15 } 16 }
3.service层
(1)AdminService.java
1 public class AdminService { 2 private AdminDao dao = new AdminDao(); 3 4 public Admin login(Admin admin) { 5 try { 6 return dao.findByNameAndPwd(admin.getAdminname(), admin.getAdminpwd()); 7 } catch (SQLException e) { 8 throw new RuntimeException(e); 9 } 10 } 11 12 }
4.dao层
(1)AdminDao.java
1 public class AdminDao { 2 3 private QueryRunner qr = new TxQueryRunner(); 4 5 public Admin findByNameAndPwd(String adminname, String adminpwd) throws SQLException { 6 String sql = "select * from t_admin where adminname=? and adminpwd=?"; 7 Admin admin = qr.query(sql, new BeanHandler<Admin>(Admin.class), adminname, adminpwd); 8 return admin; 9 } 10 }
You can do anything you set your mind to, man!