1. If dwr2 is used, remember to load the package under Lib in its download example. In this way, you cannot load only one DWR. jar package.
2. Web. xml configuration in DWR
<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>
DEBUG = true: allow http: // localhost: 8085/xxxx/DWR/index.html debugging.
3 Summary of DWR. xml
<? XML version = "1.0" encoding = "GBK"?>
<! Doctype DWR public "-// getahead limited // DTD direct Web remoting 2.0 //" http://getahead.org/dwr/dwr20.dtd ">
<DWR>
<Allow>
<Create creator = "new" javascript = "hello">
<Param name = "class" value = "Lee. hellodwr"/>
</Create>
<Convert converter = "Bean" match = "Lee. Person"/>
<Convert converter = "object" match = "Lee. Cat">
<Param name = "force" value = "true"/>
</Convert>
</Allow>
<Signatures>
<! [CDATA [
Import Lee. hellodwr;
Import Lee. person;
Import java. util. List;
Import java. util. Map;
]>
</Signatures>
</DWR>
Where
<Create creator = "new" javascript = "hello">
<Param name = "class" value = "Lee. hellodwr"/>
</Create>
It indicates that the client uses hello to call the function. You need to convert Lee. hellodwr to Javascript. Note that Class A is usually converted to JavaScript.
Use the Creator builder. If Method B in Class A needs to be converted, use convert to convert between Java class and JavaScript.
Hellodwr. Java (demonstrate conversions of various types)
Import java. util. List;
Import java. util. arraylist;
Import java. util. Map;
Import java. util. hashmap;
/**
* @ Author yeeku. H. Lee kongyeeku@163.com
* @ Version 1.0
* <Br> copyright (C), 2005-2008, yeeku. H. Lee
* <Br> this program is protected by copyright laws.
* <Br> program name:
* <Br> date:
*/
Public class hellodwr
{
Public String Hello (string name)
{
Return name + ", hello! You have already started your DWR learning journey. I wish you a good time ...";
}
Public String sendobj (person P)
{
Return P. getname () + ", hello! You have learned to use the JavaBean parameter ...";
}
Public Person getbean (string name)
{
Return new person ("server side" + name );
}
Public cat GetObject (string name)
{
Return new CAT ("server" + name );
}
Public list <person> getpersonlist ()
{
List <person> result = new arraylist <person> ();
Result. Add (new person ("set aaaa "));
Result. Add (new person ("collection BBBB "));
Result. Add (new person ("set CCCC "));
Return result;
}
Public person [] getpersonarray ()
{
Person [] result = new person [3];
Result [0] = new person ("array aaaa ");
Result [1] = new person ("array BBBB ");
Result [2] = new person ("array CCCC ");
Return result;
}
Public Map <string, person> getpersonmap ()
{
Map <string, person> result = new hashmap <string, person> ();
Result. Put ("first", new person ("map aaaa "));
Result. Put ("second", new person ("map BBB "));
Result. Put ("third", new person ("map CCCC "));
Return result;
}
Public String sendlist (list <person> PL)
{
String result = "";
For (person P: PL)
{
Result + = P. getname () + "<br> ";
}
Return result;
}
Public String sendmap (Map <string, person> pmap)
{
String result = "";
For (string key: pmap. keyset ())
{
Result + = "key" + key + "value:" + pmap. Get (key). getname () + "<br> ";
}
Return result;
}
}
Person. Java:
Public class person
{
Private string name;
Public Person ()
{
}
Public Person (string name)
{
This. Name = Name;
}
Public void setname (string name)
{
This. Name = Name;
}
Public String getname ()
{
Return name;
}
}
Client call.
// -------------------- Send the simple string parameter and return the normal string ----------------------------
Function sendmessage ()
{
VaR name = Document. getelementbyid ("name"). value;
Hello. Hello (name, CB)
}
Function CB (data)
{
Document. getelementbyid ("show"). innerhtml = data;
}
// ------------------- Send a JavaBean object as the parameter and return the normal string -------------------
Function sendobject ()
{
VaR namevalue = Document. getelementbyid ("name"). value;
Hello. sendobj ({Name: namevalue}, CB );
}
// -------------------- Call the JavaBean method returned ----------------------
Function getbean ()
{
VaR name = Document. getelementbyid ("name"). value;
Hello. getbean (name, beancb)
}
Function beancb (data)
{
Document. getelementbyid ("show"). innerhtml = data. Name + ", hello, you have learned to Use Javabean return value ";
}
// -------------------- Call the GetObject method returned ----------------------
Function GetObject ()
{
VaR name = Document. getelementbyid ("name"). value;
Hello. GetObject (name, objcb)
}
Function objcb (data)
{
Document. getelementbyid ("show"). innerhtml = data. Name + ", is the name of the cat returned from the server ";
}
// --------------- Call the method to return the set --------------------------
Function getbeanlist ()
{
Hello. getpersonlist (listcb );
}
Function listcb (data)
{
VaR result = '';
For (VAR I = 0; I <data. length; I ++)
{
Result + = data [I]. Name + "<br> ";
}
Document. getelementbyid ("show"). innerhtml = result;
}
// --------------- Call the method to return the array -------------------------
Function getbeanarray ()
{
Hello. getpersonarray (arraycb );
}
Function arraycb (data)
{
VaR result = '';
For (VAR I = 0; I <data. length; I ++)
{
Result + = data [I]. Name + "<br> ";
}
Document. getelementbyid ("show"). innerhtml = result;
}
// --------------- Call the method to return the array -------------------------
Function getbeanmap ()
{
Hello. getpersonmap (mapcb );
}
Function mapcb (data)
{
VaR result = '';
For (var key in data)
{
Result + = "The key is" + key + "and its value is:" + data [Key]. Name + "<br> ";
}
Document. getelementbyid ("show"). innerhtml = result;
}
// --------------- Call the method of sending the set -------------------------
Function sendbeanlist ()
{
VaR ARGs = [
{Name: "client AAA "},
{Name: "client BBB "},
{Name: "client CCC "}
];
Hello. sendlist (ARGs, sendlistcb );
}
Function sendlistcb (data)
{
Document. getelementbyid ("show"). innerhtml = data;
}
// --------------- Call the method for sending map -------------------------
Function sendbeanmap ()
{
VaR ARGs = {
First: {name: "client AAA "},
Second: {name: "client BBB "},
Third: {name: "client CCC "}
};
Hello. sendmap (ARGs, sendmapcb );
}
Function sendmapcb (data)
{
Document. getelementbyid ("show"). innerhtml = data;
}