zl程序教程

您现在的位置是:首页 >  其它

当前栏目

45dwr - DWR Annotations

DWR
2023-09-11 14:15:41 时间

DWR 标注是用来代替 dwr.xml 或者与其一同工作的。

初始配置

要使用 DWR 的标注,你需要在 web.xml 中配置不同的 DWR 控制器。

<servlet>
	<description>DWR controller servlet</description>
	<servlet-name>DWR controller servlet</servlet-name>
	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
	<init-param>
		<param-name>classes</param-name>
		<param-value>com.example.RemoteFunctions, com.example.RemoteBean</param-value>
	</init-param>
</servlet>

servlet 参数 classes 定义的时可以标注的类的全名,这些名字用逗号分割。

远程访问类

要使一个简单的 class 可以成为远程访问类,你需要使用@Create 和@RemoteMethod 标注。

@Create
public class RemoteFunctions {
	@RemoteMethod
	public int calculateFoo() {
	return 42;
	}
}

没有被@RemoteMethod 标注的方法不能被远程访问。
要在 Javascript 使用不同于类型的名字,使用@Create 标注的 name 属性。

@Create(name = "Functions")
public class RemoteFunctions {
}

对象转换

要使一个简单的 bean 类可以被远程访问, 使用@Convert 和@RemoteProperty 标注:

@Convert
public class Foo {
	@RemoteProperty
	private int foo;
	
	public int getFoo() {
	return foo;
	}
	@RemoteProperty
	public int getBar() {
	return foo * 42;
	}
}

要使用复杂的转换器,使用@Convert 标注的 converter 属性。