关于整合ext与jsonrpc的一点经验

      最近在自己的一个小项目中使用了ext+jsonrpc直接调用后台暴露的业务逻辑接口方式,在使用的过程中遇到了不少问题,在这里和大家分享一下:)

      最初在整合jsonrpc的时候遇到个问题,就是它的bridge是和session绑定的,很讨厌这种方式,得要在每个jsp中写那恶心的标签.后来自己找找,发现有个getGlobalBridge可以获取全局bridge,然后自己写了个小工具,让其可以把spring中的业务逻辑bean通过配置文件直接暴露出去.

/**
 * 映射JSONBridge与Spring所管理的对象.
 * 
 * @version 2008.07.04
 * @author bin
 */
public class JSONRPCSpringMappingListener implements ServletContextListener {

	/** 全局资源文件属性名 */
	private static String CONFIGNAME = "JSONRPCSpringMappingConfig";

	public void contextDestroyed(ServletContextEvent sce) {
	}

	public void contextInitialized(ServletContextEvent sce) {
		Properties properties = new Properties();
		WebApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(sce.getServletContext());
		JSONRPCBridge bridge = JSONRPCBridge.getGlobalBridge();

		// 读取资源文件
		for (String propFile : sce.getServletContext().getInitParameter(
				CONFIGNAME).split(";")) {
			try {
				if (propFile.startsWith("classpath:")) {
					properties.load(new BufferedInputStream(
							new FileInputStream(new File(sce
									.getServletContext().getRealPath(
											"/WEB-INF/classes/")
									+ "/" + propFile.substring(10)))));
				} else {
					properties.load(new BufferedInputStream(
							new FileInputStream(new File(sce
									.getServletContext().getRealPath("/")
									+ propFile))));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		// 分析资源文件,注册到全局bridge
		for (Object key : properties.keySet()) {
			String[] values = properties.getProperty(key.toString()).split(",");
			if (values.length > 1) {
				try {
					bridge.registerObject(key, context.getBean(values[0]),
							Class.forName(values[1]));
				} catch (BeansException e) {
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			} else {
				bridge.registerObject(key, context.getBean(values[0]));
			}
		}
	}

}

 这样我就可以在一个属性文件中编辑我要暴露的业务逻辑bean名字以及我需要暴露的接口了.

 

 

接下来在面对store的时候发现没有提供用方法提供数据的store于是自己又写了一小小的自定义store:

RpcStore = function(c) {
	RpcStore.superclass.constructor.call(this, Ext.apply(c, {
		proxy : new RpcProxy({
			rpcfn : c.rpcfn
		}),
		reader : new Ext.data.JsonReader(c, c.fields)
	}));
};
Ext.extend(RpcStore, Ext.data.Store);

RpcProxy = function(c) {
	RpcProxy.superclass.constructor.call(this);
	this.rpcfn = c.rpcfn;
};
Ext.extend(RpcProxy, Ext.data.DataProxy, {
	load : function(params, reader, callback, scope, arg) {
		// params = params || {};
		var result;
		try {
			if (Utils.hasProperty(params)) {
				result = reader.readRecords(this.rpcfn(params));
			}else{
				result = reader.readRecords(this.rpcfn());
			}
		} catch (e) {
			this.fireEvent("loadexception", this, arg, null, e);
			callback.call(scope, null, arg, false);
			return;
		}
		callback.call(scope, result, arg, true);
	}
});
Utils = {};
Utils.hasProperty = function(o){
	for(var p in o){
		return true;
	}
	return false;
}

 这样我在使用后台数据的时候只需要

store = new RpcStore({
	rpcfn : jsonrpc.somebean.somemethod,
	fields : ["somefield"]
});
store.load({params:{something:"something"}});
 

 

你可能感兴趣的:(spring,C++,c,ext,C#)