In the first two articles, we have introduced the method of Dwr zero configuration culture and the front-end calling back-end methods. I believe everyone should be proficient in it. Let’s mainly discuss how the back-end calls the front-end js method in reverse;
As mentioned in the previous two articles, two Service components are registered with Dwr, one is remote, and the other is the controller used by the page. This remote will be called as soon as the page is loaded, which allows the session and the page ScriptSession to establish a binding relationship, so that we can use its scriptSessionId for page directional push later;
The following is the method to create a session and page ScriptSession (the page will be called when it is loaded):
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 the Service method used to reversely call the front-end:
@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, and this scriptSessionId uses a global map pair that was saved to when we first loaded the page;
Just use the registered Service bean when you want to call it. The parameters passed in are HttpSession and the front-end funciton name you want to call. At the same time, note that the Js function exists on the current page, and the methods in the Js of the main page can be accessed, otherwise the front-end will prompt an error of undefined this method;
The above is a simple application of Dwr. It is purely the first time I use it. If there is anything wrong, please correct me. I am grateful! God, please float by~~~~~;