Directly obtain the return value of a Java class in DWR.

Source: Internet
Author: User

Directly obtain the return value of a Java class in DWR.

DWR is an open-source framework of Ajax and can easily call remote Java classes. However, DWR can only use the callback function to obtain and process the returned value in the callback function. So, is there a way to directly obtain a method's return value? Below I will write a simple Java class, as follows: public class test () {Public String getstring () {return "test" ;}} the above class is very simple, getstring directly returns a string. Assume that the class corresponding to test in DWR is not jtest configured in DWR, then we need to call the getstring method, which can be written as follows: function test () {// call the getstring method of Java test. callbackfun is the callback function jtest. getstring (callbackfun); // callback function callbackfun (data) {alert (data) ;}} the processing here is very simple. It is to call the Java class method and then process it in the callback function, after the above statement is executed, test is displayed, that is, the return value of the Java method. However, using the home function does not conform to our habits. Sometimes we want to directly obtain the return value for processing. At this time, we will be powerless. We know that DWR is the Ajax framework, so it must have Ajax features. Let's talk about the running principle of Ajax. In fact, its principle is very simple. It is to call the remote address, obtain the page returned data, and then analyze and process it. This process is asynchronous, Which is why DWR uses the callback function. You don't know when to execute the callback function after we call the Java class. Let's take a look at the Ajax method. Among them, the XMLHttpRequest OPEN function has a synchronous parameter, as shown below: XMLHttpRequest. in open (string method, string URL, Boolean asynchronous), asynchronous indicates whether to synchronize data. Now, let's open the engine. js file of DWR, search for an Asyn, and immediately find a setasync method. It turns out that DWR is set to attribute encapsulation. In this way, we can implement the function of getting the return value. Below, I encapsulate the Java class in DWR, as follows: function test () {VaR _ DATA = ""; this. getstring = function () {// set to synchronous dwrengine. setasync (false); // call the getstring method of Java test. callbackfun is the callback function jtest. getstring (callbackfun); // reset to asynchronous mode dwrengine. setasync (true); Return _ data;} // callback function callbackfun (data) {_ DATA = data ;}} the above method, set the method to synchronous before calling the Java method. After the Java method is called, The callback function is executed before the following statement is executed. In this way, the value of _ data returned is assigned, so you can get the value normally. The above statements are troublesome. You can write them as follows: function test () {VaR _ DATA = ""; this. getstring = function () {// set to synchronous dwrengine. setasync (false); // call the getstring method of Java test. callbackfun is the callback function jtest. getstring (function (data) {_ DATA = data ;}); // reset it to the asynchronous mode dwrengine. setasync (true); Return _ DATA ;}}
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.