DWR同步调用的一点改进

DWR提供的JS方法调用默认是异步的,为了得到同步的运行结果,一般类似以下方式处理:

 DWREngine.setAsync(false); //设置成同步
var _data = null;
test1Ajax.sayHello('hello', function(data){_data = data;}); //dwr调用服务端的函数
DWREngine.setAsync(true); //重新设置成异步
alert(_data); //对返回值进行处理


很简单的需求,为什么代码要如此繁琐呢?
可以对DWR简单改动后,以下面的方式调用同步方法:

 var result = test1Ajax.sayHello('hello'); //调用同步方法
alert(result); //对返回值进行处理


如果需要异步调用的话,和以前的写法一样:

 test1Ajax.sayHello('hello', function(data){
alert(data);//在对返回值进行处理
});


改动主要是将设置同步的操作放到DWR动态生成的JS代码中,如下:


package org.directwebremoting;

import java.lang.reflect.Method;

import org.directwebremoting.extend.EnginePrivate;
import org.directwebremoting.impl.DefaultRemoter;
import org.directwebremoting.util.LocalUtil;

/**
* 支持DWR同步方法调用 支持DWR3.0.0.116.rc1<br>
*
* JS代码 异步调用:<br>
* test1.sayHello("hello", function(data){ alert(data); } );
*
* JS代码 同步调用:<br>
* var result = test1.sayHello("hello"); <br>
* alert(result);
*
* @author sswh
* @createDate 2010-5-12
*/
public class SyncRemoter extends DefaultRemoter {

@Override
protected String getMethodJS(String scriptName, Method method) {
StringBuffer buffer = new StringBuffer();

String methodName = method.getName();
Class<?>[] paramTypes = method.getParameterTypes();

// Create the sdoc comment
buffer.append("/**\n");
for (int j = 0; j < paramTypes.length; j++) {
if (!LocalUtil.isServletClass(paramTypes[j])) {
buffer.append(" * @param {");
buffer.append(paramTypes[j]);
buffer.append("} p");
buffer.append(j);
buffer.append(" a param\n");
}
}
buffer.append(" * @param {function|Object} callback callback function or options object\n");
buffer.append(" */\n");

// Create the function definition
buffer.append(scriptName);
buffer.append('.');
buffer.append(methodName);
buffer.append(" = function(");
for (int j = 0; j < paramTypes.length; j++) {
if (!LocalUtil.isServletClass(paramTypes[j])) {
buffer.append("p");
buffer.append(j);
buffer.append(", ");
}
}
buffer.append("callback) {\n");

buffer.append(" if(callback != null){\n");
// The method body calls into engine.js
buffer.append(" return ");
buffer.append(EnginePrivate.getExecuteFunctionName());
buffer.append("(");
buffer.append(scriptName);
buffer.append("._path, '");
buffer.append(scriptName);
buffer.append("', '");
buffer.append(methodName);
buffer.append("\', arguments);\n");
buffer.append(" }\n\n");

buffer.append(" //synchronized\n");
buffer.append(" var dwr_result = null;\n");
buffer.append(" var dwr_async = dwr.engine._async;\n");
buffer.append(" dwr.engine._async = false;\n");
buffer.append(" var dwr_callback = function(data){dwr_result = data;};\n");
buffer.append(" var dwr_arguments = [];\n");
buffer.append(" for(var i=0; i<arguments.length; i++) dwr_arguments[i] = arguments[i];\n");
buffer.append(" dwr_arguments[arguments.length] = dwr_callback;\n ");
buffer.append(EnginePrivate.getExecuteFunctionName());
buffer.append("(");
buffer.append(scriptName);
buffer.append("._path, '");
buffer.append(scriptName);
buffer.append("', '");
buffer.append(methodName);
buffer.append("\', dwr_arguments);\n");
buffer.append(" dwr.engine._async = dwr_async;\n");
buffer.append(" return dwr_result;\n");
buffer.append("};\n\n");

return buffer.toString();

}
}


另外重新提供一个defaults.properties文件,将其中的
org.directwebremoting.extend.Remoter修改为:org.directwebremoting.SyncRemoter即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值