DWR 2.0RC1 Application

Source: Internet
Author: User
1. Overview

DWR (http://getahead.ltd.uk/dwr/) is a new Ajax way, unlike the traditional mode of accessing URLs, DWR has made a Generic service Gateway (PROXY) at the server, making the client's Javascript can directly manipulate the service layer of the remote server side, and then cooperate with Trimpath Jstemplate to render the service layer Pojo as a complex page in template instead of Dom Builder. Create a new, pure AJAX programming model.

Today, DWR's application has been explained a lot, we can Google themselves. Here are only a few key points: standard installation and use, Trimpath jstemplate application, DWR2.0 push technology application.

Springside in the bookstore example of book management--cover image Ajax upload, using DWR to get the size of the uploaded part. 2. Installation and use 2.1 Download Dwr

The latest version of 2.0RC1 is on the official website, and sometimes the latest version needs to be downloaded to dwr.dev.java.net. 2.2 configuration Files

The configuration file is automatically generated and the Web-inf/web.xml file is modified by using the Springside build assembly

If you want to write it manually, see the sample file in the Examples/bookstore/webapp/web-inf directory

Modify Web.xml to add Dwr-invoker servlet

Add Dwr.xml, configure exported service services, and beans to convert to client JavaScript.

If the security requirements are not strict, can be very simple to match up, if Strictly a little trouble, see dwr.xml comments. 2.3 Client calls 1. Introduce DWR a dynamically generated JavaScript and two standard utils.js:

Bookservice.js's name is defined in the service-side dwr.xml.

Interface/bookservice.js "/>" type= "Text/javascript" >
//-->
"/>" type= "Text/javascript" > //-->
"/>" type= "Text/javascript" > //-->
2. Use the Bookservice object:

In the JavaScript that follows, you can use the Bookservice object and his methods like in Java, such as

Bookservice.findbooksbycategory (category, Drawbooks);

The first parameter is the server-side findbookbycategory parameter, and the 2nd parameter is the function to be processed when it is returned. 3. Using Trimpath jstemplate render the returned Pojo:

The Trimpath jstemplate is a dwr-matching return result (JavaScript object,not HTML or XML) rendering method.

On the official website http://trimpath.com/project/wiki/JavaScriptTemplates download and copy template.js to any directory, and then add on the page

"/>" type= "Text/javascript" >
//-->

Then according to trimpath similar freemarker simple syntax in the writing template and the JS Pojo into the template to render.

{for B in MyBooks}
<p>${b.name}</p>
{/for}

specific demo in 1.0m3 bookstore Front Webservcie page. 4. Push Technology

dwr2.0.x push Technology to push the message, applicable to the chatroom, stock information display and other scenarios, better than the current JS timer polling service-side strategy, greatly reduce the resources of the server (no change in the data does not need to answer the client's timing query). 4.1. Receive page for push message

Sets JavaScript to receive messages pushed over by the service side

     1 <script type= ' text/javascript ' src= ' <c:url value= "/dwr/engine.js" > ' ></script> 

    <script type= ' text/javascript ' src= ' <c:url value= '/dwr/ Util.js "/> ' ></script>
    <script type= ' text/javascript ' src= ' <c:url value= '" Dwr/interface/ordernotice.js "/> ' ></script>"


  2. <script language= "JavaScript" >
        function receivemessages (ID) {
             $ (' Ordernotice '). InnerHTML = "Receive a new Order with ID + ID +"! ";
            $ (' Ordernotice '). Show ();
       }
    </script>
    3.<body onload= "Dwrengine.setreverseajax (true);"

   <div id= "Ordernotice"/>

First, the introduction of DWR and the Java Class (Ordernotice), which is responsible for performing the push operation, JS

Second, write any of the JS function of receiving information

third, in the body of the onload set Reverseajax=true, start listening to information 4.2 Java classes responsible for pushing messages

  public class Ordernotice {
    public void noticeneworder (int id) { & nbsp;       Webcontext wctx = Webcontextfactory.get ();
         Scriptbuffer script = new Scriptbuffer ();
         script.appendscript ("Receivemessages ("). AppendData ( ID). Appendscript (");
         ServerContext sctx = Servercontextfactory.get ( Wctx.getservletcontext ());
         Collection pages = sctx.getscriptsessionsbypage ("/springside/ Admin/top.jsp ");
         for (scriptsession session:pages) {
             session.addscript (script);
       }
   }
}

Visible, the first Scriptbuffer constructs a section of SQL executed on the client, calling the Receivemessages function of the previously received page.

then use Webcontext, ServerContext to locate the session that needs to be sent. Note Here hard code has a URL path to determine subscriber, or, as in chatroom, to send a session to the caller initiator on the same page as the Ordernotice. 4.3 remaining work

Set Dwr.xml, press DWR old mode, call Ordernotice from client, push information to admin background page.

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.