惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!
1、修改功能实现 dao public int gradeAdd(Connection con,Grade grade)throws Exception{ String sql="update t_grade set gradeName=?,gradeDesc=? where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, grade.getGradeName()); pstmt.setString(2, grade.getGradeDesc()); pstmt.setInt(3, grade.getId()); return pstmt.executeUpdate(); } 2、dialog对话框组件 <div id="dig" class="easyui-dialog" style="400px;height:280px;padding:10px 20px;" closed="true" buttons="#dlg-buttons"> <form id="fm" method="post" > <table> <tr> <td>班级名称:</td> <td><input type="text" name="gradeName" id="gradeName" class="easyui-validatebox" required="true" ></td> </tr> <tr> <td valign="top">班级描述:</td> <td><textarea rows="7" cols="30" name="gradeDesc" id="gradeDesc" ></textarea></td> </tr> </table> </form> </div> <div id="dlg-buttons"> <a href="#" class="easyui-linkbutton" iconCls="icon-ok" >保存</a> <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a> </div> <a href="javascript:openGradeAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true" >添加</a>| //添加标题 function openGradeAddDialog(){ $("#dig").dialog("open").dialog("setTitle","添加班级信息"); } 3、关闭 <a href="javascript:closeGradeDialog()" class="easyui-linkbutton" iconCls="icon-cancel" >关闭</a> function closeGradeDialog(){ $("#dig").dialog("close"); resetValue(); } 4、重置 function resetValue(){ $("#gradeName").val(""); $("#gradeDesc").val(""); } 5、乱码(tomcat默认不是utf-8) request.setCharacterEncoding("utf-8"); 6、添加 function saveGrade(){ $("#fm").form("submit",{ url:url, onSubmit:function(){ return $(this).form("validate"); }, success:function(result){ if(result.errorMsg){ $.messager.alert("系统提示",result.errorMsg); return; }else{ $.messager.alert("系统提示","保存成功"); resetValue(); $("#dlg").dialog("close"); $("#dg").datagrid("reload"); } } }); } request.setCharacterEncoding("utf-8"); String gradeName=request.getParameter("gradeName"); String gradeDesc=request.getParameter("gradeDesc"); Grade grade=new Grade(gradeName,gradeDesc); if(StringUtil.isNotEmpty(id)){ grade.setId(Integer.parseInt(id)); } Connection con=null; try { con=dbUtil.getCon(); int saveNums=0; JSONObject result=new JSONObject(); if(StringUtil.isNotEmpty(id)){ saveNums=gradeDao.gradeModify(con, grade); }else{ saveNums=gradeDao.gradeAdd(con, grade); } if(saveNums>0){ result.put("success", "true"); }else{ result.put("success", "true"); result.put("errorMeg", "删除失败"); } ResponseUtil.write(response, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public int gradeAdd(Connection con,Grade grade)throws Exception{ String sql="insert into t_grade values(null,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, grade.getGradeName()); pstmt.setString(2, grade.getGradeDesc()); return pstmt.executeUpdate(); } 7、修改 <a href="javascript:openGradeModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true" >修改</a>| function openGradeModifyDialog(){ var selectedRows=$("#dg").datagrid('getSelections'); if(selectedRows.length!=1){ $.messager.alert("系统提示","请选择一条要编辑的数据!"); return; } var row=selectedRows[0]; $("#dlg").dialog("open").dialog("setTitle","编辑班级信息"); $("#fm").form("load",row); url="gradeSave?id="+row.id; } request.setCharacterEncoding("utf-8"); String gradeName=request.getParameter("gradeName"); String gradeDesc=request.getParameter("gradeDesc"); String id=request.getParameter("id"); Grade grade=new Grade(gradeName,gradeDesc); if(StringUtil.isNotEmpty(id)){ grade.setId(Integer.parseInt(id)); } Connection con=null; try { con=dbUtil.getCon(); int saveNums=0; JSONObject result=new JSONObject(); if(StringUtil.isNotEmpty(id)){ saveNums=gradeDao.gradeModify(con, grade); }else{ saveNums=gradeDao.gradeAdd(con, grade); } if(saveNums>0){ result.put("success", "true"); }else{ result.put("success", "true"); result.put("errorMeg", "删除失败"); } ResponseUtil.write(response, result); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int gradeModify(Connection con,Grade grade)throws Exception{ String sql="update t_grade set gradeName=?,gradeDesc=? where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, grade.getGradeName()); pstmt.setString(2, grade.getGradeDesc()); pstmt.setInt(3, grade.getId()); return pstmt.executeUpdate(); }