现在来看看web.xml的配置。web.xml中一般是这样来写的:
一般来说,你只需要一个dwr.xml文件,并且放置在默认的位置:WEB-INF/dwr.xml,如上面配置,就可以很好的工作了。不过现实中的项目可能会很复杂,如: 一个使用J2EE的安全机制的例子:
在DWR2.0中.你也可以这样来配置。
要使用DWR的标注,你需要在web.xml中配置不同的DWR控制器。 servlet参数classes定义的时可以标注的类的全名,这些名字用逗号分割。 要使一个简单的class可以成为远程访问类,你需要使用 (Create和)RemoteMethod标注。 没有被@RemoteMethod标注的方法不能被远程访问。 要在Javascript使用不同于类型的名字,使用@Create标注的 name 属性。 要使一个简单的bean类可以被远程访问, 使用 (Convert和)RemoteProperty标注: 要使用复杂的转换器,使用@Convert标注的 converter 属性。初始配置
<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>
远程访问类
@Create public class RemoteFunctions { @RemoteMethod public int calculateFoo() {return 42; } }
@Create(name="Functions") public class RemoteFunctions { }
对象转换
@Convert public class Foo { @RemoteProperty private int foo; public int getFoo() {return foo; } @RemoteProperty public int getBar() { return foo * 42; } }