SSM博客系统
SMM博客系统(博客类型的增删查改-查)
文章目录
博客类型的增删改
BlogTypeAdminController
/**
* 添加,更新博客类别信息
* @throws Exception
*/
@RequestMapping({"/save"})
public String save(BlogType blogType,HttpServletResponse response) throws Exception {
int resultTotal=0;
//如果传过来的是空的id那么就是新增
if(blogType.getId()==null) {
//添加
resultTotal=blogTypeService.add(blogType).intValue();
}else {
//更新
resultTotal=blogTypeService.update(blogType).intValue();
}
JSONObject result=new JSONObject();
if(resultTotal>0) {
result.put("success", Boolean.valueOf(true));
}else {
result.put("success", Boolean.valueOf(false));
}
ResponseUtil.write(response, result);
return null;
}
/**
* 删除博客类别
*/
@RequestMapping({"/delete"})
public String delete(@RequestParam("ids")String ids,HttpServletResponse response) throws Exception {
String[] idsStr =ids.split(",");
for(int i=0;i<idsStr.length;i++) {
blogTypeService.delete(Integer.valueOf(idsStr[i]));//要传入的是Integer,这里是String
}
JSONObject result=new JSONObject();
result.put("success", Boolean.valueOf(true));
ResponseUtil.write(response, result);
return null;
}
博客类别管理界面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>博客类别管理页面</title>
<!-- {pageContext.request.contextPath}是JSP取得绝对路径的方法 -->
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/themes/icon.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<!-- 简体中文 -->
<script type="text/javascript" src="${pageContext.request.contextPath}/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
var url;
/**修改弹出添加博客类别信息对话框*/
function openBlogTypeModifyDiag(){
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);
//扩展之前新增的功能,既能add又能edit
url="${pageContext.request.contextPath}/admin/blogType/save.do?id="+row.id;
}
/**打开添加博客弹窗*/
function openBlogTypeAddDiag() {
$("#dlg").dialog("open").dialog("setTitle","添加博客类别信息");
url="${pageContext.request.contextPath}/admin/blogType/save.do";
}
/**保存博客类别信息*/
function saveBlogType(){
$("#fm").form("submit",{
url:url,
onSubmit:function(){
return $(this).form("validate");
},
success:function(result){
var result=eval('('+result+')');
if(result.success){
$.messager.alert("系统提示","保存成功!");
resetValue();
//关闭对话框
$("#dlg").dialog("close");
//刷新查询结果
$("#dg").datagrid("reload");
}else{
$.messager.alert("系统提示","保存失败!");
return;
}
}
});
}
//重置弹出的对话框
function resetValue(){
$("#typeName").val("");
$("#orderNo").val("");
}
/**关闭弹窗弹窗*/
function closeBlogTypeDialog(){
$("#dlg").dialog("close");
resetValue();
}
/**删除博客类型*/
function deleteBlogType(){
var selectedRows=$("#dg").datagrid("getSelections");
if (selectedRows.length==0){
$.messager.alert("系统提示","至少选择一条要删除的数据!");
return;
}
//装载id
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("${pageContext.request.contextPath}/admin/blogType/delete.do",{ids:ids},function(result){
if(result.success){
$.messager.alert("系统提示","删除成功!");
//刷新查询结果
$("#dg").datagrid("reload");
}else{
$.messager.alert("系统提示","数据删除失败");
}
},"json");
}
});
}
</script>
</head>
<!-- url取的是controller里面的RequestMapping 然后某一个方法的@RequestMapping -->
<body style="margin: 1px">
<!-- 内嵌显示博客类型页面 -->
<table id="dg" title="博客类别管理" class="easyui-datagrid" fitcolumns="true" pagination="true" rownumbers="true"
url="${pageContext.request.contextPath}/admin/blogType/list.do" fit="true" toolbar="#tb">
<!--.do一般是servlet的映射。(即:.do是个请求,不是文件。系统遇到.do的请求后就会提交给某个servlet来处理,这个servlet会根据具体的配置转发给某个后台的程序进行数据处理,然后给数据传递到页面view,最终给页面展现在用户面前,不一定是struts的,这个请求是可以自己随便配置的,你可以配置成.html,这样就是经常看到假静态) -->
<thead>
<tr>
<th field="cb" checkbox="true" align="center"></th>
<th field="id" width="40" align="center">编号</th>
<th field="typeName" width="90" align="center">博客类型名称</th>
<th field="orderNo" width="90" align="center">排序序号</th>
</tr>
</thead>
</table>
<!-- 添加博客类型 -->
<div id="tb">
<a href="javascript:openBlogTypeAddDiag()" class="easyui-linkbutton" iconCls="icon-add" plain="true"> 添加</a>
<a href="javascript:openBlogTypeModifyDiag()" class="easyui-linkbutton" iconCls="icon-edit" plain="true"> 更改</a>
<a href="javascript:deleteBlogType()" class="easyui-linkbutton" iconCls="icon-remove" plain="true"> 删除</a>
</div>
<!-- closed是有关闭选项 -->
<div id="dlg" class="easyui-dialog" style="width:500px;height:170px;padding: 2px 20px"
closed="true" buttons="#dlg-buttons">
<form id="fm" method="post">
<table cellspacing="8px">
<tr>
<td>博客类别名称:</td>
<td><input type="text" id="typeName" name="typeName" class="easyui-validatebox" required="true"/></td>
</tr>
<tr>
<td>博客类别排序:</td>
<td><input type="text" id="orderNo" name="orderNo" class="easyui-validatebox" required="true" style="width:60px;"/>(类别根据排序序号从小到大排序)</td>
</tr>
</table>
</form>
</div>
<div id="dlg-buttons">
<a href="javascript:saveBlogType()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
<a href="javascript:closeBlogTypeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
</div>
</body>
</html>
结尾小插曲-Bug
Cannot delete or update a parent row: a foreign key constraint fails (db_blog
.t_blog
, CONSTRAINT t_blog_ibfk_1
FOREIGN KEY (typeId
) REFERENCES t_blogtype
(id
))–无法删除或更新父行:外键约束失败(’ db_blog ‘。外键约束为’ typeId ‘引用’ t_blogtype ’ (’ id '))
因为之前删除的那个博客类型有博客外键约束 所以无法删除