The application of DWR in the project

Source: Internet
Author: User

  1. Introduction to the DWR framework
  2. The DWR framework is a Java open Source library that allows you to create an AJAX Web site. It allows you to invoke Java code from the Web server in your browser's JavaScript code, just like Java code in a browser. DWR works by dynamically generating JavaScript for the Java class, making the user feel like the call is happening on the browser side.
    1. 2, the use of DWR applications
    2. We can use DWR when our business needs to access the server side and implement a local refresh of the page data when the page is not submitted.
      1. 1. Import the DWR jar package
      2. 2, write a DWR framework in the page with JavaScript to invoke the Java class.
      3. Configuration in Web. xml
      4. <servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <display-name>dwr servlet</display-name>
        <description>direct Web remoter servlet</description>
        <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

        <!--this should never is present in live--
        <init-param>
        <param-name>debug</param-name>
        <param-value>true</param-value>
        </init-param>

        <!--Remove This unless your want to use active reverse Ajax--
        <init-param>
        <param-name>activeReverseAjaxEnabled</param-name>
        <param-value>true</param-value>
        </init-param>

        <!--By default DWR creates application scope objects when they is first
        Used. This creates them when the app-server is started--
        <init-param>
        <param-name>initApplicationScopeCreatorsAtStartup</param-name>
        <param-value>true</param-value>
        </init-param>

        <!--This enables full streaming mode. It ' s probably better to leave this
        If you are running across the Internet--
        <init-param>
        <param-name>maxWaitAfterWrite</param-name>
        <param-value>-1</param-value>
        </init-param>

        <!--
        For more information in these parameters, see:
        -Http://getahead.org/dwr/server/servlet
        -Http://getahead.org/dwr/reverse-ajax/configuration
        -

        <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
        <servlet-name>dwr-invoker</servlet-name>
        <url-pattern>/dwr/*</url-pattern>
        </servlet-mapping>
      5. Dwr.xml
      6. <dwr>

        <allow>

        <!--
        <filter class= "Org.getahead.dwrdemo.monitor.MonitoringAjaxFilter"/>
        <filter class= "Org.directwebremoting.filter.ExtraLatencyAjaxFilter" >
        <param name= "delay" value= "/>"
        </filter>
        -

        <!--intro-for the test on index.html--
        <create creator= "new" javascript= "Intro" >
        <param name= "class" value= "Org.getahead.dwrdemo.intro.Intro"/>
        </create>


        <convert match= "com.dianzhi.bean.City" converter= "Bean" >
        <param name= "include" value= "Cityid,cityname"/>
        </convert>

        <!--SimpleText--
        <create creator= "new" javascript= "Dwrservice" >
        <param name= "class" value= "Com.dianzhi.DWRService"/>
        </create>

        </allow>

        </dwr>
        1. 5, write JSP page, use JavaScript to invoke Java code in the background
        2. <script type= ' Text/javascript '
          src= '/dwr_mycity/dwr/interface/dwrservice.js ' >
          </script>
          <script type= ' text/javascript ' src= '/dwr_mycity/dwr/engine.js ' >
          </script>

          <script type= ' text/javascript ' src= '/dwr_mycity/dwr/util.js ' >
          </script>
          <script type= "Text/javascript" src= "Jquery/jquery-1.3.2.js" >
          </script>
          <script type= "Text/javascript" >
          Window.onload=function () {
          Dwrservice.getpropive (function (data) {
          For (var p in data) {
          Alert (p);
          Alert (data[p]);
          var option = document.createelement ("option");
          OPTION.INNERHTML=DATA[P];
          Option.value=p;
          document.getElementById ("Provice"). appendchild (option);
          }
          });
          }
          function Loadshi (ID) {
          Alert ("AAAA");
          Dwrservice.getcity (id,function (data) {
          document.getElementById ("City"). innerhtml= "";
          document.getElementById ("Qu"). innerhtml= "";
          for (var c in data) {
          var option =document.createelement ("option");
          OPTION.INNERHTML=DATA[C];
          Option.value=c;
          document.getElementById ("City"). appendchild (option);
          }

          });
          }
          function Loadqu (ID) {
          Alert ("AAAA");
          Dwrservice.getqu (id,function (data) {
          document.getElementById ("Qu"). innerhtml= "";
          for (var q in data) {
          var option =document.createelement ("option");
          OPTION.INNERHTML=DATA[Q];
          option.value=q;
          document.getElementById ("Qu"). appendchild (option);
          }

          });
          }
          </script>

          <body>
          <select id= "Provice" onchange= "Loadshi (this.value)" >
          </select>
          <select id= "City" onchange= "Loadqu (this.value)" >
          </select>
          <select id= "Qu" >
          </select>
          </body>
        3. public class Dwrservice {
          /*public list<city> getpropive () {
          Icitydao dao = new Citydao ();
          List<city> list =dao.findbyparentid (0);
          for (city City:list) {
          System.out.println (City.getcityid () + "..." +city.getcityname ());

          }
          System.out.println (List.size ());
          return list;

          }*/
          Public map<integer,string> getpropive () {
          Icitydao dao = new Citydao ();
          List<city> list =dao.findbyparentid (0);
          map<integer,string> map = new hashmap<integer,string> ();
          for (city City:list) {
          System.out.println (City.getcityid () + "..." +city.getcityname ());
          Map.put (City.getcityid (), City.getcityname ());
          }
          return map;

          }
          Public map<integer,string> getcity (int id) {

          Icitydao dao = new Citydao ();
          List<city> list =dao.findbyparentid (ID);
          map<integer,string> map = new hashmap<integer,string> ();
          for (city City:list) {
          System.out.println (City.getcityid () + "..." +city.getcityname ());
          Map.put (City.getcityid (), City.getcityname ());
          }
          return map;

          }
          Public map<integer,string> getqu (int id) {

          Icitydao dao = new Citydao ();
          List<city> list =dao.findbyparentid (ID);
          map<integer,string> map = new hashmap<integer,string> ();
          for (city City:list) {
          System.out.println (City.getcityid () + "..." +city.getcityname ());
          Map.put (City.getcityid (), City.getcityname ());
          }
          return map;

          }



The application of DWR in the project

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.