您的位置:首页 > 其它

利用ajax异步处理发布状态(发布还是停用)

2016-03-29 10:10 141 查看
1、如图,改变发布状态



2、jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<%@include file="/common/header.jsp"%>
<title>信息发布管理</title>
<script type="text/javascript">
//全选、全反选
function doSelectAll() {
// jquery 1.6 前
//$("input[name=selectedRow]").attr("checked", $("#selAll").is(":checked"));
//prop jquery 1.6+建议使用
$("input[name=selectedRow]").prop("checked",
$("#selAll").is(":checked"));
}

//添加
function doAdd(){
document.forms[0].action="${basePath}nsfw/info_addUI.action";
document.forms[0].submit();
}
//编辑
function doEdit(id){
document.forms[0].action="${basePath}nsfw/info_editUI.action?info.infoId=" + id;
document.forms[0].submit();
}
//删除
function doDelete(id){
document.forms[0].action="${basePath}nsfw/info_delete.action?info.infoId=" + id;
document.forms[0].submit();
}
//多选删除
function doDeleteAll(){
document.forms[0].action="${basePath}nsfw/info_deleteSelected.action";
document.forms[0].submit();
}
//异步发布信息,信息的id及将要改成的信息状态
function doPublic(infoId,state){
//1、更新信息状态
$.ajax({
url:"${basePath}nsfw/info_publicInfo.action",
data:{"info.infoId":infoId,"info.state":state},
type:"post",
success:function(msg){
//2、更新状态栏,操作栏的显示值
if("更新状态成功" == msg){
if(state == 1){//说明信息状态已被改成发布,状态栏显示发布,操作栏显示停用
$("#show_"+infoId).html("发布");
$("#oper_"+infoId).html('<a href="javascript:doPublic(\''+infoId+'\',0)">停用</a>');
}else{
$("#show_"+infoId).html("停用");
$("#oper_"+infoId).html('<a href="javascript:doPublic(\''+infoId+'\',1)">发布</a>');
}
}else{
alert("更新信息状态失败!");
}
},
error:function(){
alert("更新信息状态失败!");
}
});
}
</script>
</head>
<body class="rightBody">
<form name="form1" action="" method="post">
<div class="p_d_1">
<div class="p_d_1_1">
<div class="content_info">
<div class="c_crumbs"><div><b></b><strong>信息发布管理</strong></div> </div>
<div class="search_art">
<li>
信息标题:<s:textfield name="info.title" cssClass="s_text" id="infoTitle"  cssStyle="width:160px;"/>
</li>
<li><input type="button" class="s_button" value="搜 索" onclick="doSearch()"/></li>
<li style="float:right;">
<input type="button" value="新增" class="s_button" onclick="doAdd()"/> 
<input type="button" value="删除" class="s_button" onclick="doDeleteAll()"/> 
</li>
</div>

<div class="t_list" style="margin:0px; border:0px none;">
<table width="100%" border="0">
<tr class="t_tit">
<td width="30" align="center"><input type="checkbox" id="selAll" onclick="doSelectAll()" /></td>
<td align="center">信息标题</td>
<td width="120" align="center">信息分类</td>
<td width="120" align="center">创建人</td>
<td width="140" align="center">创建时间</td>
<td width="80" align="center">状态</td>
<td width="120" align="center">操作</td>
</tr>
<s:iterator value="infoList" status="st">
<tr <s:if test="#st.odd"> bgcolor="f8f8f8" </s:if> >
<td align="center"><input type="checkbox" name="selectedRow" value="<s:property value='infoId'/>"/></td>
<td align="center"><s:property value="title"/></td>
<td align="center">
<s:property value="#infoTypeMap[type]"/>
</td>
<td align="center"><s:property value="creator"/></td>
<td align="center"><s:date name="createTime" format="yyyy-MM-dd HH:mm"/></td>
<td id="show_<s:property value='infoId'/>" align="center"><s:property value="state==1?'发布':'停用'"/></td>
<td align="center">
<span id="oper_<s:property value='infoId'/>">
<s:if test="state==1">
<a href="javascript:doPublic('<s:property value='infoId'/>',0)">停用</a>
</s:if>
<s:else>
<a href="javascript:doPublic('<s:property value='infoId'/>',1)">发布</a>
</s:else>

</span>
<a href="javascript:doEdit('<s:property value='infoId'/>')">编辑</a>
<a href="javascript:doDelete('<s:property value='infoId'/>')">删除</a>
</td>
</tr>
</s:iterator>
</table>
</div>
</div>
<div class="c_pate" style="margin-top: 5px;">
<table width="100%" class="pageDown" border="0" cellspacing="0"
cellpadding="0">
<tr>
<td align="right">
总共1条记录,当前第 1 页,共 1 页   
<a href="#">上一页</a>  <a href="#">下一页</a>
到 <input type="text" style="width: 30px;" onkeypress="if(event.keyCode == 13){doGoPage(this.value);}" min="1"
max="" value="1" />   
</td>
</tr>
</table>
</div>

</div>
</div>
</form>

</body>
</html>


3、后台action处理代码

//异步发布信息
public void publicInfo(){
try {
if(info != null){
//1、更新消息状态
Info tem = infoService.findObjectById(info.getInfoId());
tem.setState(info.getState());
infoService.update(tem);

//2、输出更新结果
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write("更新状态成功".getBytes("utf-8"));
outputStream.close();

}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: