During the project process, we found that the DWR method was used to call the remote method. Sometimes the Alert box appears inexplicably with the content "error ". If a null value or a direct error is returned, corresponding exception handling must be performed:
Remote. Method (Params, function (data ){
// The client logic after the server executes, as shown in figure
Alert ("data returned after server execution:" + data );
});
Remote> processing class, method> method in class, Params> parameters in method, function> callback method, data> data returned by callback method.
The first parameter is the data that the client transmits to the server. The last parameter is a callback function. Because the JS Code generated by the server based on the Java object is:
Remote. method = function (P0, callback ){
DWR. Engine. _ execute (remote. _ path, 'remote', 'method', P0, callback );
}
If the remote method throws an exception when processing the request, DWR will only give alert ("error") a simple error, so we need to figure out how to actively capture this error, more friendly display to users. Therefore, if a remote call exception occurs
The Alert ("error") is actually the global error handling of DWR. You can use DWR. Engine. seterrorhandler (errorhandler) to change its default behavior, such
Function errh (errorstring, exception ){
Alert ("indicates the custom global error message. ");
}
DWR. Engine. seterrorhandler (errh );
We may rarely do this because the global error processing cannot meet personalized remote calls. We can write the error handling errorhandler in the remote call function parameters. Looking back at the previous callback, when it is a function, it is a processing function after a successful call. It can also be an object containing several functions. In this case, the remote call should be written as follows:
Remote. Method (Params ,{
Callback: function (data) {// processing function when the call is successful
// The client logic after the server executes, as shown in figure
Alert ("data returned after server execution:" + data );
},
Errorhandler: function (errorstring, exception) {// when an exception occurs
Alert ('error occurred when modifying XXX. Please check XXX cloud: '+ errorstring)
}
}
);
More deeply, callback cannot include the other two function attributes: predictionhandler, warninghandler, and texthtmlhandler. Without the exceptionhandler handler, The errorhandler function will be applied, and the JS error in the callback function will also be executed by errorhandler.
Again, it can be stored in DWR. configure convert related to exceptions in XML, such as: <convert match = "Java. lang. exception "converter =" exception "/>. Use errorhandler's errorstring and exception to obtain more detailed information.