將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
四大作用域:
Web應(yīng)用中的變量存放在不同的jsp對(duì)象中,會(huì)有不一樣的作用域,四種不同的作用域排序是 pageContext < request < session < application;
1、pageContext:頁(yè)面域,僅當(dāng)前頁(yè)面有效,離開頁(yè)面后,不論重定向還是轉(zhuǎn)向(即無(wú)論是redirect還是forward),pageContext的屬性值都失效;
2、request:請(qǐng)求域,在一次請(qǐng)求中有效,如果用forward轉(zhuǎn)向,則下一次請(qǐng)求還可以保留上一次request中的屬性值,而redirect重定向跳轉(zhuǎn)到另一個(gè)頁(yè)面則會(huì)使上一次request中的屬性值失效;
3、session:會(huì)話域,在一次會(huì)話過(guò)程中(從瀏覽器打開到瀏覽器關(guān)閉這個(gè)過(guò)程),session對(duì)象的屬性值都保持有效,在這次會(huì)話過(guò)程,session中的值可以在任何頁(yè)面獲取;
4、application:應(yīng)用域,只要應(yīng)用不關(guān)閉,該對(duì)象中的屬性值一直有效,并且為所有會(huì)話所共享。
利用ServletContextListener監(jiān)聽器,一旦應(yīng)用加載,就將properties的值存儲(chǔ)到application當(dāng)中
現(xiàn)在需要在所有的jsp中都能通過(guò)EL表達(dá)式讀取到properties中的屬性,并且是針對(duì)所有的會(huì)話,故這里利用application作用域,
那么什么時(shí)候?qū)roperties中的屬性存儲(chǔ)到application呢?因?yàn)槭菍roperties的屬性值作為全局的變量以方便任何一次EL的獲取,所以在web應(yīng)用加載的時(shí)候就將值存儲(chǔ)到application當(dāng)中,
這里就要利用ServletContextListener:
ServletContextListener是Servlet API 中的一個(gè)接口,它能夠監(jiān)聽 ServletContext 對(duì)象的生命周期,實(shí)際上就是監(jiān)聽 Web 應(yīng)用的生命周期。
當(dāng)Servlet 容器啟動(dòng)或終止Web 應(yīng)用時(shí),會(huì)觸發(fā)ServletContextEvent 事件,該事件由ServletContextListener 來(lái)處理。
具體步驟如下:
1、新建一個(gè)類PropertyListenter實(shí)現(xiàn) ServletContextListener接口的contextInitialized方法;
2、讀取properties配置文件,轉(zhuǎn)存到Map當(dāng)中;
3、使用ServletContext對(duì)象將Map存儲(chǔ)到application作用域中;
/** * 設(shè)值全局變量 * @author meikai * @version 2017年10月23日 下午2:15:19 */ public class PropertyListenter implements ServletContextListener { /* (non-Javadoc) * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) */ @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) */ @Override public void contextInitialized(ServletContextEvent sce) { /** * 讀取properties文件 * */ final Logger logger = (Logger) LoggerFactory.getLogger(PropertyListenter.class); Properties properties = new Properties(); InputStream in = null; try { //通過(guò)類加載器進(jìn)行獲取properties文件流 in = PropertiesUtil.class.getClassLoader().getResourceAsStream("kenhome-common.properties"); properties.load(in); } catch (FileNotFoundException e) { logger.error("未找到properties文件"); } catch (IOException e) { logger.error("發(fā)生IOException異常"); } finally { try { if(null != in) { in.close(); } } catch (IOException e) { logger.error("properties文件流關(guān)閉出現(xiàn)異常"); } } /** * 將properties文件轉(zhuǎn)存到map */ Map<String, String> pros = new HashMap<String,String>((Map)properties); /** * 將Map通過(guò)ServletContext存儲(chǔ)到全局作用域中 */ ServletContext sct=sce.getServletContext(); sct.setAttribute("pros", pros); } }
4、在web.xml中配置上面的的監(jiān)聽器PropertyListenter:
<!-- 全局變量監(jiān)聽器,讀取properties文件,設(shè)值為全局變量 --> <listener> <listener-class>com.meikai.listener.PropertyListenter</listener-class> </listener>
配置好后,運(yùn)行Web應(yīng)用,就能在所有的jsp頁(yè)面中用EL表達(dá)式獲取到properties中的屬性值了。
以上這篇將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持。
相關(guān)文章:
1. python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析2. 快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載)3. Python requests庫(kù)參數(shù)提交的注意事項(xiàng)總結(jié)4. js抽獎(jiǎng)轉(zhuǎn)盤實(shí)現(xiàn)方法分析5. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子6. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程7. AJAX的跨域問(wèn)題解決方案8. IntelliJ IDEA導(dǎo)入jar包的方法9. 通過(guò)Python pyecharts輸出保存圖片代碼實(shí)例10. vue-electron中修改表格內(nèi)容并修改樣式
