当前位置: 代码迷 >> Web前端 >> 应用DWR完成HelloWorld
  详细解决方案

应用DWR完成HelloWorld

热度:296   发布时间:2012-10-15 09:45:25.0
使用DWR完成HelloWorld.
http://www.iteye.com/topic/904488
一、到DWR的官方网站上下载dwr.jar包。将其放到项目的lib目录下。开始做第一个dwr程序。

二、web.xml配置如下:

Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
      
    <display-name>dwrtest</display-name> 
    <servlet> 
        <servlet-name>dwr-invoke</servlet-name> 
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> 
        <init-param> 
            <param-name>debug</param-name> 
            <param-value>true</param-value> 
        </init-param> 
    </servlet> 
      
    <servlet-mapping> 
        <servlet-name>dwr-invoke</servlet-name> 
        <url-pattern>/dwr/*</url-pattern> 
    </servlet-mapping> 
      
  <welcome-file-list> 
    <welcome-file>Index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app> 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>dwrtest</display-name>
<servlet>
<servlet-name>dwr-invoke</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>

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

  <welcome-file-list>
    <welcome-file>Index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


三、在web.xml同目录下新建dwr.xml文件,内容如下:

Xml代码
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> 
      
<dwr> 
    <allow> 
        <create javascript="JDate" creator="new"> 
            <param name="class" value="java.util.Date"/> 
        </create> 
        <create javascript="Hello" creator="new"> 
            <param name="class" value="com.dwr.HelloWorld"/> 
        </create> 
    </allow> 
</dwr>   

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
   
<dwr>
<allow>
<create javascript="JDate" creator="new">
<param name="class" value="java.util.Date"/>
</create>
<create javascript="Hello" creator="new">
<param name="class" value="com.dwr.HelloWorld"/>
</create>
</allow>
</dwr>  

四、Index.jsp内容如下:

Php代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
      
    <title>DWR's  HelloWorld</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
    <!--  
    <link rel="stylesheet" type="text/css" href="styles.css">  
    -->  
    <script type='text/javascript' src='dwr/interface/Hello.js'></script>  
    <script type='text/javascript' src='dwr/engine.js'></script>  
    <script type='text/javascript' src='dwr/util.js'></script>  
    <script type='text/javascript' src='hello.js'></script>  
      
  </head>  
    
  <body>  
    <input id="user" type="text"/><input type="button" value="您好" onClick="hello()"><div id="result"></div>  
  </body>  
</html> 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>DWR's  HelloWorld</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">   
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='dwr/interface/Hello.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type='text/javascript' src='hello.js'></script>

  </head>
 
  <body>
   <input id="user" type="text"/><input type="button" value="您好" onClick="hello()"><div id="result"></div>
  </body>
</html>

五、在Index.jsp同目录下新建hello.js文件,内容如下:

Js代码
function hello(){  
    var user = $('user').value;  
    Hello.sayHello(user,callback);  
}  
 
function callback(msg){  
      
    if (typeof window['DWRUtil'] == 'undefined')  
        window.DWRUtil = dwr.util;   
    DWRUtil.setValue('result',msg);  


function hello(){
var user = $('user').value;
Hello.sayHello(user,callback);
}

function callback(msg){

if (typeof window['DWRUtil'] == 'undefined')
    window.DWRUtil = dwr.util;
DWRUtil.setValue('result',msg);
} 六、部署运行程序,我的可以看到结果。
  相关解决方案