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í)會調(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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:- JavaWeb中Struts2攔截器深入分析(一)
- Struts2學(xué)習(xí)筆記(7)-訪問Web元素
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析