Dwr3.0純註解(純Java Code配置)配置與應用淺析三之後端反向調用前端_java

來源:互聯網
上載者:User

在前兩篇中我們已經介紹了Dwr零配置文化化和前端調用後端的方法,想必大家應該已經會熟練掌握了,下來我們主要探討一下後端怎麼反向調用前端的js方法;

就如前兩篇說到了用Dwr註冊了兩個Service組件,一個是remote,另外一個是頁面使用到的controller,這個remote是在頁面一載入進來就會被調用的,這樣就使得會話和頁面ScriptSession建立了綁定的關係,方便我們後面使用它的scriptSessionId進行頁面定向推送;

下面是建立會話和頁面ScriptSession的方法(頁面剛載入進來的時候就會被調用的):  

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);
下面是用來反向調用前端的Service方法:
@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, "");}});}}

這個Service是利用了scriptSessionId來進行反向定位推送的,而這個scriptSessionId是利用我們之前頁面剛載入進來就建立好儲存到的一個全域的Map對裡面;

下來只要在你想要什麼時候調用的時候使用註冊的這個Service bean就可以了,傳進去的參數為HttpSession和你想調用的前端Js funciton name,同時注意一下當前頁面存在這個Js function,而且一般是首頁面的Js裡面的方法才能被訪問到,否則前端會提示該方法未定義的錯誤;

以上就是關於Dwr的簡單應用,純屬第一次用,有什麼不對的地方,請大家指正出來,感激不盡啊!大神請飄過~~~~~;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.