您好,登錄后才能下訂單哦!
今天小編給大家分享一下怎么用Java開(kāi)發(fā)用戶后臺(tái)管理系統(tǒng)的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。
管理員登錄功能、管理員列表操作、用戶列表操作、訂單管理
設(shè)計(jì)思路:前端頁(yè)面獲取輸入的數(shù)據(jù),然后發(fā)送Ajax請(qǐng)求,在servlet中獲取數(shù)據(jù)并調(diào)用service層中的方法進(jìn)行處理,service層中調(diào)用dao層的實(shí)現(xiàn)方法,最終servlet返回一個(gè)結(jié)果集,判斷登錄是否成功。
login.html主要功能代碼:
<script> $(function () { $("#btn_login").click(function () { //獲取數(shù)據(jù) let username = $("#username").val(); let password = $("#password").val(); //數(shù)據(jù)處理 $.post("admin/login", {username: username, password: password}, function (result) { if (result.flag) { location.href = "index.html"; } else { alert(result.errorMsg); } }); }); }) </script>
servlet中功能代碼:
//登錄 public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取用戶名和密碼 String username = request.getParameter("username"); String password = request.getParameter("password"); //封裝管理員對(duì)象 Administrator admin = new Administrator(); admin.setAname(username); admin.setPassword(password); //創(chuàng)建結(jié)果對(duì)象 ResultInfo resultInfo = new ResultInfo(); //調(diào)用service查詢 Administrator administrator = service.login(admin); if (administrator == null) { resultInfo.setFlag(false); resultInfo.setErrorMsg("用戶名或密碼錯(cuò)誤!"); } if (administrator != null && !administrator.getStatus().equals("Y")) { resultInfo.setFlag(false); resultInfo.setErrorMsg("該賬戶未激活!"); } if (administrator != null && administrator.getStatus().equals("Y")) { resultInfo.setFlag(true); //設(shè)置session request.getSession().setAttribute("username", administrator.getAname()); } //調(diào)用父類中定義的方法,轉(zhuǎn)json數(shù)據(jù)后回傳 writeValue(resultInfo, response); }
service層中代碼實(shí)現(xiàn):
@Override public Administrator login(Administrator administrator) { return dao.login(administrator); }
dao層中代碼實(shí)現(xiàn):
@Override public Administrator login(Administrator administrator) { Administrator admin = null; try { String sql = "SELECT * FROM administrator WHERE aname = ? AND PASSWORD = ?"; admin = template.queryForObject(sql, new BeanPropertyRowMapper<>(Administrator.class), administrator.getAname(), administrator.getPassword()); } catch (Exception e) { } return admin; }
實(shí)現(xiàn)效果:
設(shè)計(jì)思路:數(shù)據(jù)操作與登錄功能大同小異,實(shí)現(xiàn)了管理員的增刪改查,在此重點(diǎn)介紹分頁(yè)、模糊查詢。
admin_list.html主要功能代碼:
<script> $(function () { let name = null; $("#btn_search").click(function () { name = $('#admin_name').val(); load(null, name) //取消按鈕的默認(rèn)跳轉(zhuǎn)行為 導(dǎo)致會(huì)刷新頁(yè)面 return false; }) load(null, name); }); function load(currentPage, admin_name) { //發(fā)送ajax請(qǐng)求,請(qǐng)求route/pageQuery,傳遞cid $.get("admin/pageQuery", {currentPage: currentPage, admin_name: admin_name}, function (pb) { //定義字符 let lis = ""; //計(jì)算上一頁(yè)的頁(yè)碼 let beforeNum = pb.currentPage - 1; if (beforeNum <= 0) { beforeNum = 1; } let beforePage = '<a class="prev" href="javascript:load(' + beforeNum + ',\'' + admin_name + '\')" rel="external nofollow" ><<</a>'; lis += beforePage; //定義開(kāi)始位置begin,結(jié)束位置end let begin; let end; if (pb.totalPage < 10) { //總頁(yè)碼不足10頁(yè) begin = 1; end = pb.totalPage; } else { //總頁(yè)碼超過(guò)10頁(yè) begin = pb.currentPage - 5; end = pb.currentPage + 4; //前邊不足5個(gè),后邊補(bǔ)齊 if (begin < 1) { begin = 1; end = begin + 9; } if (end > pb.totalPage) { end = pb.totalPage; begin = end - 9; } } //展示分頁(yè)頁(yè)碼 for (let i = begin; i <= end; i++) { let li; //判斷當(dāng)前頁(yè)碼是否等于i,創(chuàng)建頁(yè)碼的li if (pb.currentPage === i) { li = '<a class="num" href="javascript:load(' + i + ',\'' + admin_name + '\')" rel="external nofollow" rel="external nofollow" ><span class="current">' + i + '</span></a>'; } else { li = '<a class="num" href="javascript:load(' + i + ',\'' + admin_name + '\')" rel="external nofollow" rel="external nofollow" >' + i + '</a>'; } //拼接字符串 lis += li; } //計(jì)算下一頁(yè)的頁(yè)碼 let nextNum = pb.currentPage + 1; if (nextNum >= pb.totalPage) { nextNum = pb.totalPage; } let nextPage = '<a class="next" href="javascript:load(' + nextNum + ',\'' + admin_name + '\')" rel="external nofollow" >>></a>'; lis += nextPage; //將lis內(nèi)容設(shè)置到ul $("#pageNum").html(lis); //列表數(shù)據(jù)展示 let admin_lis = ""; for (let i = 0; i < pb.list.length; i++) { let admin = pb.list[i]; let li; if (admin.status === "Y") { li = '<tr>\n' + ' <td>\n' + ' <input type="checkbox" name="checkedId" id="checkedId" lay-skin="primary">\n' + ' <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div>' + ' </td>\n' + ' <td>' + admin.aid + '</td>\n' + ' <td>' + admin.aname + '</td>\n' + ' <td>' + admin.phone + '</td>\n' + ' <td>' + admin.email + '</td>\n' + ' <td>' + admin.role + '</td>\n' + ' <td>' + admin.date + '</td>\n' + ' <td class="td-status">\n' + ' <span class="layui-btn layui-btn-normal layui-btn-mini" >已啟用</span></td>\n' + ' <td class="td-manage">\n' + ' <a onclick="member_stop(this,' + admin.aid + ')" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="啟用">\n' + ' <i class="layui-icon"></i>\n' + ' </a>\n' + ' <a title="編輯" onclick="xadmin.open(\'編輯管理員\',\'admin-edit.html?aid=' + admin.aid + '\',600,500)" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >\n' + ' <i class="layui-icon"></i>\n' + ' </a>\n' + ' <a title="刪除" onclick="member_del(this,\'要?jiǎng)h除的id\')" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >\n' + ' <i class="layui-icon"></i>\n' + ' </a>\n' + ' </td>\n' + '</tr>'; } else if (admin.status === "N") { li = '<tr>\n' + ' <td>\n' + ' <input type="checkbox" name="checkedId" id="checkedId" lay-skin="primary">\n' + ' <div class="layui-unselect layui-form-checkbox" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div>' + ' </td>\n' + ' <td>' + admin.aid + '</td>\n' + ' <td>' + admin.aname + '</td>\n' + ' <td>' + admin.phone + '</td>\n' + ' <td>' + admin.email + '</td>\n' + ' <td>' + admin.role + '</td>\n' + ' <td>' + admin.date + '</td>\n' + ' <td class="td-status">\n' + ' <span class="layui-btn layui-btn-normal layui-btn-mini" >已停用</span></td>\n' + ' <td class="td-manage">\n' + ' <a onclick="member_stop(this,' + admin.aid + ')" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" title="停用">\n' + ' <i class="layui-icon"></i>\n' + ' </a>\n' + ' <a title="編輯" onclick="xadmin.open(\'編輯管理員\',\'admin-edit.html?aid=' + admin.aid + '\',600,500)" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >\n' + ' <i class="layui-icon"></i>\n' + ' </a>\n' + ' <a title="刪除" onclick="member_del(this,\'要?jiǎng)h除的id\')" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >\n' + ' <i class="layui-icon"></i>\n' + ' </a>\n' + ' </td>\n' + '</tr>'; } admin_lis += li; } //設(shè)置列表數(shù)據(jù) $("#admin_msg").html(admin_lis); }); } </script>
servlet中功能代碼:
//分頁(yè)、模糊查詢 public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收參數(shù) String currentPageStr = request.getParameter("currentPage"); //接收admin_name管理員名稱 String admin_name = request.getParameter("admin_name"); //判斷admin_name是否為null if (admin_name != null && !"null".equals(admin_name) && admin_name.length() > 0) { admin_name = new String(admin_name.getBytes("iso-8859-1"), "utf-8"); } else { admin_name = ""; } int currentPage = 0; //當(dāng)前頁(yè)碼,不傳遞參數(shù),默認(rèn)為1 if (currentPageStr != null && currentPageStr.length() > 0) { currentPage = Integer.parseInt(currentPageStr); } else { currentPage = 1; } //每頁(yè)顯示條數(shù),默認(rèn)為5 int pageSize = 5; //調(diào)用service查詢PageBean對(duì)象 PageBean<Administrator> pb = service.pageQuery(currentPage, pageSize, admin_name); //將pageBean對(duì)象序列化為json,返回 writeValue(pb, response); }
service層中代碼實(shí)現(xiàn):
@Override public PageBean<Administrator> pageQuery(int currentPage, int pageSize, String admin_name) { //封裝PageBean PageBean<Administrator> pb = new PageBean<>(); //設(shè)置當(dāng)前頁(yè)碼 pb.setCurrentPage(currentPage); //設(shè)置每頁(yè)顯示條數(shù) pb.setPageSize(pageSize); //設(shè)置總記錄數(shù) int totalCount = dao.findTotalCount(admin_name); pb.setTotalCount(totalCount); //設(shè)置當(dāng)前頁(yè)顯示的數(shù)據(jù)集合 int start = (currentPage - 1) * pageSize; //開(kāi)始的記錄數(shù) List<Administrator> list = dao.findByPage(start, pageSize, admin_name); pb.setList(list); //設(shè)置總頁(yè)數(shù) = 總記錄數(shù)/每頁(yè)顯示條數(shù) int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1; pb.setTotalPage(totalPage); return pb; }
dao層中代碼實(shí)現(xiàn):
@Override public int findTotalCount(String admin_name) { //定義sql模板 String sql = "SELECT COUNT(*) FROM administrator WHERE 1=1 "; StringBuilder sb = new StringBuilder(sql); //條件 List<Object> params = new ArrayList<>(); if (admin_name != null && admin_name.length() > 0) { sb.append(" and aname like ? "); params.add("%" + admin_name + "%"); } sql = sb.toString(); return template.queryForObject(sql, Integer.class, params.toArray()); } @Override public List<Administrator> findByPage(int start, int pageSize, String admin_name) { String sql = "SELECT * FROM administrator WHERE 1=1 "; StringBuilder sb = new StringBuilder(sql); //條件 List<Object> params = new ArrayList<>(); //判斷參數(shù)是否有值 if (admin_name != null && admin_name.length() > 0) { sb.append(" and aname like ? "); params.add("%" + admin_name + "%"); } //分頁(yè)條件 sb.append(" limit ?,? "); params.add(start); params.add(pageSize); //轉(zhuǎn)字符串 sql = sb.toString(); return template.query(sql, new BeanPropertyRowMapper<>(Administrator.class), params.toArray()); }
實(shí)現(xiàn)效果:
設(shè)計(jì)思路:與管理員列表設(shè)計(jì)思路基本類似。
實(shí)現(xiàn)效果:
設(shè)計(jì)思路:與管理員列表設(shè)計(jì)思路基本類似。
以上就是“怎么用Java開(kāi)發(fā)用戶后臺(tái)管理系統(tǒng)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。