Simple Ajax-DWR framework example

Source: Internet
Author: User

1. Obtain DWR. jar.

Http://www.getahead.ltd.uk/dwr/download.html

 

Put the obtained DWR. jar in the WEB-INF/lib directory.

2. Configure the Web. xml file of the WEB Project

<Servlet>

<Servlet-Name> DWR-invoker </servlet-Name>

<Display-Name> dwrservlet </display-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>

 

 

3, create a DWR. xml file, put it in the WEB-INF directory and web. xml companion

<DWR>

<Allow>

<Createcreator = "new" javascript = "JDate">

<Paramname = "class" value = "Java. util. Date"/>

</Create>

<Createcreator = "new" javascript = "Demo">

<Paramname = "class" value = "Your. java. Bean"/>

</Create>

</Allow>

</DWR>

 

 

 

 

4. Follow the instructions to run http: // localhost: 8080/your_app/DWR at this time. A page should appear, but an error occurred while running because of the disadvantages of the DWR. xml file.

Changed:

<? Xmlversion = "1.0" encoding = "UTF-8"?>

<! Doctypedwrpublic

"-// Getaheadlimited // dtddirectwebremoting1.0 // en"

Http://www.getahead.ltd.uk/dwr/dwr10.dtd>

 

<DWR>

<Allow>

<Createcreator = "new" javascript = "JDate">

<Paramname = "class" value = "Java. util. Date"/>

</Create>

<Createcreator = "new" javascript = "jstring">

<Paramname = "class" value = "Java. Lang. String"/>

</Create>

</Allow>

</DWR>

 

 

Seeing this interface, I suddenly had a great idea! If you configure the frequently-used JDK classes here, we can view the running results of various jdkapi methods very conveniently by using very.

 

 

 

For more information about DWR. DTD, see

Http://www.getahead.ltd.uk/dwr/server/dwrxml.html

 

[Note] If you are using dwr0.8.x, the write may be incorrect. I am using version 0.9.2b.

5. Click the JDate listed on the page to view the output of each Java. util. Date () method. How does this work? Check the source code, which is important.

<Scriptsrc = "/EPT/DWR/interface/JDate. js" type = text/JavaScript> </SCRIPT>

<Scriptsrc = "/EPT/DWR/engine. js" type = text/JavaScript> </SCRIPT>

<Scriptsrc = "/EPT/DWR/util. js" type = text/JavaScript> </SCRIPT>

 

6. compile a complete example:

 

6.1 compile DWR. xml

<? Xmlversion = "1.0" encoding = "UTF-8"?>

<! Doctypedwrpublic

"-// Getaheadlimited // dtddirectwebremoting1.0 // en"

Http://www.getahead.ltd.uk/dwr/dwr10.dtd>

 

<DWR>

<Allow>

<Createcreator = "new" javascript = "JDate">

<Paramname = "class" value = "Java. util. Date"/>

</Create>

<Createcreator = "new" javascript = "dateutil">

<Paramname = "class" value = "com. Neusoft. EPT. Common. dateutil"/>

</Create>

</Allow>

</DWR>

 

6.2 compile the dateutil class. I use a static class.

/**

* <P> title: </P>

* <P> Description: </P>

* <P> copyright: Copyright (c) 2002 </P>

* <P> company: </P>

* @ Authornotattributable

* @ Version1.0

*/

 

Packagecom. Neusoft. EPT. Common;

 

Importjava. SQL. date;

Importjava. SQL. timestamp;

Importjava. SQL. types;

Importjava. Text. simpledateformat;

Importjava. util. calendar;

Importjava. util. gregoriancalendar;

Importjava. util. stringtokenizer;

 

Importep. Op. DB. dbpersistence;

Importep. rowset. typeconvert;

 

Publicclassdateutil

{

Publicdateutil ()

{

}

 

/** Obtain the system time of the Current Database

*

*

* @ Parampersistence database persistence object

* @ Returnjava. SQL. timestamp */

Publicstatictimestampgetsystemdatetime (dbpersistencepersistence)

{

Timestampsysdate = persistence. getsystemdate ();

Returnsysdate;

}

 

/**

* Obtain the system time of the application server.

* @ Returnjava. SQL. Timestamp

*/

Publicstatictimestampgetsystemdatatime (){

Longmillis = system. currenttimemillis ();

Timestampsysdate = newtimestamp (millis );

Returnsysdate;

}

 

/** Function: determines whether it is a leap year.

* Input: year = year after January 1, 1582.

* Output: True indicates a leap year, and false indicates no.

*

*

*

* @ Parami_year */

Publicstaticbooleanisleapyear (inti_year)

{

/* The year that can be divisible by 100, but cannot be divisible by 400, not a leap year.

* The year that can be divisible by 100 or 400 is a leap year .*/

 

 

 

If (I _year % 100) = 0)

Return (I _year % 400) = 0 );

/* Cannot be divisible by 100. The year to be divisible by 4 is a leap year .*/

Return (I _year % 4) = 0 );

}

 

 

 

 

 

}

 

 

6.3 compile your own dwr1.jsp

<% @ Pagelanguage = "Java" %>

<% @ Pagecontenttype = "text/html; charset = GBK" %>

<HTML>

<Head>

<Scriptsrc = "/EPT/DWR/interface/dateutil. js" type = text/JavaScript>

</SCRIPT> <scriptsrc = "/EPT/DWR/engine. js" type = text/JavaScript>

</SCRIPT> <scriptsrc = "/EPT/DWR/util. js" type = text/JavaScript> </SCRIPT>

</Head>

<Bodyonunload = "_ sysunload. Unload ();">

Inputtheyear <inputtype = "text" value = "1999" id = "isleapyear">

<Inputtype = "button" value = "execute" onclick = "isleapyear ()"/>

</Body>

</Html>

<Scriptlanguage = "JavaScript">

Functionisleapyear (){

Varyear = Document. getelementbyid ("isleapyear"). value;

Dateutil. isleapyear (reply, parseint (year, 10 ));

}

Reply = function (data ){

Alert (data );

}

</SCRIPT>

 

6.4 run

Http: // localhost: 8080/EPT/dwr1.jsp

Related Article

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.