1、載入dwr的jar文件
下載地址:http://www.cnweblog.com/Files/jimmy/dwr.rar (將后綴名改為jar)
2、在web.xml中配制
<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>
3、在WEB-INF下建立dwr.xml和dwr20.dtd兩個文件
復(fù)制以下內(nèi)容到dwr20.dtd文件中,保存:
<? xml version="1.0" encoding="UTF-8" ?>
<!-- This is the DTD for DWR v2.x -->
<!--
Top level conviguration element.
-->
<! ELEMENT dwr (
(init?), (allow?), (signatures?)
) >
<!--
A list of all the classes to configure as part of dwr at startup time.
-->
<! ELEMENT init (
(creator | converter)*
) >
<!--
Define a new method of creating objects for use by Javascript.
We don't just allow access to any object and some may need special code to
get a reference to them.
-->
<! ELEMENT creator EMPTY >
<!--
@attr id The unique name by which create elements refer to us.
@attr class The fully qualified name of a class that implements Creator.
-->
<! ATTLIST creator
id ID #REQUIRED
class CDATA #REQUIRED
>
<!--
Define a new way of converting between javascript objects and java objects.
Many classes can have default conversion mechanisms but some require more
custom conversion
-->
<! ELEMENT converter EMPTY >
<!--
@attr id The unique name by which convert elements refer to us.
@attr class The fully qualified name of a class that implements Converter.
-->
<! ATTLIST converter
id ID #REQUIRED
class CDATA #REQUIRED
>
<!--
Security: we must define which classes we are allowed to access because a
free-for-all will be very dangerous.
-->
<! ELEMENT allow (
(create | convert | filter)*
) >
<!--
Allow the creation of a class, and give it a name in javascript land.
A reference to a creator is required as are some parameters specific to each
creator that define the objects it allows creation of.
It would be nice to make the creator and IDREF rather than a CDATA, since it
refers to an element defined elsewhere, however we allow multiple dwr.xml
files and we might refer to one in another file.
-->
<! ELEMENT create (
(param | include | exclude | auth | filter)*
) >
<!--
@attr creator The id of the creator to use
@attr javascript The name of the object to export to the browser
@attr scope The scope of the created variable. The default is page.
-->
<! ATTLIST create
creator CDATA #REQUIRED
javascript CDATA #REQUIRED
scope (application | session | script | request | page) #IMPLIED
>
<!--
A filter is a way to insert procesing tasks at various points during the
processing of an Ajax call. See org.directwebremoting.AjaxFilter
-->
<! ELEMENT filter (
(param)*
) >
<!--
@attr class The class name to use to filter requests
-->
<! ATTLIST filter
class CDATA #REQUIRED
>
<!--
Some elements (currently only create although there is no hard reason why
convert elements should not be the same) need customization in ways that we
can't predict now, and this seems like the only way to do it.
-->
<! ELEMENT param (#PCDATA) >
<!--
@attr name The name of the parameter to this creator
@attr value The value to set to the names parameter
-->
<! ATTLIST param
name CDATA #REQUIRED
value CDATA #IMPLIED
>
<!--
A creator can allow and disallow access to the methods of the class that it
contains. A Creator should specify EITHER a list of include elements (which
implies that the default policy is denial) OR a list of exclude elements
(which implies that the default policy is to allow access)
-->
<! ELEMENT include EMPTY >
<!--
@attr method The method to include
-->
<! ATTLIST include
method CDATA #IMPLIED
>
<!--
See the include element
-->
<! ELEMENT exclude EMPTY >
<!--
@attr method The method to exclude
-->
<! ATTLIST exclude
method CDATA #IMPLIED
>
<!--
The auth element allows you to specify that the user of a given method must be
authenticated using J2EE security and authorized under a certain role.
-->
<! ELEMENT auth EMPTY >
<!--
@attr method The method to add role requirements to
@attr role The role required to execute the given method
-->
<! ATTLIST auth
method CDATA #REQUIRED
role CDATA #REQUIRED
>
<!--
Allow conversion of a class between Java and Javascript.
A convert element uses a previously defined converter and gives a class match
pattern (which can end with *) to define the classes it allows conversion of
It would be nice to make the converter and IDREF rather than a CDATA, since it
refers to an element defined elsewhere, however we allow multiple dwr.xml
files and we might refer to one in another file.
-->
<! ELEMENT convert (
(param)*
) >
<!--
@attr converter The id of the converter to use
@attr match A class name to match for conversion
@attr javascript The optional classname for the parameter
-->
<! ATTLIST convert
converter CDATA #REQUIRED
match CDATA #REQUIRED
javascript CDATA #IMPLIED
>
<!--
If we are marshalling to collections, we need to be able to specify extra
type information to converters that are unable to tell from reflection what to
do. This element contains some Java method definitions
-->
<! ELEMENT signatures (#PCDATA) >
以下是dwr.xml的文件內(nèi)容詳解:
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"dwr20.dtd">
<dwr>
<!-- without allow, DWR isn't allowed to do anything -->
<allow>
<create creator="new" javascript="js的名字">
<param name="class" value="類的地址"></param>
</create>

<!--舉例-->
<create creator="new" javascript="NewsTypeAjax">
<param name="class" value="com.jetsum.pbcbank.news.ajax.NewsTypeAjax"></param>
</create>
</allow>

<signatures>
<![CDATA[
import java.util.List;
]]>
</signatures>


</dwr>
4、Ajax類的寫法很簡單,就和一般的類寫法一樣:
package com.jetsum.pbcbank.news.ajax;

import com.jetsum.pbcbank.news.dao.TypeDao;

public class NewsTypeAjax {

public NewsTypeAjax() {
}
public boolean checkNewsTypeExist(String typename)
{
boolean result = false;
TypeDao dao = new TypeDao();
if(dao.newsTypeExist(typename))
result = true;
return result;
}
}
5、頁面上調(diào)用寫好的dwr:
<!--在導(dǎo)入自定義的dwr之間必須先導(dǎo)入/dwr/engine.js和dwr/util.js否則會報錯-->
<script language="javascript" src="<%=request.getContextPath()%>/dwr/engine.js"></script>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/util.js"></script>

<!--導(dǎo)入自定義的dwr文件,即dwr.xml中所配制的js名,前面加上/dwr/interface/路徑-->
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/interface/NewsTypeAjax.js"></script>

<script type="text/javascript">
<!--舉例-->
function typeExist(){
<!-- 1、獲取頁面數(shù)據(jù)-->
var typename=document.getElementById('newtypename').value;
typename=typename.replace(/(^\s*)| (\s*$)/g, "");
if(typename.length==0){
alert('請輸入新聞類型名稱');
document.getElementById('newtypename').focus();
return false;
}
<!-- 2、設(shè)置為異步的,一般情況下為異步-->
DWREngine.setAsync(false);

<!-- 3、調(diào)用寫好的ajax類中的方法,有兩個參數(shù),第一個是傳入方法的數(shù)據(jù),第二個是回調(diào)函數(shù),flag為調(diào)用的方法所返回的值。-->
NewsTypeAjax.checkNewsTypeExist(typename,function (flag){
if(flag==true)
{
alert("該新聞類別已經(jīng)存在!");
return false;
}
else
{
add();
}
});
</script>
搞定!~~~
下載地址:http://www.cnweblog.com/Files/jimmy/dwr.rar (將后綴名改為jar)
2、在web.xml中配制













3、在WEB-INF下建立dwr.xml和dwr20.dtd兩個文件
復(fù)制以下內(nèi)容到dwr20.dtd文件中,保存:














































































































































































以下是dwr.xml的文件內(nèi)容詳解:


























4、Ajax類的寫法很簡單,就和一般的類寫法一樣:



















5、頁面上調(diào)用寫好的dwr:















document.getElementById('newtypename').focus();
return false;

















搞定!~~~
