Jsp&Servlet入门级项目全程实录第4讲

惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

1、添加搜索、添加、修改、删除按钮
<div id="tb">
<div>
<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" >添加</a>|
<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" >修改</a>|
<a href="javascript:deleteGrade()" class="easyui-linkbutton" iconCls="icon-remove" plain="true" >删除</a>
</div>
<div>&nbsp;班级名称:<input type="text" name="s_gradeName" id="s_gradeName" /><a href="javascript:searchGrade()" class="easyui-linkbutton" iconCls="icon-search" plain="true" >搜索</a></div>
</div> 2、实现搜索功能
dao
public ResultSet gradeList(Connection con,PageBean pageBean,Grade grade)throws Exception{
StringBuffer sb=new StringBuffer("select * from t_grade");
if(StringUtil.isNotEmpty(grade.getGradeName())){
sb.append(" and gradeName like '%"+grade.getGradeName()+"%'");
}
if(pageBean!=null){
sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());
}
PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
return pstmt.executeQuery();
} 封装
String gradeName=request.getParameter("gradeName");
if(gradeName==null){
gradeName="";
}
Grade grade=new Grade();
grade.setGradeName(gradeName); 前端
<a href="javascript:searchGrade()" class="easyui-linkbutton" iconCls="icon-search" plain="true" >搜索</a> function searchGrade(){
$('#dg').datagrid('load',{
gradeName:$('#s_gradeName').val()
})
} 3、删除功能实现
dao
/**
* delete from tableName where field in(1,3,5)
* @param con
* @param delIds
* @return
* @throws Exception
*/
public int gradeDelect(Connection con,String delIds)throws Exception{
String sql="delete from t_grade where id in("+delIds+")";
PreparedStatement pstmt=con.prepareStatement(sql);
return pstmt.executeUpdate();
} 删除Servlet配置web.xml
<servlet>
<servlet-name>gradeDeleteServlet</servlet-name>
<servlet-class>com.java1234.web.GradeDeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>gradeDeleteServlet</servlet-name>
<url-pattern>/gradeDelete</url-pattern>
</servlet-mapping> String delIds=request.getParameter("delIds"); Connection con=null;
try {
con=dbUtil.getCon();
JSONObject result=new JSONObject();
int delNums=gradeDao.gradeDelect(con, delIds);
if(delNums>0){
result.put("success", "true");
result.put("delNums", delNums);
}else{
result.put("errorMeg", "删除失败");
}
result.put("delNum", delNums);
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();
}
}
} 前端
<a href="javascript:deleteGrade()" class="easyui-linkbutton" iconCls="icon-remove" plain="true" >删除</a> function deleteGrade(){
var selectedRows=$("#dg").datagrid('getSelections');
if(selectedRows.length==0){
$.messager.alert("系统提示","请选择要删除的数据!");
return;
}
var strIds=[];
for(var i=0;i<selectedRows.length;i++){
strIds.push(selectedRows[i].id);
}
var ids=strIds.join(",");
$.messager.confirm("系统提示","您确认要删掉这<font color=red>"+selectedRows.length+"</font>条数据吗?",function(r){
if(r){
$.post("gradeDelete",{delIds:ids},function(result){
if(result.success){
$.messager.alert("系统提示","您已成功删除<font color=red>"+result.delNum+"</font>条数据!");
$("#dg").datagrid("reload");
}else{
$.messager.alert("系统提示",result.errorMsg);
}
},"json");
}
});
}
上一篇:Lua: 给 Redis 用户的入门指导


下一篇:如何在网页中显示pdf