Openlaszlo调用JavaRPC和JAVA类通信

JavaRPC允许Laszlo客户端远程调用服务端的JAVA类和方法。在laszlo中使用<javarpc>标签来声明一个JavaRPC对象:

<javarpc classname="..."
attributename="..."
scope="[session|webapp|none]"
loadoption="[loadcreate|loadonly|create]"
createargs="..."
objectreturntype="[pojo|javabean]"
autoload="[true|false]"
secure="[true|false]"
secureport="...">

classname(String): 必需。指定需要访问的JAVA类名
attributename(String):使用服务端对象时的关键词。当scope为session或webapp时使用。
Scope(String): 必需。在session,webapp和none三个中选一个。
Loadoption(String): loadcreate,loadonly和create三个中任选一个。Loadcreate:如果服务器上存在该对象时加载,如果不存在,则在加载前自动创建该对象。Loadonly:只负责加载对象,如果该对象不存在,则返回一个错误。Create:在加载对象时总是在服务器端创建该对象。默认为loadcreate。
Createargs(Array): 只有当loadoption为loadcreate或create时才有效。Createargs
数组包含一些变量,这些变量用来在服务端创建对象时使用。默认为null。
Objectreturntype(String): pojo和javabean中任选一个。如果从服务端返回一个对象,使用pojo只返回该对象中所有声明为public的成员变量。使用javabean将返回含有getXXX()方法的成员变量。
Autoload(Boolean): true或false。True:在初始化时自动加载指定的对象。False:需要在使用对象前调用load()方法。默认为true。
Secure(Boolean): true或false。如果选择true的话,会在服务端和客户端之间建立一个安全的连接。
Secureport(Number):: 只有才secure为true时才有效。
如:
<javarpc name=”returnsun” scope=”none” class=”RetuenSum”>通过returnsun来建立javarpc和java之间的桥梁。其中name就是指定一个rpc的属性(attributename)。再通过remotecall调用java类的方法:
<remotecall name=”getSun” funcname="getSun">
<param id=”a” name=”a” value=”’’”/>
<param id=”b” name=”b” value=”’’”/>
<method event="ondata" args="sum"/>
</remotecall>

说明:在使用javarpc前,首先要将JAVA类定义为安全的,如下:
<security>
<allow>
<pattern>^com.sum.ReturnSum</pattern>
</allow>
</security>

funcname对应java类的方法名称。

如果调用的方法带有参数,remotecall需用<param>来传递参数,这个参数对应JAVA方法中的形参。在传递时,remotecall会根据param的位置和JAVA方法形参的位置匹配。
<method event="ondata" args="sum"/>,使用args来接收JAVA方法的返回值,无需定义数据类型。
下面通过一个小DEMO来演示一下使用JavaRPC和JAVA进行通信:
TypesExample类:
package examples;
import java.util.Vector;
import java.util.Hashtable;

public class TypesExample {

public static String passInteger(int i) {
return "got integer parameter: " + i;
}

public static String passDouble(double d) {
return "got double parameter: " + d;
}

public static String passBoolean(boolean b) {
return "got boolean parameter: " + b;
}

public static String passClientArray(Vector v) {
return "got vector parameter: " + v;
}

public static String passClientObject(Hashtable t) {
return "got hashtable parameter: " + t;
}
}

LZX文件 demo.lzx
<canvas debug="true" height="300" width="800">
<debug x="250" y="10" width="500" height="275" />
<security>
<allow>
<pattern>^examples.TypesExample</pattern>
</allow>
</security>
<!-- See WEB-INF/classes/TypesExample.java for java source. -->
<javarpc name="types_example_rpc" scope="none"
classname="examples.TypesExample">
<method event="onload">
<!-- Set buttons visible only after JavaRPC object loads -->
canvas.buttons.setAttribute('visible', true);
</method>
<method event="ondata" args="res">
Debug.write('(types ondata) response is:', res);
</method>
<method event="onerror" args="errmsg">
Debug.write('(types onerror) error:', errmsg);
</method>
<!-- Declaratively pass an integer. -->
<remotecall funcname="passInteger">
<param value="42" />
</remotecall>
<!-- Declaratively pass a double. Note that we name this function pd1
because we have multiple remotecall declarations that call
passDouble but with different parameters. -->
<remotecall name="pd1" funcname="passDouble">
<param value="42.1" />
</remotecall>
<!-- Declaratively pass a double with 0 decimal. The 0 decimal will
truncate and the number will become an integer type when it reaches
the server. This call will fail. -->
<remotecall name="pd2" funcname="passDouble">
<param value="42.0" />
</remotecall>
<!-- Declaratively pass a double with 0 decimal. Wrapping the double in
DoubleWrapper will ensure the value will remain a double when
reaching the server. -->
<remotecall name="pd3" funcname="passDouble">
<param>
<method name="getValue">
return new LzRPC.DoubleWrapper(42.0);
</method>
</param>
</remotecall>
</javarpc>
<view name="buttons" visible="false" layout="spacing: 10">
<button text="pass integer"
onclick="types_example_rpc.passInteger.invoke()" />
<button text="pass double"
onclick="types_example_rpc.pd1.invoke()" />
<button text="pass double (will fail)"
onclick="types_example_rpc.pd2.invoke()" />
<button text="pass double w/LzRPC.DoubleWrapper"
onclick="types_example_rpc.pd3.invoke()" />
<button text="pass boolean"
onclick="this.passBoolean.invoke()">
<!-- This is a way to declare a remotecall closer to where it's being
used. The remotecontext must be set. -->
<remotecall funcname="passBoolean"
remotecontext="$once{ types_example_rpc }">
<param value="true" />
</remotecall>
</button>
<button text="pass array" onclick="this.passArray.invoke()">
<remotecall name="passArray" funcname="passClientArray"
remotecontext="$once{ types_example_rpc }">
<param value="[1, 'a string', 4.5, false]" />
</remotecall>
</button>
<button text="pass hash" onclick="this.passObject.invoke()">
<remotecall name="passObject" funcname="passClientObject"
remotecontext="$once{ types_example_rpc }">
<param
value="{ a: 1, b: 3.14159, c: 'a string value', d: true}">
</param>
</remotecall>
</button>
</view>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值