先建个监听器:
package com.yjd.hy.server;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 应用上下文监听器,读取配置文件、字典表
*
*/
public class MyServletContextListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
/** 读取配置文件 **/
String specialPath = System.getProperty("search.root")
+ "/WEB-INF/properties/xxx.properties";
Properties props = new Properties();
try {
props.load(new FileInputStream(specialPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Map<String, String> ddMap = new HashMap<String, String>();
Iterator itr = props.entrySet().iterator();
while (itr.hasNext()) {
Entry e = (Entry) itr.next();
ddMap.put(e.getKey().toString(), e.getValue().toString());
}
arg0.getServletContext().setAttribute("xxx_dict_data", ddMap);
}
}
在Web.xml中配置监听器:
<!-- 加载全局配置文件、字典数据的监听器 -->
<listener>
<listener-class>com.yjd.hy.server.MyServletContextListener</listener-class>
</listener>
Java中获取:
Object obj=ServletActionContext.getServletContext().getAttribute("xxx_dict_data");
Jsp页面中直接获取:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
${xxx_dict_data['aaa.bbb']}
</body></html>
分享到:
相关推荐
java 枚举遍历键值对 属性类Properties 类加载器
java 改变Properties文件中的键值 java 改变Properties文件中的键值
在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,有很多如系统配置信息,java如何遍历properties文件呢,本文将详细介绍,希望可以帮助到您
解决 Struts 中 ApplicationResources.properties 文件不支持中文问题 Struts 框架中,ApplicationResources.properties 文件是用于存储应用程序的资源文件,如按钮文字、菜单项、提示信息等。然而,默认情况下,...
本文侧重于了解自定义properties文件,以及properties中的数据格式,在gradle中如何使用等知识点。 一、概述 在Android Studio 创建一个项目的时候,rootProject下面会生成gradle.properties和local.properties文件...
activiti和springboot整合只使用application.properties配置文件,解决了jdbc长时间待机连接被收回报错。使用springProcessEngineConfiguration对activiti管理
IDEA WEB项目启动不加载application.properties配置文件,yml配置文件是否也会不加载未尝试,如果有遇到的可以尝试一下。
js读取properties文件,很好用的。js读取properties文件,很好用的。js读取properties文件,很好用的。js读取properties文件,很好用的。
主要介绍了Java如何遍历Properties所有元素的方法,大家可以参考使用
java写的,用来比较properties文件的工具
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
Android存取键值对的代码例子。通过利用SharedPreferences和Properties两种方式,演示了如何实现键值对保存和读取的功能。
读取properties文件返回map
NULL 博文链接:https://b275518834201204034558.iteye.com/blog/1547588
Properties 文件文中乱码问题解决 Properties 文件文中乱码问题解决是指在使用 Struts 框架进行国际化操作时,对于中文资源文件的处理问题。在 Properties 文件中,中文字符可能会变成乱码,这是因为 Eclipse 中的...
properties文件读写操作
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
进行properties文件的批量处理工具,能够用来改动部分参数而进行的文本提取修改工具。
设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法
eclipse下properties文件编辑插件