Dwr3.0 pure annotation of pure Java Code configuration configuration and application analysis three back end reverse call front end

  • 2020-05-09 18:38:08
  • OfStack

In the first two articles, we have introduced the zero configuration culture of Dwr and the method of front end calling the back end. I think you should have mastered it. Next, we will mainly discuss how the back end calls the front end's js method in reverse.

As mentioned in the previous two articles, Dwr is used to register two Service components, one is remote, and the other is controller used by the page. This remote will be called when the page is loaded into the page 1. In this way, a binding relationship is established between the session and the page ScriptSession, which is convenient for us to use its scriptSessionId for page directional push.

Here's how to set up the session and the page ScriptSession (which will be called as soon as the page loads in) :    


ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); 
String jsessionId = scriptSession.getHttpSessionId(); 
String dwrSessionId = scriptSession.getId(); 
LOGGER.info(String.format("set jsessionId = [%s],dwrsession = [%s] push enabled",jsessionId,dwrSessionId)); 
ConstantCacheService.putCacheConstant(Constants.PUSH_ID+jsessionId, dwrSessionId);

 The following is used to call the front end in reverse Service Methods: 

@Service("dwrReverseAjaxService")
public class DwrReverseAjaxService {
private static final Logger LOGGER = LoggerFactory.getLogger(DwrReverseAjaxService.class);
public void directWebRemotingWithSession(HttpSession session, final String functionName) 
{Assert.notNull(session, "[Dwr Reverse Ajax] Session can not be null!");
final String scriptSessionId = ConstantCacheService.getCacheConstantValue(Constants.PUSH_ID + session.getId());
LOGGER.info("[DWR Session ID] = " + scriptSessionId + " [Script Function Name] = " + functionName);Browser.withSession(scriptSessionId, new Runnable() {
public void run() {ScriptSessions.addFunctionCall(functionName, "");
}});}}

This Service USES scriptSessionId for reverse positioning and pushing, while this scriptSessionId USES a global Map pair that we set up and saved as soon as the page was loaded.

Down as long as when you want to call with registration of this Service bean, announced in parameters as HttpSession and you want to call the front end of the Js funciton name, note 1 at the same time under the current page is the Js function, and 1 kind is home page below Js method can be accessed to the inside, or front end will prompt the method undefined error;

The above is the simple application of Dwr, purely for the first time. If there is anything wrong, please correct it. Thank you very much! Please float over ~~~~~;


Related articles: