`

让Ajax框架Buffalo支持JSON协议而非仅仅自定义的XML协议[JS前端及单元测试完成]

阅读更多

Buffalo默认实现了一套很棒的自定义XML协议(具体请参考拙作《Ajax框架Buffalo深度研究》)。

然而在崇尚“标准”的当下,很多人对这种“非标准”的自定义的协议并不感冒,哪怕它再好。

在这种情况下,本人也是本着对Buffalo的继续研究、提升自我的态度,尝试基于org.json的“正宗”JSON参考实现RI,让Buffalo完全支持JSON标准协议。

 

本来打算自己默默研究,等完全成型之后再发布出来,然而自《Ajax框架Buffalo深度研究》发布后,在社区所凝聚的人气之旺,让我有些震惊(或者说惊喜),继而灵机一动:何不利用社区的强大平台,在线开展此项工作呢?

不能保证每天甚至每周都有让人惊喜的进展,仅希望通过这种方式,鞭策自己的同时,也获得更多社区高手的批评斧正,快速提升,少走弯路。

 

接下来的工作,欢迎各位踊跃拍砖,谢谢!

 

 

2010-02-08,状态简述:

1)前端js已经编写完成:充分结合XML协议和JSON协议,完成了代码的重构

2)单元测试已经完成,并提供了完整的参考XML协议的js单一测试用例(同样基于script.aculo.us 的unittest.js)

3)升级prototype.js到最新的1.6.1(其实buffalo对prototype.js依赖不大)

接下来的工作:

1、完成前后端集成测试,并完善demo

2、适时开展RC

 

2010-01-25,状态简述:

1)后端代码重构完成,并已纳入buffalo SVN, 建立了新的branch,位于branches/json/;

2)接下来工作的重点是:a.着手编写前台js; c.并结合demo完成集成测试。

分享到:
评论
7 楼 itstarting 2010-01-15  
<p><span style="">改造:</span><span style="" lang="EN-US">BuffaloInvoker.java</span></p>
<p> </p>
<p><span style="" lang="EN-US">
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">根据</span><span lang="EN-US">BuffaloProtocal.java</span><span style="">的改造而做出的适应性改造。</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">首先增加了一个类的私有变量</span><span lang="EN-US">protocol</span><span style="">,用以表达所用协议:</span></p>
</span></p>
<p><span style="">
<pre name="code" class="java">private String protocol = "XML";</pre>
</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">废弃原有获取单例的方式并增加新的需要明确协议的方式:</span></p>
<p><span style="">
<pre name="code" class="java"> /**
* get the instance for default xml protocol
* @return
* @deprecated use getInstance(String protocol) instead
*/
public static BuffaloInvoker getInstance() {
return getInstance("XML");
}

/**
* get the right instance for the protocol
* @param protocol
* @return
*/
public static BuffaloInvoker getInstance(String p) {
if (instance == null) {
instance = new BuffaloInvoker();
}
instance.protocol = p;
return instance;
}
</pre>
</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">三处</span><span lang="EN-US"><span style="font-family: Times New Roman;">BuffaloProtocal.getInstance()</span></span><span style="">的地方,改为:</span><span lang="EN-US"><span style="font-family: Times New Roman;">BuffaloProtocal.getInstance(protocol)</span></span><span style="">。</span></p>
<p>
</p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>

<p> </p>
6 楼 itstarting 2010-01-15  
<p><strong><span style="font-size: medium;"><span>改造:</span><span>BuffaloProtocal.java</span></span></strong></p>
<p> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>经进一步分析发现,</span><span lang="EN-US"><span style="font-family: Times New Roman;">BuffaloWorker</span></span><span>调用了</span><span lang="EN-US"><span style="font-family: Times New Roman;">BuffaloInvoker</span></span><span>实例的</span><span lang="EN-US"><span style="font-family: Times New Roman;">invoke</span></span><span>方法实现输入流的解析(</span><span lang="EN-US"><span style="font-family: Times New Roman;">unmarshal</span></span><span>)和输出结果对象的序列化(</span><span lang="EN-US"><span style="font-family: Times New Roman;">marshal</span></span><span>),而</span><span lang="EN-US"><span style="font-family: Times New Roman;">BuffaloInvoker</span></span><span>所使用的是</span></span><a name="OLE_LINK1"><span lang="EN-US"><span style="font-size: small; font-family: Times New Roman;">BuffaloProtocal</span></span></a><span><span style="font-size: small;">的实例。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>再看看</span><span lang="EN-US"><span style="font-family: Times New Roman;">BuffaloProtocal</span></span><span>实例的代码就知道,这里存在大量耦合</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span>协议的部分。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span><span style="font-size: small;">解耦在所难免。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span><span style="font-size: small;">此类改造由此产生了两个目标:</span></span></p>
<p class="MsoNormal"><span lang="EN-US"><span><span style="font-family: Times New Roman;"><span style="font-size: small;">1、</span><span style="font: 7pt ;">  </span></span></span></span><span><span style="font-size: small;">应该要能区别所用的协议;</span></span></p>
<p class="MsoNormal"><span lang="EN-US"><span><span style="font-family: Times New Roman;"><span style="font-size: small;">2、</span><span style="font: 7pt ;">  </span></span></span></span><span style="font-size: small;"><span>实例将根据不同的协议初始化对应的</span><span lang="EN-US"><span style="font-family: Times New Roman;">StreamReader</span></span><span>和</span><span lang="EN-US"><span style="font-family: Times New Roman;">StreamWriter</span></span><span>。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>为此,首先增加了一个类的私有变量</span><span lang="EN-US"><span style="font-family: Times New Roman;">protocol</span></span><span>,用以表达所用协议(这个变量后续需要重构,谁知道后续还会冒出什么协议呢,对吧):</span></span></p>
<p><strong></strong> </p>
<p> </p>
<p> </p>
<pre>private String protocol = "XML";</pre>
<p> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span><span style="font-size: small;">废弃原有获取单例的方式并增加新的需要明确协议的方式:</span></span></p>
<p><span><span style="font-size: small;">
<pre name="code" class="java"> /**
* get the instance for default xml protocol
* @return
* @deprecated use getInstance(String protocol) instead
*/
public static BuffaloProtocal getInstance() {
return getInstance("XML");
}
/**
* get the right instance for the protocol
* @param protocol
* @return
*/
public static BuffaloProtocal getInstance(String p){
if (instance == null) {
instance = new BuffaloProtocal();
}
instance.protocol = p;
return instance;
}
</pre>
<span><font size="3">
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</font></span></span>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span><span style="font-size: small; color: #ff0000;"><strong>注意:我感觉上面这样写会有线程安全问题,不知道各位怎么看?</strong></span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>改变原有的</span><span lang="EN-US"><span style="font-family: Times New Roman;">marshal</span></span><span>和</span><span lang="EN-US"><span style="font-family: Times New Roman;">unmarshal</span></span><span>方法中直接绑定</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML SteamReader</span></span><span>及</span><span lang="EN-US"><span style="font-family: Times New Roman;">SteamWriter</span></span><span>的做法,通过</span><span lang="EN-US"><span style="font-family: Times New Roman;">if…else</span></span><span>来判断(先这样,看以后再重构):</span></span></p>
<p><span style="font-size: small;"><span>
<pre name="code" class="java">public void marshall(Object value, Writer writer) {
StreamWriter steamWriter;
if(protocol.equalsIgnoreCase("JSON")){
steamWriter = new JsonStreamWriter(writer);
}else{
steamWriter = new FastStreamWriter(writer);
}
marshallingStrategy.marshal(value, converterLookup, steamWriter);
}

public BuffaloCall unmarshall(Reader reader) {
StreamReader steamReader;
if(protocol.equalsIgnoreCase("JSON")){
steamReader = new JsonStreamReader(reader);
}else{
steamReader = new FastStreamReader(reader);
}
return marshallingStrategy.unmarshal(steamReader, converterLookup);
}

public BuffaloCall unmarshall(InputStream inputStream) {
StreamReader steamReader;
if(protocol.equalsIgnoreCase("JSON")){
steamReader = new JsonInputStreamReader(inputStream);
}else{
steamReader = new FastInputStreamReader(inputStream);
}
return marshallingStrategy.unmarshal(steamReader, converterLookup);
}
</pre>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</span></span>
</p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>

<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span><span style="font-size: small;">注意了,这里我先定义了三个类,分别为:</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">JsonStreamReader</span></span><span>——实现</span><span lang="EN-US"><span style="font-family: Times New Roman;">io/SteamReader</span></span><span>接口,用以解析</span><span lang="EN-US"><span style="font-family: Times New Roman;">JSON</span></span><span>输入流;</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">JsonInputStreamReader</span></span><span>——这个得再确认,原先</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span>协议下的说法是</span><span lang="EN-US"><span style="font-family: Times New Roman;">Opera</span></span><span>下有编码问题才做的,先照葫芦画瓢改造;</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">JsonStreamWriter</span></span><span>——实现</span><span lang="EN-US"><span style="font-family: Times New Roman;">io/SteamWriter</span></span><span>接口,用以序列化执行结果并输出</span><span lang="EN-US"><span style="font-family: Times New Roman;">JSON</span></span><span>流</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21.1pt;"><span style="font-size: small;"><strong><span>上述三个类将是本次扩展实现以支持</span><span lang="EN-US"><span style="font-family: Times New Roman;">JSON</span></span></strong><strong><span>协议的重中之重。</span></strong></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21.1pt;"> </p>
5 楼 itstarting 2010-01-15  
<p><span><span>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span><span><span style="font-family: Verdana;">      经昨天晚上的分析</span></span>,上述改动不应在</span><span lang="EN-US"><span style="font-family: Times New Roman;">ApplicationServlet</span></span><span>做,而应该在</span><span lang="EN-US"><span style="font-family: Times New Roman;">AbstractRequestWorker.java</span></span><span>。即上述在</span><span lang="EN-US"><span style="font-family: Times New Roman;">ApplicationServlet</span></span><span>的修订无效。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>      此时要改造的是<span lang="EN-US">AbstractRequestWorker.java,<span>即把原先打算在</span><span lang="EN-US">ApplicationServlet</span><span>的改动,调整在此类做:</span></span></span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span></span></span></p>
<p><span style="font-size: small;">
<p><span><span lang="EN-US"><span>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span>      定义一个类全局的变量:</span></p>
</span></span></span>
</p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;">
</p>
<table class="MsoTableGrid" style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0"><tbody><tr style="">
<td style="padding-right: 5.4pt; padding-left: 5.4pt; padding-bottom: 0cm; width: 426.1pt; padding-top: 0cm; background-color: transparent;" width="568" valign="top">
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 0cm;"><strong><span style='font-size: 10pt; color: #7f0055; line-height: 120%; font-family: "Courier New";' lang="EN-US">private</span></strong><span style='font-size: 10pt; color: black; line-height: 120%; font-family: "Courier New";' lang="EN-US"> </span><strong><span style='font-size: 10pt; color: #7f0055; line-height: 120%; font-family: "Courier New";' lang="EN-US">static</span></strong><span style='font-size: 10pt; color: black; line-height: 120%; font-family: "Courier New";' lang="EN-US"> </span><strong><span style='font-size: 10pt; color: #7f0055; line-height: 120%; font-family: "Courier New";' lang="EN-US">final</span></strong><span style='font-size: 10pt; color: black; line-height: 120%; font-family: "Courier New";' lang="EN-US"> String </span><em><span style='font-size: 10pt; color: #0000c0; line-height: 120%; font-family: "Courier New";' lang="EN-US">PROTOCOL_ATTR</span></em><span style='font-size: 10pt; color: black; line-height: 120%; font-family: "Courier New";' lang="EN-US"> = </span><span style='font-size: 10pt; color: #2a00ff; line-height: 120%; font-family: "Courier New";' lang="EN-US">"X-Buffalo-Protocol"</span><span style='font-size: 10pt; color: black; line-height: 120%; font-family: "Courier New";' lang="EN-US">;</span></p>
</td>
</tr></tbody></table></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">新增</span><span lang="EN-US"><span style="font-family: Times New Roman;">getProtocol</span></span><span style="">的方法:</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;">
</p>
<table class="MsoTableGrid" style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0"><tbody><tr style="">
<td style="padding-right: 5.4pt; padding-left: 5.4pt; padding-bottom: 0cm; width: 426.1pt; padding-top: 0cm; background-color: transparent;" width="568" valign="top">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><span style='font-size: 10pt; color: #3f5fbf; font-family: "Courier New";' lang="EN-US">/**</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><span style='font-size: 10pt; color: #3f5fbf; font-family: "Courier New";' lang="EN-US"><span style=""> </span>* Get the protocol</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><span style='font-size: 10pt; color: #3f5fbf; font-family: "Courier New";' lang="EN-US"><span style=""> </span>* </span><strong><span style='font-size: 10pt; color: #7f9fbf; font-family: "Courier New";' lang="EN-US">@return</span></strong><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><span style='font-size: 10pt; color: #3f5fbf; font-family: "Courier New";' lang="EN-US"><span style=""> </span>*/</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">protected</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> String getProtocol(){</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style="">    </span></span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">//get the buffalo protocol from request header ‘</span><span style='font-size: 10pt; color: #2a00ff; font-family: "Courier New";' lang="EN-US">X-Buffalo-Protocol</span><span style='font-size: 10pt; color: #3f7f5f; font-family: "Courier New";' lang="EN-US">’</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 0cm; line-height: normal; text-align: left;" align="left"><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"><span style="">    </span></span><strong><span style='font-size: 10pt; color: #7f0055; font-family: "Courier New";' lang="EN-US">return</span></strong><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US"> (String) RequestContext.<em>getContext</em>().getHttpRequest().getHeader(</span><em><span style='font-size: 10pt; color: #0000c0; font-family: "Courier New";' lang="EN-US">PROTOCOL_ATTR</span></em><span style='font-size: 10pt; color: black; font-family: "Courier New";' lang="EN-US">);</span><span style='font-size: 10pt; font-family: "Courier New";' lang="EN-US"></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 0cm;"><span style='font-size: 10pt; color: black; line-height: 120%; font-family: "Courier New";' lang="EN-US">}</span></p>
</td>
</tr></tbody></table>
<p><span style="">
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">在这个抽象类做有什么好处呢?能确保所有</span><span lang="EN-US"><span style="font-family: Times New Roman;">Worker</span></span><span style="">都能共享此变量。</span></p>
</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">拿到以后怎么用呢?目前并没有现成的办法。</span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="">后面应该需要较大的改造力度了。</span></p>
<span>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<font size="3"><span><span lang="EN-US"><span>
<p> </p>
</span></span></span></font></span>

<p> </p>
4 楼 itstarting 2010-01-14  
我很好奇的看到有8名高手投了隐藏贴,如果大家已经有好的实现,不妨发给我学习参考,谢谢!
3 楼 itstarting 2010-01-14  
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span style="">当然了,</span><span lang="EN-US"><span style="font-family: Times New Roman;">Buffalo.Call</span></span><span style="">还需要增加</span><span lang="EN-US"><span style="font-family: Times New Roman;">json</span></span><span style="">方法,类似于</span><span lang="EN-US"><span style="font-family: Times New Roman;">xml</span></span><span style="">,用以产生</span><span lang="EN-US"><span style="font-family: Times New Roman;">json</span></span><span style="">的请求。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span style="">为了方便,这里先写死了,先把</span><span lang="EN-US"><span style="font-family: Times New Roman;">simpleService.divide</span></span><span style="">走通再说:</span></span></p>
<pre name="code" class="js"> // parse the call to json format
// TODO: now just for test
json: function(){
                this._objects = [];
var jsonstr = "{'buffalo-call':[{'method':divide},{'double':1},{'double':2}]}";
return jsonstr;
},
</pre>
<p> </p>
<p> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span style="">这回轮到修订后端的入口</span><span lang="EN-US"><span style="font-family: Times New Roman;">Servlet</span></span><span style="">,即</span><span lang="EN-US"><span style="font-family: Times New Roman;">ApplicationServlet.java</span></span><span style="">。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style=""><span style="font-size: small;">定义一个类全局的变量:</span></span></p>
<pre name="code" class="java">private static final String PROTOCOL_ATTR = "X-Buffalo-Protocol";</pre>
<p> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span style="">在</span><span lang="EN-US"><span style="font-family: Times New Roman;">doRequest</span></span><span style="">中获得协议名称:</span></span></p>
<p><span style="font-size: small;"><span style="">
<pre name="code" class="java">//get the buffalo protocol
String buffaloProtocol = (String) request.getHeader(PROTOCOL_ATTR);
LOG.debug("using protocol: " + buffaloProtocol);
</pre>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</span></span>
</p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>

<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
2 楼 itstarting 2010-01-14  
<p><span><span style="font-size: small;">
<p><span><span style="font-size: small;">
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;">首当其冲的是,得搞定入口问题。如何确保双协议的同时支持是关键?</p>
</span></span></p>
</span></span></p>
<p>  </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span><span style="font-size: small;">首先定义一个全局变量:</span></span></p>
<pre name="code" class="js">Buffalo.DefaultProtocol="JSON";//XML or JSON</pre>
<p>  </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>协议的选择应该考虑基于每次的</span><span lang="EN-US"><span style="font-family: Times New Roman;">remoteCall</span></span><span>,而不是全部要么这样要么那样。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>此时需要扩展</span><span lang="EN-US"><span style="font-family: Times New Roman;">Buffalo.Call</span></span><span>,新增参数</span><span lang="EN-US"><span style="font-family: Times New Roman;">protocol</span></span><span>,并新增两个方法:</span></span></p>
<p><span style="font-size: small;"><span>
<pre name="code" class="js">initialize: function(methodname){
……
this.protocol = Buffalo.DefaultProtocol;
},

setProtocol : function(protocol) {
if (protocol!=null &amp;&amp; (protocol=='XML' || protocol=='JSON')) {
this.protocol = protocol;
}
},

getProtocol : function() {
return this.protocol;
},
</pre>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</span></span>
</p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>

<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span>然后在正式发出后台请求的地方(</span><span lang="EN-US"><span style="font-family: Times New Roman;">Buffalo._remoteCall</span></span><span>),通过</span><span lang="EN-US"><span style="font-family: Times New Roman;">RequestHeader</span></span><span>传递这个协议指令:</span></span></p>
<p><span style="font-size: small;"><span>
<pre name="code" class="js">try {
this.transport.open("POST", url, this.async);
this.transport.setRequestHeader("X-Buffalo-Version", Buffalo.VERSION);
this.transport.setRequestHeader("X-Buffalo-Protocol", buffaloCall.getProtocol());
this.transport.setRequestHeader("Content-Type", "text/xml;charset=utf-8");
this.transport.send(sendingXML);//this.transport.send(buffaloCall.xml());
} catch (e) {
this.events.onError(this.transport);
this.events["onLoading"](false);
return;
}
</pre>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>
</span></span>
</p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"> </p>

<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"></span></p>
<p><span style="font-size: small;"><span>这样,这个指令就可以从后端的入口</span><span lang="EN-US"><span style="font-family: Times New Roman;">Servlet</span></span><span>(</span><span lang="EN-US"><span style="font-family: Times New Roman;">ApplicationServlet</span></span><span>)获得。</span></span></p>
1 楼 itstarting 2010-01-14  
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span lang="EN-US"><span style="font-family: Times New Roman;">Buffalo</span></span><span style="">原有的架构,就这个扩展的角度而言,基本还是合理到位的,核心部分均通过简约的接口定义,与具体实现进行了分离,内部也合理的运用了较好的设计模式实践。所有这些,均为实现</span><span lang="EN-US"><span style="font-family: Times New Roman;">JSON</span></span><span style="">协议的扩展和改造打下良好的基础。</span></span></p>
<p class="MsoNormal" style="margin: 6pt 0cm 0pt; text-indent: 21pt;"><span style="font-size: small;"><span style="">考虑到原有</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="">协议已经非常成熟稳定,整体实现基于</span><span lang="EN-US"><span style="font-family: Times New Roman;">JSON协议</span></span><span style="">的扩展思路是这样的:</span></span></p>
<p class="MsoNormal" style=""><span style="" lang="EN-US"><span style=""><span style="font-family: Times New Roman;"><span style="font-size: small;">1、</span><span style='font: 7pt "Times New Roman";'>  </span></span></span></span><span style="font-size: small;"><span style="">改造原有与</span><span lang="EN-US"><span style="font-family: Times New Roman;">XML</span></span><span style="">协议耦合过高的代码;</span></span></p>
<p class="MsoNormal" style=""><span style="" lang="EN-US"><span style=""><span style="font-family: Times New Roman;"><span style="font-size: small;">2、</span><span style='font: 7pt "Times New Roman";'>  </span></span></span></span><span style="font-size: small;"><span style="">实现基于</span><span lang="EN-US"><span style="font-family: Times New Roman;">JSON</span></span><span style="">的</span><span lang="EN-US"><span style="font-family: Times New Roman;">io</span></span><span style="">读写接口;</span></span></p>
<p class="MsoNormal" style=""><span style="font-size: small;"><span style="">3、重新编写/扩展客户端序列化、反序列化机制。</span></span></p>
<p> </p>

相关推荐

    基于java的国产Ajax框架 Buffalo.zip

    而在Java领域,虽然有如jQuery、Prototype等JavaScript库提供了Ajax功能,但针对国内开发者需求,一些国产Ajax框架应运而生,其中就包括了Buffalo框架。 Buffalo框架是专为Java开发者设计的轻量级、高性能的Ajax...

    基于java的开发源码-国产Ajax框架 Buffalo.zip

    **Ajax框架Buffalo详解** Ajax(Asynchronous JavaScript and XML)技术是现代Web开发中的关键组成部分,它使得网页能够实现异步更新,提升了用户体验。而基于Java的Ajax框架Buffalo则是国内开发者为解决这一需求而...

    国产Ajax框架 Buffalo源码

    无论服务器返回的是XML、JSON还是其他格式,框架都能自动解析并转换为JavaScript对象,供前端使用。这得益于框架内建的强大的数据解析库,它能处理各种常见的数据格式,大大简化了数据处理的步骤。 此外,Buffalo还...

    ajax十大框架

    根据给定的信息,我们可以梳理出以下关于Ajax框架的相关知识点: ### 1. Prototype Prototype 是一个功能强大的 JavaScript 库,它扩展了 DOM API,并提供了一系列便捷的方法和对象。该库非常优雅,支持丰富的 ...

    AJAX(Buffalo)配置使用

    AJAX,全称为Asynchronous JavaScript and XML,是一种在无需刷新整个网页的情况下,能够更新部分网页内容的技术。在实际开发中,它极大地提升了用户体验,让用户在交互时感觉更加流畅。本篇将详细介绍AJAX的配置与...

    buffalo-2.0.1-bin.zip

    Buffalo-2.0.1-bin.zip是一款专为开发者设计的高效、易用且功能丰富的Ajax工具框架。Ajax,即Asynchronous JavaScript and XML,是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。Buffalo框架正是...

    10大js框架 javaScript框架

    在探讨“十大JavaScript框架”这一主题时,我们深入解析了多个关键框架,这些框架在现代Web开发中扮演着至关重要的角色。以下是对每个框架的详细分析: ### 1. Prototype Prototype是最早一批的JavaScript库之一,...

    struts2 buffalo 资源

    "Struts2 Buffalo 资源"这个标题可能指的是一个包含了Struts2框架特定版本(2.0.14)以及与其相关的Buffalo AJAX技术框架的资源集合。 Buffalo框架是Struts2的一个插件,专门用于处理AJAX请求。它简化了在Struts2...

    2005.08.06.Ajax.pdf

    - **数据格式**:虽然名称中包含XML,但实际上Ajax支持多种数据格式,如XML、JSON等。 - **应用场景**:Ajax广泛应用于各种需要实时交互的应用场景,如即时通讯、在线表单提交等。 #### Ajax技术的发展背景 2005年...

    java开源包3

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包1

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包11

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包2

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包6

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包5

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包10

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包4

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包8

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包7

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

    java开源包9

    jSIP这个Java包目标是用Java实现SIP(SIP:Session Initiation Protocol)协议及SIP协议的其它扩展部 分。 Java表达式语法解析库 parboiled parboiled 是一个纯Java库提供了一种轻量级,易于使用,功能强大和优雅的PEG...

Global site tag (gtag.js) - Google Analytics