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. Is there any way to directly obtain the return value of a method?
Below I will write a simple Java class, as follows:
Public class Test ()
{
Public String getString ()
{
Return "test ";
}
}
The above class is very simple. The getString in it directly returns a string.
Assume that the classes corresponding to Test in DWR are 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
Function callBackFun (data)
{
Alert (data );
}
}
The processing here is very simple, that is, to call the java class method and then process it in the callback function. After the above section is executed, the test, that is, the return value of the java method, will be displayed. However, the use of callback functions 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. open (String method, String URL, boolean asynchronous)
The asynchronous parameter 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 shown below:
Function Test ()
{
Var _ data = "";
This. getString = function ()
{
// Set to synchronization
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
Function callBackFun (data)
{
_ Data = data;
}
}
The preceding method is set to synchronous before calling the java method. After the java method is called, the following statement is executed after the callback function is executed, if _ data is returned, the value is assigned. Therefore, the value can be obtained normally.
The above statements are troublesome. You can write them as follows:
Function Test ()
{
Var _ data = "";
// Set to synchronization
DWREngine. setAsync (false );
// Call the getString method of Java Test. callBackFun is the callback function.
JTest. getString (function (data) {_ data = data ;});
// Reset to asynchronous mode
DWREngine. setAsync (true );
Return _ data;
}
Author: "battier blog"