一个简单的dwr例子

项目架构中整合了dwr,所以平时也就照着用,以前搞过早忘了,一直没自己完整的敲个心里不爽啊,最近找个敲了下, 居然顺利跑起来,心情那个明媚啊

1.环境:eclips+Nitrox(myEclips插件也一样),dwr本版1.0(有点低了,不过手头有1.0包就用用,懒得去下了,原谅下把^_^...),建个web项目,不多说,直接上图,看机构,很清晰把:

一个简单的dwr例子_第1张图片

2.先看效果图,这样,心里有奔头,呵呵

效果大概讲下:输入内容点“你好”按钮,哈罗那段话后面就变,无刷新,鼠标移到图片上下面对应显示介绍,当然图片我自己找的,并不是什么书,就是那个意思了,你那么聪明能明白哦,o(∩_∩)o...

3.看代码

BOOK.java

 /**

 * 

 */

package test;



import java.util.Locale;

import java.util.ResourceBundle;



/**

 * @author gjy

 *

 */

public class Book {



 private ResourceBundle resource;

 Locale locale=new Locale("UTF-8");

 public Book(){

  resource=ResourceBundle.getBundle("resources/book",locale); //找对应资源文件

 }

 

 public String getDescription(String key){

  return resource.getString(key);      //根据获得的值取得对应资源文件中的描述,作为返回值

 }

 

 

}
Hello.java
/**

 * 

 */

package test;



/**

 * @author gjy

 *

 */

public class Hello {

	

	public String hello(String name){

		return "哈罗!"+name+"!您的第一个DWR";

	}

}
book.js
function getBookData(ele) {   

    //Book.java??getDescription??   

      Book.getDescription(ele.id, setBookData);}   

         

      //????   

function setBookData(d) {    

     DWRUtil.setValue('info', d);}   

        

function clearData() {    

     DWRUtil.setValue('info', '');} 
hello.js
 function hello() {    
       var user = $("user").value;     
       //$(id)??document.getElementById("id")   
         Hello.hello(user, callback);   
}   
function callback(msg) {    
              DWRUtil.setValue("result", msg);   
              }  
资源文件:book_zh_CN.properties
ajax = Ajax in action /u4E2D/u6587/u7248/u7684/u4ECB/u7ECD/u2026   

  

java = Java  /u5B66/u4E60/u7B14/u8BB0/u7684/u4ECB/u7ECD   

  

spring = Spring /u6280/u672F/u624B/u518C/u7684/u4ECB/u7ECD/u2026BlaBla... 
 
web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>



<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

	version="2.4">

	

	<display-name>Web Application</display-name>



	  <servlet>  

        <servlet-name>dwr-invoker</servlet-name>  

        <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>  

        <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>  

    

    <welcome-file-list>  

        <welcome-file>index.jsp</welcome-file>  

    </welcome-file-list>

</web-app>
其中:
<servlet-name>dwr-invoker</servlet-name>          
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>          
<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>
是对drw的配置一定要有的哦.
dwr.xmlpei文件


 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">

<dwr>

	<allow>

		<create creator="new" javascript="JDate">

			<param name="class" value="java.util.Date"/>

		</create>

		

		<create creator="new" javascript="Hello">

			<param name="class" value="test.Hello"/>

		</create>

		

		<create creator="new" javascript="Book" scope="application">

			<param name="class" value="test.Book"/>

		</create>

		

	</allow>



</dwr>
对对应类的配置。
index.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"  

    pageEncoding="UTF-8"%>   

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   

<html>   

<head>   

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/engine.js'></script>   

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/interface/JDate.js'></script>    

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/interface/Hello.js'></script>   

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/util.js'></script>   

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/interface/Book.js'></script>   

<script type="text/javascript" src="js/hello.js"></script>   

<script type="text/javascript" src="js/book.js"></script>   

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   

<title>第一个DWR程序</title>   

  

</head>   

<body>   

  

<table width="606" height="98" border="1" bordercolor="#bde3ff">   

    <tr>   

        <td width="223"><input id="user" type="text" /> <input   

            type="button" value="你好" onClick="hello();" /></td>   

  

    </tr>   

    <tr>   

        <td colspan="2">   

        <div id="result"></div>   

        </td>   

    </tr>   

</table>   

  

<div id="ajax" onmouseover="getBookData(this);"  

    onmouseout="clearData();"><a   

    href="http://www.gotop.com.tw/waweb2004/home/home.aspx?pg=HM010X&bn=AXP011800"><small><img   

    style="border: 0px solid; width: 80px; height: 110px; float: left;"  

    alt="Ajax in action 中文版" title="Ajax in action 中文版"  

    src="images/a.jpg" hspace="10" vspace="2"></small></a></div>   

       

<div id="spring" onmouseover="getBookData(this);"  

    onmouseout="clearData();"><a   

    href="http://www.gotop.com.tw/waweb2004/home/home.aspx?pg=HM010X&bn=ACL021000"><small><img   

    style="border: 0px solid; width: 80px; height: 110px; float: left;"  

    alt="Spring 技术手册" title="Spring 技术手册" src="images/b.jpg"  

    hspace="10" vspace="2"></small></a></div>   

       

<div id="java" onmouseover="getBookData(this);"  

    onmouseout="clearData();"><a   

    href="http://www.gotop.com.tw/waweb2004/home/home.aspx?pg=HM010X&bn=ACL020931"><small><img   

    style="border: 0px solid; width: 80px; height: 110px; float: left;"  

    alt="Java 学习笔记" title="Java 学习笔记"  

    src="images/a.jpg" hspace="10" vspace="2"></small></a></div>   

       

<br /><br /><br /><br /><br /><br /><br /><br />   

<div id="info"></div>   

</body>   

</html>  
 
这两个js是必须要引的啦
<script type="text/javascript" src="js/hello.js"></script>   

<script type="text/javascript" src="js/book.js"></script>  
 
 
这个几个是对应你dwr.xml中配置的对应的java类,javascript="Hello",这样知道找哪个类:
<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/interface/JDate.js'></script>    

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/interface/Hello.js'></script>   

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/util.js'></script>   

<script type='text/javascript'  

    src='<%=request.getContextPath() %>/dwr/interface/Book.js'></script>   
 
就这么多把,有需要的我也可以打个包发给你。。。
 

你可能感兴趣的:(JavaScript,java,spring,Ajax,DWR,border)