以前用過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
有貓相伴的日子 閱讀(2246)
評論(5) 編輯 收藏 所屬分類:
dwr