Introduction to DWR

Source: Internet
Author: User

DWR
: Direct Web remoting, that is, the Java method that can be remotely accessed to the server on the client, is equivalent to a method that can be called to the Java class using JavaScript.

It sounds nice. In fact, this is just the most intuitive feeling. In fact, a dwrservlet is used in the middle to access the server code, while javascript accesses this dwrservlet through Ajax, so it seems that JavaScript directly accesses the Java code.
. DWR encapsulates Ajax and has functions similar to jquery.

 

To use helloword:

 

First:

Get the DWR package to DWR. Jar. Download: http://directwebremoting.org/dwr/download.html in the project

 

Second:

I just mentioned that there is actually a servlet in the middle to connect js to the server, so we need to configure the dwrservlet information in Web. XML, as shown below:

 

 <Servlet>


<Servlet-Name>

DWR</Servlet-Name>


<Servlet-class>
Org. directwebremoting. servlet. dwrservlet
</Servlet-class>


<! -- The init-Param here is only used for debugging -->

<Init-param>

<Param-Name>
Debug
</Param-Name>

<Param-value>
True
</Param-value>

 </Init-param>

</Servlet>


<Servlet-mapping>

<Servlet-Name>
DWR
</Servlet-Name>

<URL-pattern>
/DWR /*
</Url-pattern>

</Servlet-mapping>

 

Write a test class:

 

Package

Org. LC. DWR;

Public
Class

Dwrtest {


Public

String sayhello (string somebody ){
Return

Somebody + ", hello! ";
}
}

Again (key ):

Create a DWR. xml file under Web-INF of the project:

 

The content is as follows:

 

<DWR>
<Allow>

<! -- The information of the test class is configured here -->

 
<Create javascript ="
HW
"Creator ="
New
"
>
<! -- JavaScript = "HW" indicates that the method of the test class should be called with the HW object in JS, such as HW. sayhello ("China"); creator = "new" indicates to use the new keyword to create an object for the test class .. Spring is not integrated here -->

<Param name ="

Class
"Value ="

Org. LC. DWR. dwrtest
"/>


</Create>
</Allow>
</DWR>

 

 

 

Finally:

 

Client code

 

<HTML>

<Head>


<SCRIPT type
= 'Text/JavaScript
'Src
= '/DWR/interface/HW. js
'> </SCRIPT>
<SCRIPT type
= 'Text/JavaScript
'Src
= '/DWR/engine. js
'> </SCRIPT>
<SCRIPT type
= 'Text/JavaScript
'Src
= '/DWR/util. js
'> </SCRIPT>

<SCRIPT>


Function
C (){
HW. sayhello (DWR. util. getvalue ("C
"), Function (RDATA) {<! -- The first parameter is used as the sayhello parameter in the dwrtest class, and the second anonymous function is used to process the return value of sayhello. -->

DWR. util. setvalue ("CC
", RDATA );

});
}
</SCRIPT>
</Head>

<Body>



<

Input


Type
= "Text
"Id =" C
"/> <Input

Type
= "Button" Value
= "Sayhello" onclick
= "C ()
;"/>


<Div

ID
= "Cc"></Div>


</Body>
</Html>

 

:

 

Thoughts:

 

The Return Value of the method here is a string, so there is no problem. What if the return value is a list or a JavaBean object ?,

Or the input parameter is a JavaBean object. How can this problem be solved? If you do not know, add the QQ group: 37983546 and ask a question.

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.