如何遍历properties文件的键值对并放置到application作用域里

先建个监听器:

[java]  view plain copy
  1. package com.yjd.hy.server;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.IOException;  
  6. import java.util.HashMap;  
  7. import java.util.Iterator;  
  8. import java.util.Map;  
  9. import java.util.Properties;  
  10. import java.util.Map.Entry;  
  11.   
  12. import javax.servlet.ServletContextEvent;  
  13. import javax.servlet.ServletContextListener;  
  14.   
  15. /** 
  16.  * 应用上下文监听器,读取配置文件、字典表 
  17.  *  
  18.  */  
  19. public class MyServletContextListener implements ServletContextListener {  
  20.   
  21.     public void contextDestroyed(ServletContextEvent arg0) {  
  22.     }  
  23.   
  24.     public void contextInitialized(ServletContextEvent arg0) {  
  25.         /** 读取配置文件 **/  
  26.         String specialPath = System.getProperty("search.root")  
  27.                 + "/WEB-INF/properties/xxx.properties";  
  28.         Properties props = new Properties();  
  29.         try {  
  30.             props.load(new FileInputStream(specialPath));  
  31.         } catch (FileNotFoundException e) {  
  32.             e.printStackTrace();  
  33.         } catch (IOException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.   
  37.         Map<String, String> ddMap = new HashMap<String, String>();  
  38.   
  39.         Iterator itr = props.entrySet().iterator();  
  40.         while (itr.hasNext()) {  
  41.             Entry e = (Entry) itr.next();  
  42.             ddMap.put(e.getKey().toString(), e.getValue().toString());  
  43.         }  
  44.         arg0.getServletContext().setAttribute("xxx_dict_data", ddMap);  
  45.     }  
  46.   
  47. }  


在Web.xml中配置监听器:

[html]  view plain copy
  1. <!-- 加载全局配置文件、字典数据的监听器 -->  
  2. <listener>  
  3.     <listener-class>com.yjd.hy.server.MyServletContextListener</listener-class>  
  4. </listener>  


Jsp页面中直接获取:
[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'MyJsp.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.   
  20.   </head>  
  21.     
  22.   <body>   
  23.     ${xxx_dict_data['aaa.bbb']} <br>  
  24.   </body>  
  25. </html>  

Java中获取:

[java]  view plain copy
  1. Object obj=ServletActionContext.getServletContext().getAttribute("<span style="font-family: Arial, Helvetica, sans-serif;">xxx_dict_data</span>");  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值