我的第一个项目(员工绩效管理系统 步骤超级详细---未完待更...)
生活随笔
收集整理的這篇文章主要介紹了
我的第一个项目(员工绩效管理系统 步骤超级详细---未完待更...)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一.分級(jí)建立項(xiàng)目包 1.建立department包(實(shí)現(xiàn)有關(guān)department表的操作) ① 建立dao包(實(shí)現(xiàn)數(shù)據(jù)庫(kù)中department的增刪改查操作? 建立java和數(shù)據(jù)庫(kù)直接的聯(lián)系) (1)添加記錄 ?? ??? ?? (2)根據(jù)主鍵刪除 ?? ??? ???(3)根據(jù)查詢條件刪除 ?? ??? ???(4)根據(jù)主鍵修改其它全部字段 ?? ??? ???(5)根據(jù)主鍵修改部分其他字段 ?? ??? ???(6)根據(jù)多條件模糊查詢記錄 ?? ??? ???(7)根據(jù)主鍵查詢一條記錄 ② 建立model包(創(chuàng)建有關(guān)department實(shí)體類) ③?建立service包 ④?建立servlet包 2.建立employee包(實(shí)現(xiàn)有關(guān)employee表的操作) ① 建立dao包(實(shí)現(xiàn)數(shù)據(jù)庫(kù)中employee的增刪改查操作??建立java和數(shù)據(jù)庫(kù)直接的聯(lián)系) (1)添加記錄 (2)根據(jù)主鍵刪除 (3)根據(jù)查詢條件刪除 (4)根據(jù)主鍵修改其它全部字段 (5)根據(jù)主鍵修改部分其他字段 (6)根據(jù)多條件模糊查詢記錄 (7)根據(jù)主鍵查詢一條記錄 ② 建立model包(創(chuàng)建有關(guān)employee實(shí)體類) ③?建立service包 ④?建立servlet包 3.建立project包(實(shí)現(xiàn)有關(guān)project表的操作) ① 建立dao包(實(shí)現(xiàn)數(shù)據(jù)庫(kù)中project的增刪改查操作? 建立java和數(shù)據(jù)庫(kù)直接的聯(lián)系) (1)添加記錄 (2)根據(jù)主鍵刪除 (3)根據(jù)查詢條件刪除 (4)根據(jù)主鍵修改其它全部字段 (5)根據(jù)主鍵修改部分其他字段 (6)根據(jù)多條件模糊查詢記錄 (7)根據(jù)主鍵查詢一條記錄 ② 建立model包(創(chuàng)建有關(guān)project實(shí)體類) ③?建立service包 ④?建立servlet包 4.建立score包(實(shí)現(xiàn)有關(guān)score表的操作) ① 建立dao包(實(shí)現(xiàn)數(shù)據(jù)庫(kù)中score的增刪改查操作? 建立java和數(shù)據(jù)庫(kù)直接的聯(lián)系) (1)添加記錄 (2)根據(jù)主鍵刪除 (3)根據(jù)查詢條件刪除 (4)根據(jù)主鍵修改其它全部字段 (5)根據(jù)主鍵修改部分其他字段 (6)根據(jù)多條件模糊查詢記錄 (7)根據(jù)主鍵查詢一條記錄 ② 建立model包(創(chuàng)建有關(guān)score實(shí)體類) ③?建立service包 ④?建立servlet包 5.建立until 工具包(封裝工具類簡(jiǎn)化代碼) ① 建立JDBCUtil工具類(數(shù)據(jù)庫(kù)加載、數(shù)據(jù)庫(kù)連接、數(shù)據(jù)庫(kù)操作、數(shù)據(jù)庫(kù)關(guān)閉、) ② 建立FmtEmpty(判空) ③?建立jdbc 配置文件 6.建立init 工具包(封裝工具類簡(jiǎn)化代碼) ① 建立CharFilter工具類(解決數(shù)據(jù)庫(kù)中文亂碼的問題) 二、 整個(gè)系統(tǒng)的交互過程(開發(fā)流程) ①建立工具類 (1)hedaer? 封裝路徑(java代碼獲取、EL表達(dá)式獲取) ②創(chuàng)建注冊(cè)界面(reg.jsp)? css(base.css -- window樣式) <%@ page language="java" contentType="text/html; charset=UTF-8" ????pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <!-- <meta charset="UTF-8"> --> <title>注冊(cè)頁(yè)面</title> <!-- <link rel="stylesheet"??href="/demo220111/web/base/layui/css/layui.css" > --> <!-- <script type="text/javascript"??src="/demo220111/web/base/layui/layui.js" ></script> --> <!-- 利用包含來簡(jiǎn)化代碼(工具類) --> <%@ include file="/web/header.jsp" %> </head> <body style="background: #f1f1f1"> <div class="window"> ?????<fieldset class="layui-elem-field" style="margin: 10px"> ???????<legend>注冊(cè)</legend> ???????<div class="layui-field-box"> ?????????<form class="layui-form layui-form-pane" > ???????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">賬號(hào)</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="code" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入賬號(hào)"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">密碼</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="password" name="pass"??required??lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入密碼"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">姓名</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="name" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入姓名"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="button" value="注冊(cè)"??class="layui-btn" lay-submit lay-filter="reg" > ?????????????????????????<input type="reset" value="重置"??class="layui-btn layui-btn-primary"> ??????????????????????</div> ??????????????????????<input type="button" value="返回登錄"??class="layui-btn" οnclick=""> ????????????????</div> ?? ??? ??? ??? ??? ?<!-- reg是傳遞給EmployeeServlet 參數(shù) --> ????????????????<input type="hidden" name="action" value="reg" > ?????????</form> ???????</div> ?????</fieldset> ????? </div> <script type="text/javascript"> var form = layui.form; var $ = layui.jquery; var layer = layui.layer; form.on("submit(reg)",function(data){ ?????//layer.msg("1111"); ?????console.log(data.field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url : base.app +"/EmployeeServlet", ???????????type : "post", ???????????data : data.field, //a=b&c=d /{a:b,c:d} ???????????dataType : "text",//text / json ???????????success : function(data){ ????????????????console.log(data) ????????????????// todo ???????????} ?????}); }); </script> ????? </body> </html> (1)?<input type="button" value="注冊(cè)" class="layui-btn" lay-submit lay-filter="reg" >? 輸入框不能為空 觸發(fā)回調(diào)方法(測(cè)試段) var form = layui.form; var layer = layui.layer; form.on("submit(reg)",function(data){ ?????layer.msg("1111"); }); (2)注冊(cè)頁(yè)面請(qǐng)求,是請(qǐng)求到employee表中,注冊(cè)員工信息 觸發(fā)回調(diào)方法(應(yīng)用段) <script type="text/javascript"> var form = layui.form; var $ = layui.jquery; var layer = layui.layer; form.on("submit(reg)",function(data){ ?????//layer.msg("1111"); ?????console.log(data.field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url:"", ???????????data:"", ???????????type:"", ???????????dataType:"", ???????????success:function(data){ ???????????????? ???????????} ?????}); }); </script> (3)編寫EmployeeServlet類中的方法 (接受請(qǐng)求、獲取數(shù)據(jù)、封裝對(duì)象、調(diào)用方法、返回結(jié)果) 注解@WebServlet("/EmployeeServlet") 實(shí)現(xiàn)ajax請(qǐng)求交互 private static final long serialVersionUID =1L; ????? ?????@Override ?????protected void doGet(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????doPost(req, resp); ?????} ????? ? ??????@Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????String res = null; ???????????switch (req.getParameter("action")) { ????????????????case "reg": ?????????????????????res = reg(req); ?????????????????????break; ???????????} ???????????// ajax 請(qǐng)求接收 ???????????PrintWriter writer = resp.getWriter(); ???????????writer.write(res); ???????????writer.flush(); ???????????writer.close(); ???????????writer = null ; ?????} ?????private String reg(HttpServletRequest req) { ???????????String code = req.getParameter("code"); ???????????String pass = req.getParameter("pass"); ???????????String name = req.getParameter("name"); ??????????? ???????????EmployeeModel model = new??EmployeeModel(code,name,pass); ???????????return service.insert(model); ?????} (4) 實(shí)現(xiàn)員工注冊(cè)功能(主要測(cè)試能否交互) (5) 完善員工注冊(cè)功能(主要完善業(yè)務(wù)邏輯) PS: ?????private static String table = "employee"; ? ? ?private static String cols =??"code,name,pass,code_dept,image"; ?????// 此處的數(shù)據(jù)庫(kù)表字段 在方法中可以不使用某一個(gè),但是不能在方法中使用沒有的表字段 1.reg.jsp 文件 form.on("submit(reg)",function(data){ ?????//layer.msg("1111"); ?????//console.log(data.field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url : base.app +"/EmployeeServlet", ???????????type : "post", ???????????data : data.field, //a=b&c=d /{a:b,c:d} ???????????dataType : "text",//text / json ???????????success : function(data){ ????????????????// console.log(data)??測(cè)試ajax請(qǐng)求是否可用 ????????????????// todo ????????????????if(data==1){ ?????????????????????layer.msg("注冊(cè)成功") ????????????????}else if(data == "repeat"){ ?????????????????????layer.msg("注冊(cè)失敗,賬號(hào)重復(fù)") ????????????????}else{ ?????????????????????layer.msg("注冊(cè)失敗") ????????????????} ???????????} ?????}); }); ? ? 2.EmployeeServiceImpl 文件 public String insert(EmployeeModel model) { ???????????// return dao.insert(model) + "";??測(cè)試能否實(shí)現(xiàn)交互 ??????????? ???????????// 完善業(yè)務(wù)邏輯 ???????????// 在EmployeeModel 創(chuàng)建code構(gòu)造方法 ???????????EmployeeModel m1 = new??EmployeeModel(model.getCode()); ???????????EmployeeModel mdb = dao.selectModel(m1); ???????????if (mdb != null) ????????????????return "repeat"; ???????????return dao.insert(model) + ""; ???????????} ? ? 3.EmployeeModel? 創(chuàng)建code 構(gòu)造方法 public EmployeeModel(String code) { ???????????super(); ???????????this.code = code; ?????} 4.給reg.jsp 頁(yè)面添加交互操作(判斷注冊(cè)是否成功) var form = layui.form; var $ = layui.jquery; var layer = layui.layer; form.on("submit(reg)",function(data){ ?????//layer.msg("1111"); ?????//console.log(data.field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url : base.app +"/EmployeeServlet", ???????????type : "post", ???????????data : data.field, //a=b&c=d /{a:b,c:d} ???????????dataType:'text',//text / json ???????????success : function(data){ ????????????????//console.log(data)??//測(cè)試ajax請(qǐng)求是否可用 ????????????????// todo ????????????????if(data==1){ ?????????????????????layer.msg("注冊(cè)成功") ????????????????}else if(data=="repeat"){ ?????????????????????layer.msg("注冊(cè)失敗,賬號(hào)重復(fù)") ????????????????}else{ ?????????????????????layer.msg("注冊(cè)失敗") ????????????????} ???????????} ???????????error:function(){// 異常處理 ????????????????alert("注冊(cè)失敗,賬號(hào)重復(fù)"); ???????????} ?????}); }); 5.使用工具類來簡(jiǎn)化reg.jsp 頁(yè)面添加操作的代碼 header.jsp (將引用路徑進(jìn)行封裝) <!-- java代碼 獲取工程名 --> <% String app=request.getContextPath(); %> <meta charset="UTF-8"> <link rel="stylesheet" href="<%=app %>/web/base/css/base.css" > <link rel="stylesheet" href="<%=app??%>/web/base/layui/css/layui.css" > <script type="text/javascript" src="<%=app??%>/web/base/layui/layui.js" ></script> <!-- EL表達(dá)式 獲取工程名 --> <script> var base={ ???????????app:"${pageContext.request.contextPath}" ??????????? } </script> <script type="text/javascript" src="<%=app??%>/web/base/js/base.js"></script> base.js 工具類(將操作方法進(jìn)行封裝) var form = layui.form; var $ = layui.jquery; var layer = layui.layer; // 添加監(jiān)聽 function formSubmit(event,url,dataType,func){ ?????//console.log(event) 調(diào)試 ?????form.on("submit(" +event+")",function(data){ ?????//???console.log(data.field)調(diào)試 ???????????ajax(url,data.field,dataType,func) ?????}) } function ajax(url,field,dataType,func){ ?????//console.log(field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url : base.app + url, ???????????type : "post", ???????????data : field, ???????????dataType : dataType, //后采用 ???????????//dataType : "text",//text / json ???????????success : func //?????????error:function(){// 異常處理 //??????????????alert("注冊(cè)失敗,賬號(hào)重復(fù)"); //?????????} ??????????? ?????}); } 封裝 關(guān)于請(qǐng)求的? FmtRequest 類 public class FmtRequest { ????? ????? ?????/** ??????* 根據(jù)傳過來的所有參數(shù)得到實(shí)體類的對(duì)象 ??????* 根據(jù)請(qǐng)求的參數(shù)情況反射實(shí)體類的對(duì)象???(請(qǐng)求的參數(shù)名與實(shí)體類的屬性名一致 ,并且一個(gè)參數(shù)名對(duì)應(yīng)一個(gè)參數(shù)值 ) ??????* ??????* @param <T> 反射 ??????* @param req ??????* @param clazz ??????* @return ??????*/ ?????public static <T> T parseModel(HttpServletRequest??req,Class<T> clazz) { ???????????T obj = null; ???????????try { ????????????????obj = clazz.newInstance(); ???????????} catch (InstantiationException |??IllegalAccessException e) { ????????????????e.printStackTrace(); ????????????????return null; ???????????} ???????????Map<String, String[]> map = req.getParameterMap(); //??傳過來的所有參數(shù) ???????????for (Entry<String, String[]> entry : map.entrySet())??{ ????????????????String name = entry.getKey(); ????????????????if("action".equals(name)) ?????????????????????continue; ????????????????try { ?????????????????????Field field =??clazz.getDeclaredField(name); ?????????????????????field.setAccessible(true); ?????????????????????field.set(obj,entry.getValue()[0]); ????????????????} catch (NoSuchFieldError | SecurityException |??NoSuchFieldException | IllegalArgumentException |??IllegalAccessException e) { ?????????????????????e.printStackTrace(); ????????????????} ???????????} ??????????? ???????????return obj; ?????} ????? ????? ?????/** ??????* 根據(jù)所傳過來的映射關(guān)系得到實(shí)體類對(duì)象 ??????* 根據(jù)映射關(guān)系反射得到實(shí)體類的對(duì)象 ??????* ??????* @param <T> ??????* @param req ??????* @param clazz ??????* @param fields key=屬性名???value=參數(shù)名 ??????*/ ?????private static <T> T parseModel(HttpServletRequest??req,Class<T> clazz,Map<String,String> fields) { ???????????T obj = null; ???????????try { ????????????????obj = clazz.newInstance(); ???????????} catch (InstantiationException |??IllegalAccessException e) { ????????????????e.printStackTrace(); ????????????????return null; ???????????} ???????????for (Entry<String, String> entry : fields.entrySet())??{ // 根據(jù)所傳過來的映射關(guān)系 ????????????????String name = entry.getKey();??// 得到實(shí)體類的屬性名 ????????????????String val =req.getParameter(entry.getValue());???// 得到實(shí)體類的參數(shù)名 ????????????????try { ?????????????????????Field field =??clazz.getDeclaredField(name); ?????????????????????field.setAccessible(true); ?????????????????????field.set(obj,val); ????????????????} catch (NoSuchFieldError | SecurityException |??NoSuchFieldException | IllegalArgumentException |??IllegalAccessException e) { ?????????????????????e.printStackTrace(); ????????????????} ???????????} ??????????? ???????????return obj; ?????} ????? ????? ????? ?????/** ??????* 將 ajax 請(qǐng)求進(jìn)行封裝 ??????* @param wr ??????* @param val ??????*/ ?????public static void write(Writer wr,String val) { ???????????try { ????????????????wr.write(val); ????????????????wr.close(); ????????????????wr.flush(); ???????????} catch (IOException e) { ????????????????e.printStackTrace(); ???????????} ???????????wr = null; ??????????? ?????} } 7.通過應(yīng)用FmtRequest 類 簡(jiǎn)化EmployeeServlet中的代碼 簡(jiǎn)化EmployeeServlet中的代碼 private String reg(HttpServletRequest req) { ???????????//String code = req.getParameter("code"); ???????????//String pass = req.getParameter("pass"); ???????????//String name = req.getParameter("name"); ???????????//EmployeeModel model = new??EmployeeModel(code,name,pass); ???????????//return service.insert(model); ??????????? ???????????// 利用FmtRequest類中 parseModel方法進(jìn)行簡(jiǎn)化 ???????????// 將請(qǐng)求當(dāng)中的參數(shù)得到,映射到對(duì)應(yīng)的實(shí)體類中。 ???????????EmployeeModel model =??FmtRequest.parseModel(req,EmployeeModel.class); ???????????return service.insert(model); ????? (6) 實(shí)現(xiàn)員工登錄功能(主要完善業(yè)務(wù)邏輯) <input type="button" value="登錄" class="layui-btn" lay-submit??lay-filter="login" > 這里的login 決定了對(duì)應(yīng)的js代碼怎么實(shí)現(xiàn) <input type="hidden" name="action" value="login" > 這里的login 決定了servlet文件中的分支怎么實(shí)現(xiàn) 1. 創(chuàng)建登錄界面 <%@ page language="java" contentType="text/html; charset=UTF-8" ????pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <!-- <meta charset="UTF-8"> --> <title>登錄頁(yè)面</title> <!-- <link rel="stylesheet"??href="/demo220111/web/base/layui/css/layui.css" > --> <!-- <script type="text/javascript"??src="/demo220111/web/base/layui/layui.js" ></script> --> <!-- 利用包含來簡(jiǎn)化代碼(工具類) --> <%@ include file="/web/header.jsp" %> </head> <body style="background: #f1f1f1"> <div class="window" > ?????<fieldset class="layui-elem-field" style="margin: 10px"> ???????<legend>注冊(cè)</legend> ???????<div class="layui-field-box"> ?????????<form class="layui-form layui-form-pane" > ???????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">賬號(hào)</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="code" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入賬號(hào)"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">密碼</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="password" name="pass"??required??lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入密碼"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="button" value="登錄"??class="layui-btn" lay-submit lay-filter="login" > ?????????????????????????<input type="reset" value="重置"??class="layui-btn layui-btn-primary"> ??????????????????????</div> ??????????????????????<input type="button" value="返回注冊(cè)"??class="layui-btn" οnclick=""> ????????????????</div> <!--?????????????????reg是傳遞給EmployeeServlet 參數(shù) --> ????????????????<input type="hidden" name="action" value="login"??> ?????????</form> ???????</div> ?????</fieldset> ????? </div> <script type="text/javascript"> 調(diào)用servlet中的分支? formSubmit("login","/EmployeeServlet",'text',function(data){//console.log(data) ?????if(data==1){ ???????????layer.msg("登錄成功") ?????}else if(data=="repeat"){ ???????????layer.msg("登錄失敗,賬號(hào)重復(fù)") ?????}else{ ???????????layer.msg("登錄失敗") ?????} }) </script> </body> </html> 2. 在servlet中 建立分支?? 一、 創(chuàng)建login分支 (1) ?@Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????String res = null; ???????????switch (req.getParameter("action")) { ????????????????case "reg": ?????????????????????res = reg(req); ?????????????????????break; ????????????????case "login": ?????????????????????res = login(req); ?????????????????????break; ???????????} ???????????// ajax 請(qǐng)求接收 ???????????// 利用FmtRequest 工具類中write方法進(jìn)行簡(jiǎn)化 ???????????FmtRequest.write(resp.getWriter(), res); ??????????? ?????} 二、生成對(duì)應(yīng)的 login 方法(2) ?????private String login(HttpServletRequest req) { ???????????EmployeeModel model =??FmtRequest.parseModel(req,EmployeeModel.class); ???????????String res = service.login(model); ???????????return null; ?????} 實(shí)現(xiàn)后(7) ?????private String login(HttpServletRequest req) { ???????????EmployeeModel model = FmtRequest.parseModel(req,??EmployeeModel.class); ???????????String res = service.login(model); // res 就是EmployeeServiceImpl中 login方法中的 0、1、2 ???????????if ("1".equals(res)) { ????????????????req.getSession().setAttribute("user",??service.selectModel(model)); ???????????} ???????????return res; ?????} 3.先在IEmployeeService接口中創(chuàng)建用于登錄的方法 參考文章地址:接口和抽象類詳解(接口繼承、實(shí)現(xiàn)接口、抽象類繼承)_lemo_ice的博客-CSDN博客_類繼承接口 用到了抽象類的注入 --暫不確定 自己理解的 小明有添加的功能 可以通過接口實(shí)現(xiàn)小明擁有按人名添加的功能 小紅有添加的功能 可以通過接口實(shí)現(xiàn)小明擁有按id添加的功能 IEmployeeService類(3) String login(EmployeeModel model); 實(shí)現(xiàn)后(6) /** ??????* 登錄功能 ??????* ??????* @param model ??????* @return String 0=賬號(hào)不存在???1=登錄成功???2=密碼錯(cuò)誤 ??????*/ ?????String login(EmployeeModel model); 4.IEmployeeService接口新加了login方法它的實(shí)現(xiàn)類(EmployeeServiceImp)也需要對(duì)添加方法進(jìn)行實(shí)現(xiàn)(重寫) EmployeeServiceImpl類 (4) @Override ?????public String login(EmployeeModel model) { ???????????// TODO Auto-generated method stub ???????????return null; ?????} 實(shí)現(xiàn)后:(5) @Override ?????public String login(EmployeeModel model) { ???????????EmployeeModel mdb = selectModel(model); ???????????if (mdb == null) ????????????????return "0"; ???????????String pass = MD5.encode(model.getPass()); ???????????return mdb.getPass().equals(pass) ? "1" : "2"; ?????} 5. 將密碼MD5加密(此處在service中加密MD5) 將注冊(cè)后加密結(jié)果和登錄時(shí)加密結(jié)果進(jìn)行比較判斷是否正確 注冊(cè)加碼 @Override ?????public String insert(EmployeeModel model) { ???????????// return dao.insert(model) + "";??測(cè)試能否實(shí)現(xiàn)交互 ????? ???????????// 完善業(yè)務(wù)邏輯 ???????????// 在EmployeeModel 創(chuàng)建code構(gòu)造方法 ???????????EmployeeModel m1 = new??EmployeeModel(model.getCode()); ???????????EmployeeModel mdb = dao.selectModel(m1); ???????????if (mdb != null) ????????????????return "repeat"; ???????????model.setPass(MD5.encode(model.getPass())); ???????????return dao.insert(model) + "";?? ?????} 登錄加密 @Override ?????public String login(EmployeeModel model) { ???????????EmployeeModel mdb = selectModel(model); ???????????if (mdb == null) ????????????????return "0"; ???????????String pass = MD5.encode(model.getPass()); ???????????return mdb.getPass().equals(pass) ? "1" : "2"; ?????} 6.在登錄界面添加跳轉(zhuǎn)注冊(cè)界面,在注冊(cè)界面添加跳轉(zhuǎn)登錄界面 注冊(cè)界面跳轉(zhuǎn)登錄界面?? ? html: <input type="button" value="返回登錄" class="layui-btn"??οnclick="toHref()"> javascript function toHref(){ ?????location.href = base.app + "/web/login.jsp"; } 登錄界面跳轉(zhuǎn)注冊(cè)界面 html: <input type="button" value="返回注冊(cè)" class="layui-btn"??οnclick="toHref()"> javascript function toHref(){ ?????location.href = base.app + "/web/reg.jsp"; } 簡(jiǎn)化代碼 在base.js 文件中添加 function toHref(url){ ?????location.href = base.app + url; } 修改注冊(cè)界面代碼 <input type="button" value="返回注冊(cè)" class="layui-btn"??οnclick="toHref('/web/reg.jsp')"> 修改登錄界面代碼 <input type="button" value="返回登錄" class="layui-btn"??οnclick="toHref('/web/login.jsp')"> (7) 實(shí)現(xiàn)主界面功能 1.創(chuàng)建page文件夾(登錄之后的操作 --- 防盜鏈)? ? ? 2.從登錄界面跳轉(zhuǎn) (layui 彈出層) formSubmit("login","/EmployeeServlet",'text',function(data){//console.log(data) ?????if(data == 1){ ???????????layer.msg("登錄成功哦!!!",{time:1000},function(){ ????????????????toHref("/web/page/main.jsp") ???????????}) ?????}else if(data == 2){ ???????????layer.msg("登錄失敗,密碼錯(cuò)誤!!!") ?????}else if(data == 0){ ???????????layer.msg("登錄失敗,賬號(hào)不存在!!!") ?????} }) 3.創(chuàng)建主界面(layui) <%@ page language="java" contentType="text/html; charset=UTF-8" ????pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <%@ include file="/web/header.jsp" %> <meta charset="UTF-8"> <title>主界面</title> </head> <body> <body> <div class="layui-layout layui-layout-admin"> ??<div class="layui-header"> ????<div class="layui-logo layui-hide-xs layui-bg-black">員工績(jī)效管理系統(tǒng)</div> ????<!-- 頭部區(qū)域(可配合layui 已有的水平導(dǎo)航) --> ????<ul class="layui-nav layui-layout-left"> ??????<!-- 移動(dòng)端顯示 --> ??????<li class="layui-nav-item layui-show-xs-inline-block??layui-hide-sm" lay-header-event="menuLeft"> ????????<i class="layui-icon layui-icon-spread-left"></i> ??????</li> ?????? ??????<li class="layui-nav-item layui-hide-xs"><a href="">部門信息</a></li> ??????<li class="layui-nav-item layui-hide-xs"><a href="">員工信息</a></li> ??????<li class="layui-nav-item layui-hide-xs"><a href="">項(xiàng)目信息</a></li> ??????<li class="layui-nav-item"> ????????<a href="javascript:;">績(jī)效信息</a> ????????<dl class="layui-nav-child"> ??????????<dd><a href="">menu 11</a></dd> ??????????<dd><a href="">menu 22</a></dd> ??????????<dd><a href="">menu 33</a></dd> ????????</dl> ??????</li> ????</ul> ????<ul class="layui-nav layui-layout-right"> ??????<li class="layui-nav-item layui-hide??layui-show-md-inline-block"> ????????<a href="javascript:;"> ??????????<img src="<%=app %>/web/base/img/1.jpeg"??class="layui-nav-img"> ????????????用戶 [   ${user.name}   ] ????????</a> ????????<dl class="layui-nav-child"> ??????????<dd><a href="">修改個(gè)人資料</a></dd> ??????????<dd><a href="">修改個(gè)人密碼</a></dd> ??????????<dd><a href="">修改個(gè)人博客</a></dd> ????????</dl> ??????</li> ??????<li class="layui-nav-item" lay-header-event="menuRight"??lay-unselect> ????????<a href="javascript:;"> ??????????<i class="layui-icon layui-icon-more-vertical"></i> ????????</a> ??????</li> ????</ul> ??</div> ?? ??<div class="layui-side layui-bg-black"> ????<div class="layui-side-scroll"> ??????<!-- 左側(cè)導(dǎo)航區(qū)域(可配合layui已有的垂直導(dǎo)航) --> ??????<ul class="layui-nav layui-nav-tree" lay-filter="test"> ????????<li class="layui-nav-item layui-nav-itemed"> ??????????<a class="" href="javascript:;">信息維護(hù)</a> ??????????<dl class="layui-nav-child"> ????????????<dd><a href="javascript:;"??data-url="/web/page/department/list.jsp"??class="site-demo-active">部門信息維護(hù)</a></dd> ????????????<dd><a href="javascript:;"??data-url="/web/page/employee/list.jsp"??class="site-demo-active">員工信息維護(hù)</a></dd> ????????????<dd><a href="javascript:;"??data-url="/web/page/employee/list.jsp"??class="site-demo-active">項(xiàng)目信息維護(hù)</a></dd> ????????????<dd><a href="javascript:;"??data-url="/web/page/department/list.jsp">績(jī)效信息維護(hù)</a></dd> ??????????</dl> ????????</li> ????????<li class="layui-nav-item"> ??????????<a href="javascript:;">menu group 2</a> ??????????<dl class="layui-nav-child"> <!--?????????????實(shí)現(xiàn)打開網(wǎng)頁(yè)顯示主體網(wǎng)頁(yè) --> ????????????<dd><a??href="javascript:openurl('/web/page/department/list.jsp');">list??1</a></dd> ????????????<dd><a href="javascript:;">list 2</a></dd> ????????????<dd><a href="">超鏈接</a></dd> ??????????</dl> ????????</li> ??????</ul> ????</div> ??</div> ?? ??<div class="layui-body"> ????<!-- 內(nèi)容主體區(qū)域 --> ????<iframe name="framA" width="99%" height="97%"></iframe> ??</div> ?? ??<div class="layui-footer"> ????<!-- 底部固定區(qū)域 --> ??????有問題請(qǐng)聯(lián)系我們!0000-0000000 ??</div> </div> <script type="text/javascript"> //JS // 使用 H5(jQuery) 提供的 data-url實(shí)現(xiàn)打開后顯示主體網(wǎng)頁(yè)?????data-url="/web/page/department/list.jsp" $('.site-demo-active').click(function(){ ?????window.open(base.app + $(this).data("url"),"framA") }) $('.site-demo-active').click() // 默認(rèn)打開第一個(gè) // <!--???????????????實(shí)現(xiàn)打開網(wǎng)頁(yè)顯示主體網(wǎng)頁(yè) --> function openurl(url){ ?????window.open(base.app + url,'framA') } </script> </body> </body> </html> 4.主界面退出操作 (layui) (1).mian.jsp html <li class="layui-nav-item"> ???????????<a href="javascript:toLogout()">退出</a> </li> js function toLogout(){ ?????layerConfirm(function(){ ???????????toHref('/EmployeeServlet?action=logout') ?????},"您確定要離開了嗎?") } base.js function layerConfirm(func,title){ ?????layer.confirm(title ? title : "確定要進(jìn)行該操作?",{ ???????????icon:3, ???????????title:"提示" ?????},func) } (2).?EmployeeServlet 實(shí)現(xiàn)分支 ?????case "logout": ????????????????req.getSession().removeAttribute("user"); ????????????????resp.sendRedirect(req.getContextPath()+"/web/login.jsp"); ????????????????break; 5.防盜鏈的功能 (UserFilter類來實(shí)現(xiàn),用戶退出后或未登錄情況下,無法訪問page文件下的界面) @WebFilter(urlPatterns =??{"/web/page/*","/EmployeeServlet","/DepartmentServlet","/ProjectServlet","/ScoreServlet"}) public class UserFilter implements Filter { // 防盜鏈??確認(rèn)當(dāng)前地址欄用戶是否登錄 ?????@Override ?????public void doFilter(ServletRequest request,??ServletResponse response, FilterChain chain) ????????????????throws IOException, ServletException { ???????????HttpServletRequest req = (HttpServletRequest)??request; ???????????String action = req.getParameter("action"); ???????????if("reg".equals(action)||"login".equals(action)) { //??將 注冊(cè)請(qǐng)求和登錄請(qǐng)求單獨(dú)放行 ????????????????chain.doFilter(request, response); ????????????????return; ???????????} ????????????Object user = req.getSession().getAttribute("user"); ????????????if (user == null) { ????????????????req.getRequestDispatcher("/web/login.jsp").forward(request,??response);// 進(jìn)行頁(yè)面跳轉(zhuǎn)(請(qǐng)求轉(zhuǎn)發(fā)) ???????????} else ????????????????chain.doFilter(request, response); ?????} ????? } 6.? 右側(cè)邊欄 操作展示功能 html <li class="layui-nav-item" lay-header-event="menuRight"??lay-unselect> ????????<a href="javascript:openRight();">請(qǐng)求處理 ??????????<i class="layui-icon layui-icon-more-vertical"></i> ????????</a>? </li> js function openRight(){ ?????layer.open({ ???????????type:1 ???????????,content:'<div style="padding:15px;">處理右側(cè)面板的操作</div>' ???????????,area:['260px','100%'] ???????????,offset:'rt'//右上角 ???????????,anim:5 ???????????,shadeClose:true ?????}); } 7.主界面主體查詢操作部分(layui 面板) (1)html 操作代碼 面板上半部分 html: <div class="layui-collapse"> ??<div class="layui-colla-item"> ????<h2 class="layui-colla-title" >部門信息--查詢條件</h2> ????<div class="layui-colla-content layui-show"> ???????? ?????????<fieldset class="layui-elem-field layui-field-title"??style="margin: 10px"> ?????????????<legend>部門信息--查詢條件</legend> ?????????????<div class="layui-field-box"> ???????????????<form class="layui-form" > ?????????????????<div class="layui-form-item"> ????????????????????????????<label class="layui-form-label">編號(hào)</label> ???????????????????????<div class="layui-input-inline"> ? ? ? ? ? ? ? ? ? ? ? ? ? ?<input type="text" name="code" placeholder="請(qǐng)輸入編號(hào)"??autocomplete="off" class="layui-input "> ????????????????????????????</div> ????????????????????????????<label class="layui-form-label">名稱</label> ???????????????????????<div class="layui-input-inline"> ? ? ? ? ? ? ? ? ? ? ? ? ? ?<input type="text" name="name"??placeholder="請(qǐng)輸入名稱" autocomplete="off" class="layui-input "> ????????????????????????????</div> ????????????????????????????? ????????????????????????????<span> ????????????????????????????????<input type="button" value="查詢" class="layui-btn layui-btn-sm" lay-submit lay-filter="search"??> ???????????????????????????????<input type="reset" value="重置"??class="layui-btn layui-btn-sm layui-btn-primary"> ????????????????????????????????<input type="button" value="添加" class="layui-btn layui-btn-sm" lay-submit lay-filter="search"??> ????????????????????????????</span> ??????????????????????</div> <!--?????????????????reg是傳遞給EmployeeServlet 參數(shù) --> <!--??????????????????<input type="hidden" name="action"??value="login" > --> ???????????????</form> ?????????????</div> ???????????</fieldset> ????</div> ??</div> </div> 面板下半部分 HTML: <!-- 面板下半部分 --> <table class="layui-table"> <!-- colgroup限制寬度 --> ?????<colgroup> ???????????<col width="10&" ><col width="15%" ><col width="15%"??><col width="20%" ><col width="40%" > ?????</colgroup> ?????<thead> ???????????<tr> ???????????<th>序號(hào)</th><th>編號(hào)</th><th>名稱</th><th>電話</th><th>操作</th> ???????????</tr> ?????</thead> ?????<tbody> ??????????? ?????</tbody> ????? </table> <script type="text/javascript"> // base文件要先在base.js文件中定義出來 // var element =layui.element; // 折疊面板不好用 記得用渲染 // element.render(); </script> (2)JavaScript操作代碼 script 實(shí)現(xiàn) 部分代碼 解釋: //function formSubmit(event,url,dataType,func){ //???//console.log(event) 調(diào)試 //???form.on("submit(" +event+")",function(data){ //???//???console.log(data.field)調(diào)試 //?????????ajax(url,data.field,dataType,func) //???}) //} // 調(diào)用了base.js 文件??取出 查詢行中( <input type="button"??value="查詢" class="layui-btn layui-btn-sm" lay-submit??lay-filter="search" >)的search //??此處 search是網(wǎng)頁(yè)查詢行的方法? ? //? EmployeeServlet是實(shí)現(xiàn)業(yè)務(wù)邏輯的類 //??json??是請(qǐng)求傳遞的參數(shù)類型 以json形式傳遞 formSubmit('search','/EmployeeServlet','json',function(data){}) formSubmit 類比base.js 文件 (3)EmployeeServlet中操作代碼 one:先繼承HttpServlet public class EmployeeServlet extends HttpServlet {} two:定義程序 序列化ID public class EmployeeServlet extends HttpServlet { ?????private static final long serialVersionUID = 1L; } three: 創(chuàng)建表單提交數(shù)據(jù)到服務(wù)器的方式 public class EmployeeServlet extends HttpServlet { ?????private static final long serialVersionUID = 1L; (4) EmployeeServlet中建立業(yè)務(wù)邏輯分支 @Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????String res = null; ???????????switch (req.getParameter("action")) { ???????????case "list": ????????????????res = list(req); ????????????????break; ???????????default: ????????????????break; ???????????} ?????} list 通過隱藏域(hidden) 來傳遞到EmployeeServlet中? 通過EmployeeServlet 中 'list' 分支來實(shí)現(xiàn)表格中數(shù)據(jù)的加載 lsit.jsp文件 <input type="hidden" name="action" value="list"> 這樣就可以實(shí)現(xiàn)list分支的方法了 常用套路: 1接受請(qǐng)求、 2獲取數(shù)據(jù)、 3封裝對(duì)象、 4調(diào)用方法、 5返回結(jié)果 1接受請(qǐng)求、 ?String conextPath = req.getContextPath(); ?String path1 = "/web/success.jsp"; 2獲取數(shù)據(jù)、 DepartmentModel model2 = new DepartmentModel(); ? ? ?req.getParameter("code"); ? ? ?req.getParameter("name"); ?? ?可以利用工具類簡(jiǎn)化 ? // 將所有請(qǐng)求的參數(shù)映射到實(shí)體類中 ? ? ?DepartmentModel model =FmtRequest.parseModel(req,??DepartmentModel.class);???? ? ? ? ? ? return null; 1和2 利用工具類簡(jiǎn)化后: ?????private String list(HttpServletRequest req) { ? ? ? ?// 將所有請(qǐng)求的參數(shù)映射到實(shí)體類中 ? ? ? ?DepartmentModel model =FmtRequest.parseModel(req,??DepartmentModel.class);???? ? ? ? ? ?return null; ?????} 3封裝對(duì)象(1)、 ?? ?在接口(IDepartmentService)中定義方法(至少有這五個(gè)) ?public interface IDepartmentService { ?????String insert(DepartmentModel model); ?????String delete(DepartmentModel model); ?????String update(DepartmentModel model); ?????List<DepartmentModel> selectList(DepartmentModel model); ?????DepartmentModel selectModel(DepartmentModel model); } 3封裝對(duì)象(2)、 ? ? 在實(shí)現(xiàn)類中(EmployeeServiceImpl)重寫方法 ,根據(jù)對(duì)應(yīng)的方法去數(shù)據(jù)庫(kù)里查找 ?? ??? ?初始版本 public class DepartmentServiceImpl??implements??IDepartmentService{ ?????private IDepartmentDao dao2??= new Department2DaoImpl(); ????? ????? ?????@Override ?????public String insert(DepartmentModel model) { ???????????return dao2.Insert(model)+""; ?????} ?????@Override ?????public String delete(DepartmentModel model) { ???????????return dao2.delete(model)+""; ?????} ?????@Override ?????public String update(DepartmentModel model) { ???????????return dao2.updateActive(model)+""; ?????} ?????@Override ?????public List<DepartmentModel> selectList(DepartmentModel??model) { ???????????return dao2.selectList(model); ?????} ?????@Override ?????public DepartmentModel selectModel(DepartmentModel model)??{ ???????????return dao2.selectModel(model); ?????} ????? ? ? ?簡(jiǎn)單實(shí)現(xiàn)查詢方法 ?? ?@Override ?????public List<DepartmentModel> selectList(DepartmentModel??model) { ???????????String code = model.getCode(); ???????????model.setCode(code == null ? "%" : "%" + code + "%"); ???????????String name = model.getName(); ???????????model.setName(name == null ? "%" : "%" + name + "%"); ???????????return dao2.selectList(model); ?????} ?? ? 4調(diào)用方法(通過調(diào)用IDepartmentService接口來調(diào)用實(shí)現(xiàn)方法) ?? ?List<DepartmentModel> selectList(DepartmentModel model); 5返回結(jié)果 ?? ?private String list(HttpServletRequest req) { ? ? ? ? // 將所有請(qǐng)求的參數(shù)映射到實(shí)體類中 ? ? ? ? DepartmentModel model =FmtRequest.parseModel(req,??DepartmentModel.class);???? ? ? ? ? // service.selectList(model)??返回的是集合 ????????List<DepartmentModel> list =??service.selectList(model); ? ? ? ? return null; ?????} ? ? 簡(jiǎn)化后: ?? ?private List<DepartmentModel> list(HttpServletRequest req) { ??? ? // 將所有請(qǐng)求的參數(shù)映射到實(shí)體類中 ? ? ? ? ?DepartmentModel model =FmtRequest.parseModel(req,??DepartmentModel.class);???? ? ? ? ? ?// service.selectList(model)??返回的是集合 ? ? ? ? ?return service.selectList(model); ?????} ?? ? ? ? 這里由于返回的是集合 就不能用字符串接收了 就需要 將doPost中的String類型的res 修改為Object型來接收(DepartmentServlet文件下) ?? ??? @Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????Object res = null; ???????????switch (req.getParameter("action")) { ???????????case "list": ????????????????res = list(req); ????????????????break; ???????????default: ????????????????break; ???????????} ?? ??? ??? ?FmtRequest.write(resp.getWriter(), res);? ?? ??? ??? ?// 難點(diǎn):這里重寫了FmtRequest工具類中的write方法 ? ? ?} 至此完成一個(gè) 從 網(wǎng)頁(yè)到 java 再?gòu)膉ava到數(shù)據(jù)庫(kù)? 再數(shù)據(jù)庫(kù)返回到j(luò)ava 最后從Java(傳遞)展示在網(wǎng)頁(yè)上 FmtRequest工具類中的write方法重寫代碼為: public static void write(Writer wr,Object val) { ???????????if (val instanceof Collection<?>) { // 判斷對(duì)象是否為線性集合 ????????????????write(wr, new??JSONArray((Collection<?>)val).toString()); ????????????????//傳進(jìn)去的線性集合,在傳過來的時(shí)候進(jìn)行向下轉(zhuǎn)型(var 是Object類型 最大了), ????????????????//轉(zhuǎn)型完之后傳遞到JSONArray當(dāng)中在調(diào)用toString方法得到j(luò)son格式的字符串, ????????????????//將json格式的字符串再傳遞給write方法,再寫回給網(wǎng)頁(yè)。 ???????????} else if(val instanceof String){ // 判斷對(duì)象是否為字符串 ????????????????write(wr, val.toString()); ???????????}else if (val instanceof Map<?, ?>) { // 判斷對(duì)象是否為map集合 ????????????????write(wr, new??JSONObject((Map<?,?>)val).toString()); ???????????}else {// 判斷是否為(其它對(duì)象)實(shí)體類 ????????????????write(wr, new JSONObject(val).toString()); ???????????} ??????????? ?????} (5) 完善javascript 功能 1 . base.js 文件添加 var laytpl = layui.laytpl; list.js文件 element.render(); formSubmit('search','/DepartmentServlet','json',function(data){ //????console.log(data) ??????var html = ""; ??????var tpl = $("#tradd").html(); ?? ?? ? $.方法? 別忘了加 $. ??????$.each(data , function(i,dom){ ????????????var d =??{id:i,code:dom.code,name:dom.name,tel:dom.tel} ????????????html += laytpl(tpl).render(d); ??????}) ??????$("tbody").html(html); }) 2.(只包含紫色部分) //? 實(shí)現(xiàn)一開的主體界面 不用點(diǎn)查詢 就可以顯示數(shù)據(jù),為后續(xù)的添加操作和 刪除操作奠基(相當(dāng)于刷新吧) function refresh(){ ?????$("input[value='重置' ]").click(); ?????$("input[value='查詢' ]").click(); }refresh(); </script> <!-- 定義模板引擎 --> <script type="text/html" id="tradd"> <tr> ?????<td>{{d.id}}</td><td>{{d.code}}</td><td>{{d.name}}</td><td>{{d.tel}}</td> ?????<td> ? ? ? ? ? ?<a href="javascript:" class="layui-btn layui-btn-sm" >刪除</a> ???????????<a href="javascript:" class="layui-btn layui-btn-sm" >修改</a> ?????</td> </tr> </script> 8.主界面主體添加操作部分(彈出層) ? ? ? ? 添加(1)在base.js 定義彈出層 方法(主要測(cè)試能否彈出 ,一步一步,慢慢來) // layui 彈出層 function layerOpen(url , end){ ?????layer.open({ ???????????type : 2,??????????????????????????????????// 彈出窗口的形式 ???????????content : base.app + url,?????????????? ???// 彈出窗口的內(nèi)容(路徑,將路徑上的網(wǎng)頁(yè)展示在彈出窗口中) ???????????area : ['650px','450px'],????????????? ? ??// 彈出窗口的大小尺寸 ???????????fixed : false,?????????????????????????????// 彈出窗口的 固定(不可拖動(dòng)) ???????????maxmin : true,?????????????????????????????// 彈出窗口的最大化 ???????????shade : 0.3,???????????????????????????????// 彈出窗口的陰影 ???????????closeBtn : 1,??????????????????????????????// 彈出窗口的關(guān)閉按鈕 ???????????shadeClose : false,????????????????????????// 點(diǎn)擊遮罩是否關(guān)閉 ???????????success : function(layero,index){? ? ? ? ? //??成功打開后要調(diào)用的方法 ???????????????? ???????????}, ???????????end : end??????????????????????????????????// 關(guān)閉后要調(diào)用的方法 ???????????????? ??????????? ?????}) } 添加 ??(2)在list.jsp 文件中 添加 靜態(tài)事件綁定(html中),并且創(chuàng)建add.jap添加跳轉(zhuǎn)后的頁(yè)面 ?? ?<input type="button" value="添加" class="layui-btn layui-btn-sm"??οnclick="openAdd()" > 添加? ?(3)在list.jsp 文件中 給添加按鈕創(chuàng)建跳轉(zhuǎn)方法? // 實(shí)現(xiàn) 一開的主體界面 不用點(diǎn)查詢 就可以顯示數(shù)據(jù)???為后續(xù)的添加操作和??刪除操作奠基(相當(dāng)于刷新吧) function refresh(){ ?????$("input[value='重置' ]").click(); ?????$("input[value='查詢' ]").click(); }refresh(); // 定義添加方法 function openAdd(){ ?????// 測(cè)試添加按鈕能否彈出 ?????// layer.msg(123) ?????// 調(diào)用layerOpen方法 ?????layerOpen("/web/page/department/add.jsp" , refresh); } 關(guān)閉?(1) 在base.js文件中 定義關(guān)閉方法 // 彈出層關(guān)閉 function layerClose(){? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 當(dāng)你在iframe頁(yè)面關(guān)閉自身時(shí) ?????var index = parent.layer.getFrameIndex(window.name); // 先得到當(dāng)前iframe層的索引 ?????parent.layer.close(index);? ? ? ? ? ? ? ? ? ? ? ? ? ?// 再執(zhí)行關(guān)閉 }?? ? 關(guān)閉? (2) 在add.js文件中添加關(guān)閉的靜態(tài)事件綁定 <input type="button" value="關(guān)閉" class="layui-btn"??οnclick="layerClose()" > ?實(shí)現(xiàn)數(shù)據(jù)添加操作? ??(1)html文件 的操作 首先完善 add.jsp 頁(yè)面的html代碼 <%@ page language="java" contentType="text/html; charset=UTF-8" ????pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <%@ include file="/web/header.jsp" %> <meta charset="UTF-8"> <title>部門添加界面</title> </head> <body> <fieldset class="layui-elem-field" style="margin: 10px"> ???????<legend>部門添加界面</legend> ???????<div class="layui-field-box"> ?????????<form class="layui-form layui-form-pane" > ???????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">編號(hào)</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="code" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入編號(hào)"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">名稱</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="name" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入名稱"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">電話</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="tel" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入聯(lián)系方式"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ?????????????????<div class="layui-input-black"> ?? ??? ??? ??? ??? ?? ? // 添加動(dòng)態(tài)事件綁定? ?1 ?????????????????????<input type="button" value="確定"??class="layui-btn" lay-submit lay-filter="add" > ?????????????????????????<input type="reset" value="重置"??class="layui-btn layui-btn-primary"> ?????????????????????????<input type="button" value="關(guān)閉"??class="layui-btn" οnclick="layerClose()" > ??????????????????????</div> ????????????????</div> ????????????????<!-- reg是傳遞給EmployeeServlet 參數(shù) --> ????????????????<input type="hidden" name="action" value="reg" > ?????????</form> ???????</div> ?????</fieldset> ????? ????? <script type="text/javascript"> // 2 實(shí)現(xiàn)添加操作的JavaScript代碼 formSubmit("add","/DepartmentServlet",'text',function(data){console.log(data) ?????if(data==1){ ???????????layer.msg("添加成功",layerClose) ?????}else if(data=="repeat"){ ???????????layer.msg("添加失敗,編號(hào)重復(fù)") ?????}else{ ???????????layer.msg("添加失敗") ?????} }) </script>?? </body> </html> ?? ??? ??實(shí)現(xiàn)數(shù)據(jù)添加操作? ??(2) 在DepartmentServlet類中 定義add 分支和 創(chuàng)建add方法 ? 1. 定義 add 分支? ??@Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????Object res = null; ???????????switch (req.getParameter("action")) { ???????????case "list": ????????????????res = list(req); ????????????????break; ???????????case "add": ????????????????res = add(req); ????????????????break; ???????????????? ???????????default: ????????????????break; ???????????} ???????????FmtRequest.write(resp.getWriter(), res); ?????} 2. 創(chuàng)建 add 方法 ? ? ??/** ??????* 定義上述分支中的 add方法 ??????* @param req ??????* @return ??????*/ ?????private String add(HttpServletRequest req) { ???????????DepartmentModel model =FmtRequest.parseModel(req,??DepartmentModel.class);???? ???????????return service.insert(model); ?????} ?實(shí)現(xiàn)數(shù)據(jù)添加操作? ??(3) 去IDepartmentService接口中?定義insert方法 ? ? ? String insert(DepartmentModel model); ?實(shí)現(xiàn)數(shù)據(jù)添加操作? ??(4)在DepartmentServiceImpl中? 通過接口(IDepartmentService) 重寫添加的業(yè)務(wù)邏輯 DepartmentServiceImpl類中 1. 重寫insert 業(yè)務(wù)邏輯 @Override ?????public String insert(DepartmentModel model) { ?? ??? ?? ? // 2 和 3 只是為了在這里查詢判斷時(shí)更嚴(yán)謹(jǐn) ???????????if (selectModel(model) == null) { ????????????????Integer res = dao2.Insert(model); ????????????????return res == null ? null : res.toString(); ???????????} ???????????return "repeat"; ?????} 2.為了更嚴(yán)謹(jǐn)一點(diǎn) 先在?DepartmentModel? 添加code 無參構(gòu)造方法和有參構(gòu)造方法 public DepartmentModel() {// 無參構(gòu)造方法??? ?????} ?????public DepartmentModel(String code) { // 有參構(gòu)造方法 ???????????this.code = code; ?????} 3. 在調(diào)用selectModel 方法查詢判斷 code字段 @Override ?????public DepartmentModel selectModel(DepartmentModel model)??{ ???????????return dao2.selectModel(new??DepartmentModel(model.getCode())); ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作? ?(1)在 list.jsp中 實(shí)現(xiàn)刪除的html代碼 <script type="text/html" id="tradd"> <tr> ?????<td>{{d.id}}</td><td>{{d.code}}</td><td>{{d.name}}</td><td>{{d.tel}}</td> ?????<td> ???????????<a href="javascript:del('{{d.code}}')"??class="layui-btn layui-btn-sm layui-btn-danger " > ????????????????<i class='layui-icon layui-icon-delete'></i> ???????????</a> ???????????<a href="javascript:" class="layui-btn layui-btn-sm"??>修改</a> ?????</td> </tr> </script> 實(shí)現(xiàn)數(shù)據(jù)刪除操作? ? ?(2)在 list.jsp中 實(shí)現(xiàn)刪除的javascrip代碼 // 定義刪除方法 function del(code){ ?????// 測(cè)試方法是否可用? 1 ?????//layer.msg(code) ?????layerConfirm(function(index){ ???????????// 測(cè)試layerConfirm提示是否可用? 2 ???????????// layer.msg(code); ?? ??? ??? // 通過調(diào)用 base.js 文件的ajax請(qǐng)求 實(shí)現(xiàn)刪除? ?注意: code是字段? del 是DepartmentServlet的 del 分支 ? ? ? ? ? ?ajax("/DepartmentServlet",{code:code,action:'del'}) ?????}) } // 完善刪除方法 function del(code){ ?????// 測(cè)試方法是否可用 ?????//layer.msg(code) ?????layerConfirm(function(index){ ???????????// 測(cè)試layerConfirm提示是否可用 ???????????// layer.msg(code); ???????????ajax("/DepartmentServlet",{code:code,action:'del'},"text",function(data){ ? ? ? ? ? ? ? ? // 測(cè)試ajax 調(diào)用是否可行 ?? ??? ??? ?? ? //?console.log(data) ????????????????if(data == 1){ ?????????????????????layer.msg("刪除成功",refresh) ????????????????}else{ ?????????????????????layer.msg("刪除失敗") ????????????????}???? ???????????}) ?????}) } 實(shí)現(xiàn)數(shù)據(jù)刪除操作? (3)在DepartmentServlet中創(chuàng)建del 分支 并創(chuàng)建對(duì)應(yīng)的del方法 DepartmentServlet?類中 1. 創(chuàng)建del 分支 ?????@Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????Object res = null; ???????????switch (req.getParameter("action")) { ???????????case "list": ????????????????res = list(req); ????????????????break; ???????????case "add": ????????????????res = add(req); ????????????????break; ???????????case "del": ????????????????res = del(req); ????????????????break; ???????????default: ????????????????break; ???????????} ???????????FmtRequest.write(resp.getWriter(), res); ?????} 2. 創(chuàng)建del 方法 private Object del(HttpServletRequest req) { ???????????return service.delete(FmtRequest.parseModel(req,??DepartmentModel.class)); ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作?? ??(4)在IDepartmentService接口中創(chuàng)建del方法 public interface IDepartmentService { ?????String insert(DepartmentModel model); ?????String delete(DepartmentModel model); ?????String update(DepartmentModel model); ?????List<DepartmentModel> selectList(DepartmentModel model); ?????DepartmentModel selectModel(DepartmentModel model); } 實(shí)現(xiàn)數(shù)據(jù)刪除操作??(5)在IDepartmentServiceImpl中重寫del方法 @Override ?????public String delete(DepartmentModel model) { ???????????Integer res = dao2.delete(model); ???????????return res == null ? null : res.toString(); ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作??(6)在IDepartment2DaoImpl中重寫del方法 ?????@Override ?????public Integer delete(DepartmentModel model) { ???????????StringBuffer sql = new StringBuffer(" delete from??department "); ???????????// 通過使用工具類JDBCUtil 簡(jiǎn)化 根據(jù)查詢條件的刪除操作代碼 ???????????List<Object> values = appendWhere(sql,model); // 這里調(diào)用了appendWhere去判斷是否對(duì)應(yīng) ???????????return JDBCUtil1.update(sql.toString(), values); ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作??(7)在IDepartment2DaoImpl中調(diào)用了appendWhere去判斷是否對(duì)應(yīng) private List<Object> appendWhere(StringBuffer sql,??DepartmentModel model) { ???????????sql.append("where 1=1"); ???????????List<Object> values = new ArrayList<>(); ???????????try { ????????????????String code = model.getCode(); ????????????????if (!FmtEmpty.isEmpty(code)) { ?????????????????????sql.append(" and code like ? "); ?????????????????????values.add(code); ????????????????} ????????????????String name = model.getName(); ????????????????if (!FmtEmpty.isEmpty(name)) { ?????????????????????sql.append(" and name like ? "); ?????????????????????values.add(name); ????????????????} ????????????????String tel = model.getTel(); ????????????????if (!FmtEmpty.isEmpty(tel)) { ?????????????????????sql.append(" and tel like ? "); ?????????????????????values.add(tel); ????????????????} ???????????} catch (Exception e) { ????????????????e.printStackTrace(); ???????????} ???????????return values; ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作??(8)將結(jié)果返回到DepartmentServlet中 的del 方法中去 在回到 分支里面去判斷 private Object del(HttpServletRequest req) { ???????????return service.delete(FmtRequest.parseModel(req,??DepartmentModel.class)); ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作??(9)如果 res = del(req)? 則在數(shù)據(jù)庫(kù)中刪除成功? @Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????Object res = null; ???????????switch (req.getParameter("action")) { ???????????case "list": ????????????????res = list(req); ????????????????break; ???????????case "add": ????????????????res = add(req); ????????????????break; ???????????case "del": ????????????????res = del(req); ????????????????break; ???????????default: ????????????????break; ???????????} ???????????FmtRequest.write(resp.getWriter(), res); ?????} 實(shí)現(xiàn)數(shù)據(jù)刪除操作? (10)返回到 list.jsp文件中 執(zhí)行if判斷做出相應(yīng)的提示 // 定義刪除方法 function del(code){ ?????// 測(cè)試方法是否可用 ?????//layer.msg(code) ?????layerConfirm(function(index){ ???????????// 測(cè)試layerConfirm提示是否可用 ???????????// layer.msg(code); ???????????ajax("/DepartmentServlet",{code:code,action:'del'},"text",function(data){ ????????????????console.log(data) ????????????????if(data == 1){ ?????????????????????layer.msg("刪除成功",refresh) ????????????????}else{ ?????????????????????layer.msg("刪除失敗") ????????????????}???? ???????????}) ?????}) } ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(前半部分 回顯)? (1)?在list.jsp 創(chuàng)建修改頁(yè)面的跳轉(zhuǎn) 方法 1.創(chuàng)建修改按鈕 <script type="text/html" id="tradd"> <tr> ?????<td>{{d.id}}</td><td>{{d.code}}</td><td>{{d.name}}</td><td>{{d.tel}}</td> ?????<td> ???????????<a href="javascript:del('{{d.code}}')"??class="layui-btn layui-btn-sm layui-btn-danger " > ????????????????<i class='layui-icon layui-icon-delete'></i> ???????????</a> ??????????<a href="javascript:openUpd('{{d.code}}')" class="layui-btn??layui-btn-sm" >修改</a> ?????</td> </tr> </script> 2. // 定義修改方法 function openUpd(code){ ?????// 通過修改頁(yè)面的路徑 傳參數(shù) ?????layerOpen("/web/page/department/upd.jsp?code="+code ,??refresh); } ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(前半部分 回顯)?? (2)??創(chuàng)建修改頁(yè)面 upd.jsp? 獲取參數(shù) <%@ page language="java" contentType="text/html; charset=UTF-8" ????pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <%@ include file="/web/header.jsp" %> <meta charset="UTF-8"> <title>修改頁(yè)面</title> </head> <body> <fieldset class="layui-elem-field" style="margin: 10px"> ???????<legend>部門修改界面</legend> ???????<div class="layui-field-box"> ?????????<form class="layui-form layui-form-pane" > ???????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">編號(hào)</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="code" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入編號(hào)"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">名稱</label> ?????????????????<div class="layui-input-inline"> ?? ??? ??? ?? ??? ??? ??<!-- readonly??不可修改 --> ?????????????????????<input type="text" name="code" required???lay-verify="required" readonly ???????????????????????????placeholder="請(qǐng)輸入編號(hào)"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ??????????????????????<label class="layui-form-label">電話</label> ?????????????????<div class="layui-input-inline"> ?????????????????????<input type="text" name="tel" required???lay-verify="required" ???????????????????????????placeholder="請(qǐng)輸入聯(lián)系方式"??autocomplete="off" class="layui-input"> ??????????????????????</div> ????????????????</div> ????????????????<div class="layui-form-item"> ?????????????????<div class="layui-input-black"> ?????????????????????<input type="button" value="確定"??class="layui-btn" lay-submit lay-filter="get" > ?????????????????????????<input type="reset" value="重置"??class="layui-btn layui-btn-primary"> ?????????????????????????<input type="button" value="關(guān)閉"??class="layui-btn" οnclick="layerClose()" > ??????????????????????</div> ????????????????</div> ????????????????<!-- reg是傳遞給EmployeeServlet 參數(shù) --> ????????????????<input type="hidden" name="action" value="get" > ?????????</form> ???????</div> ?????</fieldset> <script type="text/javascript"> // 得到傳入的參數(shù) code var code = '<%=request.getParameter("code")%>'; // layer.msg(code) function init(){ ?????ajax("/DepartmentServlet",{code:code,action:'get'},'json',function(data){ ???????????console.data(data) ??????????? ?????}) } </script> </body> </html> ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(前半部分 回顯)?? (3)??創(chuàng)建DepartmentServlet 類中的 get 分支 和 get方法 1. 創(chuàng)建get 分支 @Override ?????protected void doPost(HttpServletRequest req,??HttpServletResponse resp) throws ServletException, IOException { ???????????Object res = null; ???????????switch (req.getParameter("action")) { ???????????case "list": ????????????????res = list(req); ????????????????break; ???????????case "add": ????????????????res = add(req); ????????????????break; ???????????case "del": ????????????????res = del(req); ????????????????break; ???????????case "get": ????????????????res = get(req); ????????????????break; ???????????default: ????????????????break; ???????????} ???????????FmtRequest.write(resp.getWriter(), res); ?????} 2.創(chuàng)建get 方法 ?? ??/** ??????* 定義上述分支中的 get方法 ??????* @param req ??????* @return ??????*/ ?????private DepartmentModel get(HttpServletRequest req) { ???????????return service.selectModel(FmtRequest.parseModel(req,??DepartmentModel.class)); ??????????? ?????} ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(前半部分 回顯)?? (3)? 通過IDepartmentService接口調(diào)用DepartmentServiceImpl實(shí)現(xiàn)的方法 IDepartmentService接口 ?????DepartmentModel selectModel(DepartmentModel model); DepartmentServiceImpl實(shí)現(xiàn)類 ?? ?@Override ??????? ?public DepartmentModel selectModel(DepartmentModel model)??{ ????????????? ?return dao2.selectModel(new??DepartmentModel(model.getCode())); ?????? ??} 再次通過dao2在調(diào)用Department2DaoImpl中的selectModel重寫方法 ??????????@Override ?????public DepartmentModel selectModel(DepartmentModel model)??{ ???????????StringBuffer sql = new StringBuffer("select??id,code,name,tel from "); ???????????sql.append(table).append(" where code =? "); ???????????// 通過使用工具類JDBCUtil 簡(jiǎn)化根據(jù)主鍵查詢一條記錄的代碼 ???????????List<Object> values = Arrays.asList(model.getCode()); ???????????return JDBCUtil1.queryModel(sql.toString(), values,??DepartmentModel.class); ?????} ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(前半部分 回顯)?? (4) 將結(jié)果返回到 DepartmentServlet中 先返回給? get 方法 再返回給 get分支判斷 是否 相等? 如果相等 將數(shù)據(jù)以json的形式傳回到upd.jsp網(wǎng)頁(yè)中 執(zhí)行回顯方法 ajax請(qǐng)求?實(shí)現(xiàn)數(shù)據(jù)修改操作(前半部分 回顯)?? (5) 如果分支判斷相等 ,就將數(shù)據(jù)以json的形式傳回網(wǎng)頁(yè) 執(zhí)行回顯方法? 完成操作 // 得到傳入的參數(shù) code var code = '<%=request.getParameter("code")%>'; // 測(cè)試點(diǎn)擊修改是否能得到相應(yīng)的code // layer.msg(code) function init(){ ?????ajax("/DepartmentServlet",{code:code,action:'get'},'json',function(data){ ???????????console.log(data) ???????????$("input[name='code']").val(data.code) ???????????$("input[name='name']").val(data.name) ???????????$("input[name='tel']").val(data.tel) ???????????form.val("formA",{code:data.code,name:data.name,tel:data.tel}); ?????//???form.val("formA",data); ?????}) } // 不調(diào)用 init方法 它怎么執(zhí)行!!! init(); ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(后半部分 更新)? ? ? ?(6) upd.jsp 頁(yè)面創(chuàng)新 upd 方法 formSubmit("upd","/DepartmentServlet",'text',function(data){//console.log(data) ?????if(data==1){ ???????????layer.msg("修改成功",layerClose) ?????}else{ ???????????layer.msg("修改失敗") ?????} }) ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(后半部分 更新)? ? ? ?(7) 去DepartmentServlet 文件創(chuàng)建upd 分支 和 upd 執(zhí)行方法 1. upd 分支?? ? ??? ?case "upd": ? ? ? ? ? ? res = upd(req); ? ? ? ? ? ? break;????? 2. upd 方法 ? ? ?/** ??????* 定義上述分支中的 upd方法??修改的前半部分???更新 ??????* @param req ??????* @return ??????*/ ?????private Object upd(HttpServletRequest req) { ???????????return service.update(FmtRequest.parseModel(req,??DepartmentModel.class)); ?????} ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(后半部分 更新)? ? ? ?(8) 通過 接口 和實(shí)現(xiàn)類 執(zhí)行 update 方法 1. 先調(diào)用IDepartmentService接口 public interface IDepartmentService { ?????String update(DepartmentModel model); } 2. 在調(diào)用DepartmentServiceImpl實(shí)現(xiàn)類重寫方法 ?????@Override ?????public String update(DepartmentModel model) { ???????????return dao2.updateActive(model)+""; ?????} 3. 調(diào)用DepartmentServiceImpl? 中的updateActive方法(先調(diào)用IDepartmentDao接口) public interface IDepartmentDao { ?? ?Integer updateActive(DepartmentModel model); } 4. 調(diào)用DepartmentServiceImpl? 中的updateActive方法(再調(diào)用Department2DaoImpl實(shí)現(xiàn)類中的updateActive 方法) public class Department2DaoImpl implements IDepartmentDao { ?????@Override ?????public Integer updateActive(DepartmentModel model) { ???????????StringBuffer sql = new StringBuffer("update "); // 字符串的拼接一定要會(huì) ???????????sql.append(table).append(" set id = id "); ???????????// 通過使用工具類JDBCUtil 簡(jiǎn)化根據(jù)主鍵修改部分其他字段操作的代碼 ???????????List<Object> values = appendSet(sql, model); // 這里又調(diào)用了appendSet方法執(zhí)行后返回 ???????????return JDBCUtil1.update(sql.toString(), values); ?????} ?? ?private List<Object> appendSet(StringBuffer sql, DepartmentModel??model) { // 定義字符串拼接方法 ???????????List<Object> values = new ArrayList<Object>(); ???????????String name = model.getName(); ???????????if (FmtEmpty.isEmpty(name)) { // 判斷有沒有對(duì)象以及是否為空 str == null || str.trim().isEmpty() ????????????????sql.append(",name=? ");// 字符串的拼接一定要會(huì) ????????????????values.add(name); ???????????} ???????????String tel = model.getTel(); ???????????if (tel != null) { ????????????????sql.append(" ,tel=? ");// 字符串的拼接一定要會(huì) ????????????????values.add(tel); ???????????} ???????????sql.append("where code = ? "); ???????????values.add(model.getCode()); ???????????return values; ?????} } ajax請(qǐng)求實(shí)現(xiàn)數(shù)據(jù)修改操作(后半部分 更新)? ? ? ?(8) 將 結(jié)果再依次返回? 并將參數(shù)傳回到 upd.jsp 文件中 繼續(xù)執(zhí)行if 判斷 顯示相應(yīng)提示 完成 更新操作。 // 修改的后半部分 更新數(shù)據(jù) formSubmit("upd","/DepartmentServlet",'text',function(data){//console.log(data) ?????if(data==1){ ???????????layer.msg("修改成功",layerClose) ?????}else{ ???????????layer.msg("修改失敗") ?????} }) 分頁(yè)的實(shí)現(xiàn)?? 為了減少時(shí)間 暫時(shí)去掉了防盜鏈功能UserFilter類上面的注解 注釋了 最后記得取消掉啊!!!!!!!!! 三、交互過程? ?接受請(qǐng)求:? ?web-->servlet-->service-->dao-->sql ?回應(yīng)請(qǐng)求: table-->model-->dao( *jdbc - 數(shù)據(jù)庫(kù)和Java*)-->web-->servlet(*網(wǎng)頁(yè)和Java*)-->service servlet (每個(gè)分支的過程(1接受請(qǐng)求、2獲取數(shù)據(jù)、3封裝對(duì)象、4調(diào)用方法、5返回結(jié)果)) 四、未實(shí)現(xiàn)錯(cuò)誤點(diǎn) dataType : "text",//text / json? 可以 ? ??dataType : "json",? ? 不可以--- 原因是EmployeeServlet 中返回類型不對(duì)應(yīng),不是json類型,暫時(shí)采用 異常拋出的方式 dataType:'text' 只能是文本 dataType:'json' 可以是文本也可以是json 網(wǎng)上尋找的方法:通過建立異常拋出將錯(cuò)誤信息展示出來(只能是湊合,不嚴(yán)謹(jǐn) -- 屬于只能是勉強(qiáng)看上去是正確的!) function ajax(url,field,func){ ?????//console.log(field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url : base.app + url, ???????????type : "post", ???????????data : field, ???????????//dataType : "json",//text / json ???????????success : func, ? ? ? ? ?error:function(){// 異常處理 ? ? ? ? ? ? ? alert("注冊(cè)失敗,賬號(hào)重復(fù)"); ? ? ? ? ?} ??????????? ?????}); } 后來采用: function formSubmit(event,url,dataType,func){ ?????//console.log(event) 調(diào)試 ?????form.on("submit(" +event+")",function(data){ ?????//???console.log(data.field)調(diào)試 ???????????ajax(url,data.field,dataType,func) ?????}) } function ajax(url,field,dataType,func){ ?????//console.log(field) ?????$.ajax({//四個(gè)屬性一個(gè)方法 ???????????url : base.app + url, ???????????type : "post", ???????????data : field, ???????????dataType : dataType, //后采用 ???????????//dataType : "text",//text / json ???????????success : func ?????}); }
2.使用請(qǐng)求轉(zhuǎn)發(fā)的方式實(shí)現(xiàn) 回顯(get2)方法? ?未實(shí)現(xiàn) 報(bào)方法不可用
五、已解決錯(cuò)誤點(diǎn)
判斷注冊(cè)輸入賬號(hào)是否存在? 老師用的是 反射?
EmployeeDaoImpl類中? //???private static String cols = "code,name,pass"; ?????private static String cols =??"code,name,pass,code_dept,image"; ?????// 此處的數(shù)據(jù)庫(kù)表字段 在方法中可以不使用某一個(gè),但是不能在方法中使用沒有的表字段 控制臺(tái)報(bào) SQL語句錯(cuò)誤? ?code=‘網(wǎng)頁(yè)你輸入的數(shù)’
關(guān)于字符串拼接(沒搞明白)===》 (苦思冥想 終于解決,但是原因居然是因?yàn)榭崭? ,append的空格沒對(duì)起來? ? 還要記得里面有符號(hào)時(shí)要加 轉(zhuǎn)義字符? ?\? ? ?重要!!!!! 重要!!!!? 重要!!!!)
StringBuffer sql = new StringBuffer("select id,code,name,tel??from ").append(table); 不可以 (沒看出為什么來) StringBuffer sql = new StringBuffer("select id,code,name,tel??from ").append(table).append(" ");? 改成這樣就可以了? ? append 拼接時(shí)一定不要忘了 語句間的 空格(切記!!!)? ?不要忘了空格(重要重要!!!)? ?不要忘了空格!!!? ?不要忘了空格!!!不要忘了空格!!!不要忘了空格!!! ??????????? ? StringBuffer sql = new StringBuffer("select??id,code,name,tel from department ");? 可以
append? 未解之謎(不是未解了 ,搞定了!!!)
為什么?StringBuffer sql = new StringBuffer("select id,code,name,tel??from ").append(table); 取到的值為空? 但是StringBuffer sql = new StringBuffer("select??id,code,name,tel from department "); 就能取到? 初步懷疑 是因?yàn)?JDBCUtil工具類中的selectList遍歷有問題
五、NullPointerException 有可能是緩存的問題
1.
2.
六、解決報(bào)錯(cuò) 問題的步驟
? 1.網(wǎng)頁(yè) 控制臺(tái)報(bào) 500 就是java端代碼報(bào)錯(cuò)了?
? 2.回到j(luò)ava控制臺(tái)
3.先去 FmtRequest方法中 用控制臺(tái)輸出可能產(chǎn)生為空的變量的值 如果沒有輸出
4. 去DepartmentServlet 中 輸出add分支中 可能為空的變量(req) 如果 還沒輸出就是 分支根本沒有執(zhí)行 是jsp文件中的錯(cuò)誤
5.隱藏域中的方法和 java中 不對(duì)應(yīng)導(dǎo)致的
6.修改后又包sql語句錯(cuò)誤 然后去Department2DaoImpl 找insert 語句 發(fā)放插入sql語句錯(cuò)誤 字段對(duì)應(yīng)不起來 修改后 成功添加
拼接容易出錯(cuò)就用完整的sql語句 一點(diǎn)一點(diǎn)來實(shí)現(xiàn)
七、使用debug 方式來調(diào)試??
F5:跳入方法
F6:向下逐行調(diào)試
F7:跳出方法
F8:直接跳轉(zhuǎn)到下一個(gè)斷點(diǎn)
debug 詳解??Eclipse debug:F5、F6、F7、F8 作用_chao430的博客-CSDN博客_debug f5 f6 f7 f8
總結(jié)
以上是生活随笔為你收集整理的我的第一个项目(员工绩效管理系统 步骤超级详细---未完待更...)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vbs脚本截取字符
- 下一篇: 惠普服务器做虚拟化,节省成本立竿见影 惠