Recently, a framework called dwr was used in the project. Since I just saw js for a few days, I don't understand many things, first, a web application using the dwr framework is configured according to the information on the Internet.
Eclipse: Juno Service Release 2
Tomcat: version-6.x
Jdk: 6.x
Use eclipse to create a dynamic web project.
Modify web. xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" 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 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><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>login.jsp</welcome-file><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file><welcome-file>test.html</welcome-file></welcome-file-list></web-app>
Create three classes
Package com. charvis. model; public class User {// Login ID, the only private String id of the primary key; // name private String name; // password private String password; // email private String email; public String getId () {return id;} public void setId (String id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getPassword () {return password;} public void setPassword (String password) {this. password = password;} public String getEmail () {return email;} public void setEmail (String email) {this. email = email ;}}
Package com. charvis. dao; import java. util. hashMap; import java. util. map; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; import com. charvis. model. user; public class UserDAO {private static Log log = LogFactory. getLog (UserDAO. class); // store the stored data private static Map dataMap = new HashMap ();/* // permanent User public boolean save (user User) {if (dataMap. containsKey (user. getId () return false; System. out. println ("start to save the user below"); System. out. println ("id:" + user. getId (); System. out. println ("password:" + user. getPassword (); System. out. println ("name:" + user. getName (); System. out. println ("email:" + user. getEmail (); dataMap. put (user. getId (), user); System. out. println ("User save ended"); return true;} * // permanent user public boolean save (User user) {if (dataMap. containsKey (user. getId () return false; log.info ("start to save the user"); log.info ("id:" + user. getId (); log.info ("password:" + user. getPassword (); log.info ("name:" + user. getName (); log.info ("email:" + user. getEmail (); dataMap. put (user. getId (), user); log.info (""); return true;} // find the User public User find (String id) {return (user) dataMap. get (id );}}
package com.charvis.dwr;import com.charvis.dao.UserDAO;import com.charvis.model.User;public class DWRUserAccess {UserDAO userDAO = new UserDAO(); public boolean save(User user) { return userDAO.save(user); } public User find(String id) { return userDAO.find(id); }}
Create a new test.html File
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> DWR test </TITLE> <meta http-equiv = content-Type content = "text/html; charset = UTF-8 "> <script type = 'text/javascript 'src =" dwr/engine. js "> </script> <script type = 'text/javascript 'src =" dwr/util. js "> </script> <script type = 'text/javascript 'src =" dwr/interface/DWRUserAccess. js "> </script> </HEAD> <BODY> <B> User Registration </B> <br> -------------------- ---------------------------- <Br> <form name = "regForm"> Login ID: <input type = "text" name = "id"> <br> command: <input type = "password" name = "password"> <br> last name: <input type = "text" name = "name"> <br> Email: <input type = "text" name = "email"> <br> <input type = "button" name = "submitBtn" value = "Submit" onclick = "OnSave () "> <br> </form> <br> <B> user query </B> <br> -------------------------------------------- <Br> <form name =" queryForm "> Login Land ID: <input type = "text" name = "id"> <br> <input type = "button" name = "submitBtn" value = "Submit" onclick = "OnFind () "> <br> </form> <br> </BODY> <script language =" JavaScript "> function saveFun (data) {if (data) {alert ("registration successful! ");} Else {alert (" Login ID already exists! ") ;}} Function OnSave () {var userMap ={}; userMap. id = regForm. id. value; userMap. password = regForm. password. value; userMap. name = regForm. name. value; userMap. email = regForm. email. value; DWRUserAccess. save (userMap, saveFun);} function findFun (data) {if (data = null) {alert ("user not found:" + queryForm. id. value); return;} alert ("locate user, nid:" + data. id + ", npassword:" + data. password + ", nname:" + data. name + ", nemail:" + data. email);} function OnFind () {DWRUserAccess. find (queryForm. id. value, findFun) ;}</SCRIPT> </HTML>
Then create the dwr. xml file under the web-inf folder.
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://directwebremoting.org/schema/dwr30.dtd"><dwr><allow><create creator="new" javascript="DWRUserAccess"><param name="class" value="com.charvis.dwr.DWRUserAccess" /></create><convert converter="bean" match="com.charvis.model.User" /></allow></dwr>
After research, we can find that this configuration file is mapped to js and java classes. The DWRUserAccess object in js should be mapped to the object of the class com. charvis. dwr. DWRUserAcces. The parameter data of the callback function is the object com. charvis. model. User.
Run the test. Enter one at the user name. The registration is successful. Here I tried commons-logging. Of course, log4j can be used.