以前用过dwr1做过一些简单的例子,现在想认真深入的了解dwr2并希望以后有机会用过她。在我眼里dwr2是ajax世界的大美女,她清纯、可爱,最讨J2EE程序员喜欢。
DWR嵌入到现有的J2EE(Web)项目的的配置
1、把dwr.jar文件拷贝到项目的lib目录下。
2、修改项目的web.xml文件,在文件中加入以下代码
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
3、在WEB-INF目录中建立dwr.xml文件,例如:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<init></init>
<allow>
<create creator="new" javascript="dwrbo">
<param name="class" value="com.dwr.DwrBO"/>
</create>
</allow>
</dwr>
以上dwr.xml文件内容还是比较好理解。就是发布一个类,一个类发布了,那么就可以通过js 的代码来调用这个类里的方法了。
4、com.dwr.DwrBO类的内容如下:
package com.dwr;
import org.directwebremoting.WebContextFactory;
import uk.ltd.getahead.dwr.ExecutionContext;
public class DwrBO {
public String getInclude()throws Exception{
return WebContextFactory.get().forwardToString("/a.jsp");
}
public String getServerInfo(){
return WebContextFactory.get().getServletContext().getServerInfo()+
"Run on JDK: "+System.getProperty("java.specification.version")+
"Useing DWR :"+ExecutionContext.get().getVersion();
}
public int[] getNumbers(boolean big){
if(big){
System.out.println("big==true");
return new int[]{1000,2000,3000,4000};
}else{
System.out.println("big==false");
return new int[]{1,2,3,4,5,6,7,8,9,10};
}
}
}
这实际上是dwr官方网站上的例子.
5、页面调用代码
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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">
<style type="text/css">
<!--
#Layer1 {
position:absolute;
left:47px;
top:24px;
width:677px;
height:31px;
z-index:1;
}
#context {
position:absolute;
left:45px;
top:100px;
width:679px;
height:100px;
z-index:2;
background-color: #f8f8f8;
clip: rect(10px,auto,auto,auto);
margin-top: 10px;
}
#version {
position: relative;
top: 200px;
width: 200px;
}
#Layer2 {
position:absolute;
left:26px;
top:247px;
width:485px;
height:76px;
z-index:3;
background-color: f2f2f2;
border: 1px solid #f4f4f4;
}
body {
font-size: 12px;
}
-->
</style>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/dwrbo.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'></script>
<SCRIPT language="javascript">
function loadinfo(data) {
DWRUtil.setValue("context", data);
}
function test(){
dwrbo.getInclude(loadinfo);
}
function loadinfoversion(data){
DWRUtil.setValue("version",data);
}
function showversion(){
dwrbo.getServerInfo(loadinfoversion);
}
function createList(data){
DWRUtil.removeAllOptions("numlist");
DWRUtil.addOptions("numlist",data);
}
function update(){
alert("xxxs");
dwrbo.getNumbers(createList,${"bigselect"}.checked);
}
</SCRIPT>
</head>
<body>
<div id="Layer1">
<input type="submit" name="Submit" value="测试" onClick="test()">
<input type="submit" name="Submit" value="版本信息" onClick="showversion()">
</div>
<div id="context">
</div>
<div id="version">
</div>
<div id="Layer2">
<input type="checkbox" id=bigselect onclick="update()">数字类型<br>
<select id=numlist></select>
</div>
</body>
</html:html>
这只是一个简单的例子,至于具体的技术说明就不说了,因为很简单。把它贴出来当作一个笔记吧,希望能进一步学习DWR
posted on 2006-09-16 16:11
有猫相伴的日子 阅读(2239)
评论(5) 编辑 收藏 所属分类:
dwr