当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jfinal与bootstrap的登出实战详解

jfinal与bootstrap的登出实战详解

2017年12月08日  | 移动技术网IT编程  | 我要评论

前言:本篇推出“jfinal与bootstrap的登出实战”,旨在介绍如果通过a标签弹出登出确认框,然后发送退出请求到jfinal,然后再刷新页面的做法。主要难点在于1.如果通过a标签的内容弹出登出确认框,2.如何通过a标签刷新对应弹出的页面。

前端技术

1.构建a标签

复制代码 代码如下:
<a href="${ctx}/mem/logout"  target="ajaxtodo" callback="ajaxdone" atitle="你确定要退出吗?" id="user_login_out" style="padding: 0 6px;">退出</a>

注意:

1. target=”ajaxtodo”,指定a标签要通过ajax发起请求。
2. callback=”ajaxdone”,指定a标签回调函数
3. atitle=”你确定要退出吗?”,指定确认信息

2.初始化a标签ajax事件

function initui(_box) {
 var $p = $(_box || document);

 // dwz.ajax.js
 if ($.fn.ajaxtodo) {
  $("a[target=ajaxtodo]", $p).ajaxtodo();
 }
}

注意:

1. 页面加载完成后执行initui方法,使target为ajaxtodo的a标签具有指定的ajaxtodo方法。

3.a标签的ajax请求

function ajaxtodo(url, callback) {
 var $callback = callback;
 if (!$.isfunction($callback)) {
  $callback = eval('(' + callback + ')');
 }

 var forwardurl = window.location.href;
 if (url.indexof("?") != -1) {
  url += "&forwardurl=" + forwardurl;
 } else {
  url += "?forwardurl=" + forwardurl;
 }
 $.ajax({
  type : 'post',
  url : url,
  datatype : "json",
  cache : false,
  success : $callback,
  error : yunm.ajaxerror
 });
}

注意:

1. forwardurl 记录登出的页面

4.为jquery对象增加ajaxtodo方法

$.fn.extend({
 ajaxtodo : function() {
 return this.each(function() {
  var $this = $(this);
  $this.click(function(event) {
  var url = unescape($this.attr("href")).replacetmbyid($(event.target).parents(".unitbox:first"));
  yunm.debug(url);
  if (!url.isfinishedtm()) {
   $.showerr($this.attr("warn"));
   return false;
  }
  var title = $this.attr("atitle");
  if (title) {
   $.showconfirm(title, function() {
   ajaxtodo(url, $this.attr("callback"));
   });
  } else {
   ajaxtodo(url, $this.attr("callback"));
  }
  event.preventdefault();
  });
 });
 },
});

5.回调函数

function ajaxdone(json) {
 yunm.ajaxdone(json);
 if (json[yunm.keys.statuscode] == yunm.statuscode.ok || json[yunm.keys.statuscode] == yunm.statuscode.info) {
 // 如果指定了后调转页面,进行调转
 if (json.forwardurl) {
  location.href = json.forwardurl;
 }
 }
}

6.弹出weebox确认框

$.showconfirm = function(str, funcok, funcclose) {
 var okfunc = function() {
 $.weeboxs.close("yunm_confirm_box");
 funcok.call();
 };
 $.weeboxs.open(str, {
 boxid : 'yunm_confirm_box',
 contenttype : 'text',
 showbutton : true,
 showcancel : true,
 showok : true,
 title : '确认',
 width : 280,
 type : 'wee',
 onopen : function() {
  init_ui_button();
 },
 onclose : funcclose,
 onok : okfunc
 });
};

function init_ui_button() {
 $("button.ui-button[init!='init']").each(function(i, o) {
 $(o).attr("init", "init"); // 为了防止重复初始化
 $(o).ui_button();
 });

}

jfinal技术

public void logout() {

 if (getsession().getattribute("username") != null) {
  // 清除session
  getsession().removeattribute("username");
 }

 ajaxdonesuccess("登出成功!");

 renderjson();
 }


增加logout方法。

这里写图片描述

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网