DWR使用简介

更详细的(如消息推送)可以参考http://wang371134086.iteye.com/blog/1849716的博客
下面记录下最简单的DWR运用
1、导入drw.jar
2、web.xml配置
<servlet>
<display-name>DWR Servlet</display-name>
	<servlet-name>dwr-invoker</servlet-name>
	<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!--<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> -->
	<init-param>  
            <param-name>crossDomainSessionSecurity</param-name>  
               <param-value>false</param-value>  
        </init-param>  
        <init-param>  	
          <param-name>allowScriptTagRemoting</param-name>  
          <param-value>true</param-value>  
        </init-param>  
        <init-param>  
          <param-name>classes</param-name>  
          <param-value>java.lang.Object</param-value>  
        </init-param>   
        <init-param>  
            <param-name>activeReverseAjaxEnabled</param-name>  
            <param-value>true</param-value>  
        </init-param>  
        <init-param>  
           <param-name>initApplicationScopeCreatorsAtStartup</param-name>  
           <param-value>true</param-value>  
        </init-param>  
        <init-param>  
            <param-name>maxWaitAfterWrite</param-name>  
            <param-value>3000</param-value>  
        </init-param>
        <init-param>  
            <param-name>logLevel</param-name>  
            <param-value>WARN</param-value>  
        </init-param>   
<init-param>
	     <param-name>debug</param-name>
	     <param-value>true</param-value>
	  </init-param>
	</servlet>
	<servlet-mapping>
	  <servlet-name>dwr-invoker</servlet-name>
	  <url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>


3、同级目录下dwr.xml配置
<dwr>  
     <allow>  
          <create creator="new" javascript="testDwr">  
            <param name="class" value="Test"/>  
         </create>  
         <!--
         <create creator="new" javascript="SendMessage">  
            <param name="class" value="SendMessage"/>  
         </create> 
         --> 
     </allow>  
</dwr>

4、test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<script type='text/javascript' src='../../dwr/engine.js'></script>  
<script type='text/javascript' src='../../dwr/util.js'></script>  
<script type='text/javascript' src='../../dwr/interface/testDwr.js'></script>
<script type="text/javascript">
function test(){
	testDwr.show();
};
</script>
</head>
<body>
	<div><button onclick="test()">click me</button></div>
</body>
</html>

5、后台Test类里的show方法
public class Test {
	public void show(){
		System.out.println("hello world");
	}	
}

你可能感兴趣的:(JavaScript,java,xml,servlet,DWR)