Struts2中實(shí)現(xiàn)web應(yīng)用的初始化實(shí)例詳解
在JavsSE中,main方法為應(yīng)用提供了入口,而在Android中,我們可以使用Application對于整個(gè)應(yīng)用的生命周期進(jìn)行管理,那么在基于Struts2的JavaEE應(yīng)用中,如何實(shí)現(xiàn)類似的功能呢。
其中一種比較好的方式,是通過實(shí)現(xiàn)ServletContextListener接口進(jìn)行堅(jiān)挺,重寫contextInitialized方法,實(shí)現(xiàn)自己需要進(jìn)行的初始化操作,之后在web.xml中添加相應(yīng)的listner,tomcat在啟動(dòng)服務(wù)時(shí)會(huì)調(diào)用相應(yīng)方法。
lintener 代碼:
package listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class InitListener implements ServletContextListener { public void contextDestroyed(ServletContextEvent sce) { System.out.println("web exit ... "); } public void contextInitialized(ServletContextEvent sce) { System.out.println("web init ... "); //系統(tǒng)的初始化工作 //TODO } }
web.xml
?xml version="1.0" encoding="UTF-8"?> web-app> listener> listener-class>fangwei.listener.InitListener/listener-class> /listener> filter> filter-name>struts2/filter-name> filter-class>org.apache.struts2.dispatcher.FilterDispatcher/filter-class> /filter> filter-mapping> filter-name>struts2/filter-name> url-pattern>/*/url-pattern> /filter-mapping> /web-app>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
標(biāo)簽:南京 廣州 大連 銅川 貸款邀約 樂山 黃石 內(nèi)江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Struts2中實(shí)現(xiàn)web應(yīng)用的初始化實(shí)例詳解》,本文關(guān)鍵詞 Struts2,中,實(shí)現(xiàn),web,應(yīng),用的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。