Dwr-ajax Open Source Framework

Source: Internet
Author: User


DWR Full Name Direct WEB Remoting principle transforms Java code into JS object for cross-domain access

Xml

<?xml version= "1.0"  encoding= "UTF-8"? ><web-app xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "    xmlns=" Http://java.sun.com/xml/ns/javaee " xmlns:web="/HTTP/ Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "    xsi:schemalocation=" Http://java.sun.com/xml /ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "    id=" WebApp_ID "  version= "2.5" ><display-name>DWR  (direct web remoting) </display-name>   <description>A Simple Demo DWR</description>  <listener>     <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>   </listener>    <servlet>    <servlet-name> Dwr-invoker</servlet-name>    <servlet-class> org.directwebremoting.servlet.dwrservlet</servlet-class>        <init-param>      < param-name>debug</param-name>      <param-value>true</ param-value>    </init-param>      < load-on-startup>1</load-on-startup>  </servlet>     < servlet-mapping>    <servlet-name>dwr-invoker</servlet-name>     <url-pattern>/dwr/*</url-pattern>  </servlet-mapping>     </web-app>

Dwr.xml

<?xml version= "1.0"  encoding= "UTF-8"? ><! doctype dwr public  "-//getahead limited//dtd direct web remoting 3.0// EN " " HTTP://GETAHEAD.ORG/DWR/DWR30.DTD "><dwr>  <allow>     <!--    <filter class= "Com.example.dwr.monitor.MonitoringAjaxFilter"/>     <filter class= "Org.directwebremoting.filter.ExtraLatencyAjaxFilter" >       <param name= "delay"  value= "/>    </filter" >    -->    <!-- intro - for the  Test on index.html -->    <create creator= "New" >       <param name= "Class"  value= "ORG.KONGHAO.DWR.MODEL.MYDWR"/>     </create>    <convert conVerter= "Bean"  match= "Org.konghao.dwr.model.User"/>    <convert converter= " Bean " match=" Org.konghao.dwr.model.Group "/>        <convert  match= "Java.lang.Exception"  converter= "Exception"/>    <convert  Converter= "Bean"  match= "java.lang.StackTraceElement"/>  </allow></dwr>


package org.konghao.dwr.model;import java.io.file;import java.io.ioexception;import  java.io.inputstream;import java.util.arraylist;import java.util.list;import  javax.servlet.http.httpservletrequest;import org.apache.commons.io.fileutils;import  org.apache.commons.io.filenameutils;import org.directwebremoting.webcontext;import  Org.directwebremoting.webcontextfactory;public class mydwr {    public  string hello (String world)  {         System.out.println ("hello " +world);        return  "Hello   "+world;    }        public user  Load ()  {        user u = new user (1, "Zhang San", New  group (1, "Finance Office")); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;U;&NBSP;&NBsp;  }        public list<user> list ()  {        List<User> users = new  Arraylist<user> ();         users.add (New User (1, "Zhang San", New  group (1, "Finance Office"));         users.add (New user (2, "John Doe", new  group (2, "Science and Technology Branch"));         users.add (New user (3, "Harry", new  group (3, "propaganda Department")));        return users;     }        public void add (User user)  {         system.out.println (user);    }     public void deleteuser ()  {        throw new  myexception ("Error while deleting the user");  &nBsp;  }        public int add (int a,int  b)  {        return a+b;    }         public string upload (Inputstream is,string filename)  throws ioexception {        // Webcontext can get HttpServlet objects         WebContext wc =  Webcontextfactory.get ();         httpservletrequest req =  wc.gethttpservletrequest ();         string realpath =  req.getsession (). Getservletcontext (). Getrealpath ("Upload");         string fn = filenameutils.getname (filename);         string filepath = realpath+ "/"+fn;        fileutils.copyinputstreamtofile (Is, new File ( filepath));         return filepath;    }}



package org.konghao.dwr.model;public class myexception extends runtimeexception  {    public myexception ()  {        super ();         // todo auto-generated constructor stub     }    public myexception (string message, throwable  cause)  {        super (message, cause);         // TODO Auto-generated constructor stub     }    public myexception (string message)  {         super (message);         // todo  Auto-generated constructor stub    }    public  MyException (Throwable cauSE)  {        super (cause);         // todo auto-generated constructor stub    }}



Package org.konghao.dwr.model;public class user {    private int  id;    private string username;    private group  group;    public int getid ()  {         return id;    }    public void setid (int  id)  {        this.id = id;     }    public string getusername ()  {         return username;    }    public void  Setusername (String username)  {        this.username =  username;    }    public group getgroup ()  {         return group;    }    public void setgroup ( Group group)  {        this.group = group;     }    public user (Int id, string username, group  group)  {        super ();         this.id = id;        this.username =  username;        this.group = group;     }    public user ()  {        super ( );    }     @Override     public String  ToString ()  {        return  "user [id="  + id  +  ",  usErname= " + username + ",  group= " + group                 +  "]";     }}


Package org.konghao.dwr.model;public class group {    private int  id;    private String name;             public int getid ()  {         return id;    }    public void setid (Int id)  {        this.id = id;    }     public string getname ()  {         Return name;    }    public void setname (String  Name)  {        this.name = name;     }    public group (Int id, string name)  {         sUper ();        this.id = id;         this.name = name;    }    public  Group ()  {        super ();    }      @Override     public string tostring ()  {         return  "group [id="  + id +  " name="  +  name +  "]";     }        }



No refresh upload file

<%@ page language= "java"  contenttype= "Text/html; charset=utf-8"      pageencoding= "UTF-8"%><! doctype html public  "-//w3c//dtd html 4.01 transitional//en"   "HTTP// Www.w3.org/TR/html4/loose.dtd ">


This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1752923

Dwr-ajax Open Source Framework

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.