1:后台方法的实现
//班级的添加的实现 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(); } //班级的修改的实现 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.getGradeDesc()); pstmt.setString(2, grade.getGradeDesc()); pstmt.setInt(3, grade.getId()); return pstmt.executeUpdate(); }
2:在新建一个servlet类,建好这个类之后就再web.xml配置:
<servlet> <servlet-name>gradeSaveServlet</servlet-name> <servlet-class>com.fxr.web.GradeSaveServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>gradeSaveServlet</servlet-name> <url-pattern>/gradeSave</url-pattern> </servlet-mapping>
3:编写前台的界面:
- 首先引入JQuery的easyui-dialog对话框
easyui-validatebox表单验证功能
对话框的代码:
<!-- 添加对话框 --> <div id="dlg" class="easyui-dialog" style="width: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="javascript:saveGrade()" class="easyui-linkbutton" iconCls="icon-ok">保存</a> <a href="javascript:closeGradeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a> </div>
用Javascript做点击添加按钮的操作弹出对话框:
<a href="javascript:openGradeAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
function openGradeAddDialog(){ $("#dlg").dialog("open").dialog("setTitle","添加班级信息"); url="gradeSave";//提交给那个servlet处理 }
同样的方式修改的对话框:
<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; }
保存的按钮操作:
<div id="dlg-buttons"> <a href="javascript:saveGrade()" class="easyui-linkbutton" iconCls="icon-ok">保存</a> <a href="javascript:closeGradeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a> </div>
保存的javascript方法的实现:
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"); } } }); }
function resetValue(){
$("#gradeName").val("");
$("#gradeDesc").val("");
}
后台servlet的代码的实现:
package com.fxr.web; import java.io.IOException; import java.sql.Connection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import com.fxr.dao.GradeDao; import com.fxr.model.Grade; import com.fxr.util.DbUtil; import com.fxr.util.ResponseUtil; import com.fxr.util.StringUtil; public class GradeSaveServlet extends HttpServlet{ DbUtil dbUtil=new DbUtil(); GradeDao gradeDao=new GradeDao(); @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 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("errorMsg", "保存失败"); } ResponseUtil.write(response, result); }catch(Exception e){ e.printStackTrace(); }finally{ try { dbUtil.closeCon(con); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
时间: 2024-10-09 14:56:06