用户退出系统时,需要清除session中存留的数据。数据清理完毕后,跳转到用户登录界面。这里通过jQuery的easyui-tab控件定义退出按钮并给此控件绑定事件处理函数,代码如下:
1.jsp中定义div
2. 生成控件并绑定事件
$('#centerTab').tabs({
tools:[{
iconCls:'icon-back',
handler: function(){
$.messager.confirm('注销提示', '你确定注销吗?', function(r){
if(r){
doLogout();
}
});
}
}]
});
3. 事件处理函数如下
function doLogout() {
$.ajax( {
async : false,
cache : false,
type : 'POST',
dataType : "json",
url : root + "/Web/login/doLogout.action",// 请求的action路径
success : function(data) { // 请求成功后处理函数。
if (data == null) {
window.location = root+"/Web/login/page/LoginPage.jsp";
} else if (data.errorMsg!=null) {//后台异常处理
}
}
});
}
注意:1. 这里url是由项目名称+命名空间名称+action名称组成,其中root通过获得。
2.服务器端如何将ajax请求结果回馈到前端data呢?
这里,服务器端需要定义对应POJO,如:
public class Message {
private String errorMsg;
private boolean result;
public Message(){}
public Message(String errorMsg){
super();
this.errorMsg = errorMsg;
}
public Message(String errorMsg, boolean result) {
super();
this.errorMsg = errorMsg;
this.result = result;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public boolean isResult() {
return result;
}
public void setResult(boolean result) {
this.result = result;
}
}
通过HttpServletResponse的一个对象response(response.setCharacterEncoding(ConstantUtil.ENCODING_UTF8)解决乱码问题)的getWriter方法获取PrintWriter的一个对象out,然后将数据以json的形式print出来即可。
PrintWriter out;
try {
out = response.getWriter();
Message msg = new Message(errorMsg);
out.print(JSONObject.fromObject(msg).toString());
} catch (IOException e1) {
e1.printStackTrace();
}
JSONObject.fromObject需要导入ezmorph-1.0.3和json-lib-2.1,这里版本要一致,否则会报错。
转载本文请联系原作者获取授权,同时请注明本文来自程冬科学网博客。
链接地址:http://blog.sciencenet.cn/blog-448935-803986.html
上一篇:mathematica绘制几何图形
下一篇:ajax获取json数据然后将其装载到jqgrid实现