html自动跳转到action,科学网—js直接调用action的一种解决方法 - 程冬的博文

用户退出系统时,需要清除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实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值