java使用dwr

 

DWR全称:Direct Web Remoting

即在js前端中使用远程服务器端的java方法。

 

DWR是方便使用AJAX连接JS和JAVA的的一个框架,把服务器端 Java 对象的方法公开给 JavaScript 代码
如果是用dwr2.0的jar包,还需要同时导入log4j.jar和commons-loggin.jar,勿忘!!
web.xml和dwr.xml放在WEB-INF下!

web.xml的配置如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>
			org.directwebremoting.servlet.DwrServlet
		</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>scriptCompressed</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>crossDomainSessionSecurity</param-name>
			<param-value>false</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>

</web-app>

 

 dwr.xml的配置如下:

 

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "http://getahead.org/dwr/dwr20.dtd">

<dwr>
	<allow>
		
		<!--通知栏处理-->
		<create creator="new" javascript="Notices" scope="request">
			<param name="class" value="com.xxx.dwr.Notices" />
		</create>

	</allow>
</dwr>
 

js文件引用:

<script type="text/javascript" src="<%= request.getContextPath()%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%= request.getContextPath()%>/dwr/interface/Notices.js"></script>
 

js调用方法:

Notices.methodA('arg0','arg1',function(returnVal){
     
    //回调函数内部方法,对java方法的返回值进行处理

}
);
 

 

你可能感兴趣的:(java)