|||
用户退出系统时,需要清除session中存留的数据。数据清理完毕后,跳转到用户登录界面。这里通过jQuery的easyui-tab控件定义退出按钮并给此控件绑定事件处理函数,代码如下:
1.jsp中定义div
<div class="easyui-tabs" id="centerTab" fit="true" border="false">
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通过<% String root= request.getContextPath(); %>获得。
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,这里版本要一致,否则会报错。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-5-19 01:32
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社