Ajax使用

情况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);
        }
        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();
        }
    }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值