Introduction to dwr

Source: Internet
Author: User

Introduction to dwr

 

In addition to a commons-logging-1.2.jar,

Put dwr. jar and commons-logging-1.2.jar in lib.

Let's take a look at an entry-level teaching material at www.bkjia.com.

This write is very simple and can be implemented according to the configuration.

1. entity User

 

Package iwco. entity; 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 ;}}

2. DWRUserAccess class

 

 

package iwco.dwrs;import iwco.dao.UserDAO;import iwco.entity.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);    }}

 

3. Add the following content to the WEB. xml file:

 

   
  
   DWR Servlet
    
  
   dwr-invoker
      
  
   org.directwebremoting.servlet.DwrServlet
    
       
   
    debug
        
   
    true
     
  
 
   
  
   dwr-invoker
    
  
   /dwr/*
  
 

4. Add dwr. xml below the WEB-INF/as follows:

 

 

       
  
       
      
  
       
      
    
  
 

 

52.16test.html page

 

 <Script src =/dwrTest/dwr/engine. js> </script> <script src =/dwrTest/dwr/util. js> </script> <script src =/dwrTest/dwr/interface/DWRUserAccess. js> </script> <script LANGUAGE = JavaScript> function saveFun (data) {if (data) {alert (registered successfully !); } Else {alert (the 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 (users cannot be found: + queryForm. id. value); return;} alert (locate the user, nid: + data. id +, npassword: + data. password +, nname: + data. name +, nemail: + data. email);} function OnFind () {DWRUserAccess. find (queryForm. id. value, findFun);} // --> </script> User Registration

------------------------------------------------


User query
------------------------------------------------

Project organization diagram:

 

Then, allow the project and open the browser for testing. Let's take a look at it. I used it a long time ago. It Feels simple and I know the principle. However, a colleague asked me the day before yesterday, asking me to configure one. At that time, I wanted to complete it. Later I thought there were a lot of introductory teaching materials. But if you think about it carefully, you can record it and ask the connection address directly.

1. Add three js files on the jsp page:

 

<script src=/dwrTest/dwr/engine.js></script><script src=/dwrTest/dwr/util.js></script><script src=/dwrTest/dwr/interface/DWRUserAccess.js></script>
The first two did not say that dwr comes with js, and the third is our own. People who just came into contact must be confused. In fact, it's easy to understand. We have the configuration in dwr:

 

 

 
 
      
 
The file name of the third js is the javascript value, and it is the directory starting with dwr/interface.

 

2. create nodes in dwr. xml. creator can be configured in several ways:

New: Java uses the "new" keyword to create an object

None: it does not create an object (v1.1 +)

Scripted: creates an object through BSF using a scripting language, such as BeanShell or Groovy

Spring: Access Bean through Spring framework

Struts: Use Struts FormBean (v1.1 +)

Jsf: Bean using JSF (v1.1 +)

Pageflow: access the PageFlow (v1.1 +) of Weblogic or Beehive)

Ejb3: Use EJB3 session bean (v2.0 +)

The above example uses new, and many projects now use Spring, so I gave a spring:

 

 
    
    
  
  
     
 
  
 

Add the following content to the corresponding spring file:
 
 
Of course, you can handle the annotation yourself.



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.