Ajax使用
2015-10-09 09:18
330 查看
情况1 直接返回信息:
Message.java返回的信息
/** * 消息 */ public class Message { /** * 类型 */ public enum Type { /** 成功 */ success, /** 警告 */ warn, /** 错误 */ error } /** 类型 */ private Type type; /** 内容 */ private String content; /** * 初始化一个新创建的 Message 对象,使其表示一个空消息。 */ public Message() { } /** * 初始化一个新创建的 Message 对象 */ public Message(Type type, String content) { this.type = type; this.content = content; } public Message(Type type, String content, Object... args) { this.type = type; this.content = SpringUtils.getMessage(content, args); } /** * 返回成功消息 * */ public static Message success(String content, Object... args) { return new Message(Type.success, content, args); } /** * 返回警告消息 * */ public static Message warn(String content, Object... args) { return new Message(Type.warn, content, args); } /** * 返回错误消息 */ public static Message error(String content, Object... args) { return new Message(Type.error, content, args); } /** * 获取类型 * * @return 类型 */ public Type getType() { return type; } /** * 设置类型 * * @param type 类型 */ public void setType(Type type) { this.type = type; } /** * 获取内容 * * @return 内容 */ public String getContent() { return content; } /** * 设置内容 * * @param content 内容 */ public void setContent(String content) { this.content = content; } @Override public String toString() { return SpringUtils.getMessage(content); } }
ban.java controller
/** * 禁用 */ @RequestMapping(value = "/ban", method = RequestMethod.POST) public @ResponseBody Message ban(Long id, ModelMap model, HttpServletRequest request,RedirectAttributes redirectAttributes) { AppCategory appCategory = appCategoryService.find(id); if(appCategory.getApps() != null && appCategory.getApps().size() > 0){ return Message.warn("该分类下存在应用,请确认!"); } appCategory.setStatus(false); appCategoryService.update(appCategory); return SUCCESS_MESSAGE; }
list.ftl 前台ajax
// 禁用 $ban.click(function() { var $this = $(this); var id = $this.closest("tr").find("input[name='ids']").val(); $.dialog({ type: "warn", content: "${message("console.appCategory.banConfirm")}", ok: message("console.dialog.ok"), cancel: message("console.dialog.cancel"), onOk: function() { layer.load(); $.ajax({ url: "ban.ct", type: "POST", data: {id:id}, dataType: "json", cache: false, success: function(message) { layer.closeAll('loading'); $.message(message); if (message.type == "success") { setTimeout(function() { location.reload(true); }, 1500); } } }); } }); });
情况2 不提示信息 刷新页面
function changeStatus(id,status){ $.ajax({ type:"GET", url:"changeStatus.ct", data:{ id:id, status:status }, success:function(){ location.reload(); } }) }
@RequestMapping(value="/changeStatus",method = RequestMethod.GET) public void changeStatus(Long id,Boolean status,HttpServletResponse response){ AdCategory adCategory = adCategoryService.find(id); adCategory.setStatus(status); adCategoryService.update(adCategory); try { response.setContentType("text/html; charset=UTF-8"); JsonUtils.writeValue(response.getWriter(), "success"); } catch (IOException e) { e.printStackTrace(); } }
情况3 处理返回数据对象
//生成appKey function changeAppKey(){ $.ajax({ type:"GET", url:"changeAppKey.ct", success:function(data){ var dataJson = JSON.parse(data); $("#appKey").val(dataJson.appKey); $("#appSecret").val(dataJson.appSecret); }, }); }
/** * 重置key * * @param response * @see [类、类#方法、类#成员] */ @RequestMapping(value = "/changeAppKey", method = RequestMethod.GET) public void changeAppKey(HttpServletResponse response) { AppCredential appKey = this.generaterAppKey(); try { response.setContentType("text/html; charset=UTF-8"); JsonUtils.writeValue(response.getWriter(), appKey) aeb0 ; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
情况4
/* 下架应用 */ function unShelve(appId,type){ var shelveMessage ="
确认下架该应用?
";if(type =="up"){
shelveMessage ="
确认上架该应用?
";}
layer.confirm(shelveMessage, {
title:false,
closeBtn: false,
btn: ['确定','取消'], //按钮
shade: false //不显示遮罩
}, function(){
//左边按钮的响应事件
$.ajax({
type:"GET",
url:"unShelve.ct",
data:{
id:appId,
type:type
},
dataType:"json",
success:function(appName){
var message = "应用 "+appName+" 已";
if(type =="up"){
message+="上架";
$("#shelve").html('下架');
}else{
message+="下架";
$("#shelve").html('上架');
}
layer.msg(message, {
icon: 1,
time:1200
});
location.reload();
},
});
}, function(){
//右边按钮的响应事件
layer.close();
});
}
@RequestMapping(value = "/unShelve", method = RequestMethod.GET) public void unShelve(Long id, String type, HttpServletResponse response) { App app = appService.find(id); if ("down".equals(type)) { app.setIsOnline(false); } else { app.setIsOnline(true); app.setOnlineDate(DateUtil.getDate()); } appService.update(app); try { response.setContentType("text/html; charset=UTF-8"); JsonUtils.writeValue(response.getWriter(), app.getAppName()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
相关文章推荐
- 未处理System.TypeInitializationException异常处理
- 一个数码管电路引起的思考
- SQL Server Management Studio 2012 设置脚本默认保存路径
- 第六周上机实践项目5——后缀表达式
- 第6周 项目4—数制转换
- 第三周项目4 顺序表应用
- Android TextView自动换行文字排版参差不齐的原因
- 第六周 项目1-建立顺序栈算法库
- leetcode 258--Add Digits
- 异步和非阻塞
- 先作较小规模的投资,借以加深对有关业务的了解,并在当地积累经验和建立人脉网络,增加对监管架构的认识
- 第四周项目2建立“单链表”算法库
- 世界最小移动电源问世——小到看不见!
- 第四周项目2 建设“单链表”算法库
- VS2013 图片背景·全透明背景图(转)
- C++编译后的函数名反向
- 开关型直流电源比线性直流电源效率高的原因?
- 对Thrift的一点点理解
- digiKam-图片批量处理功能,还是非常实用的
- 使用Slua框架开发Unity项目的重要步骤