package com.ydq.read.propertyfile; import java.io.InputStream; import java.util.MissingResourceException; import java.util.Properties; import java.util.ResourceBundle; /** * 读取配置文件中的参数 * * 一般情况下,首页的内容是比较比较多,但内容结构大多也有本同的地方,比方说某几块内容都是展示的文章,只是文章来源的类别不同,或者说某几个版面都展示商品, * 只是商品所属分类不同罢了,那么我们在后台取数据时,都需要根据类别ID来取值的。一般做法都是把这些不同的来源ID在代码中写好,这种做法又分为以下三种情况: * * 1、前台写死ID,通过<jsp:include page="xx.do?id=xx">,可以进行动态的包含内容。 * * 2、可在service层写一个id的数组,String[] ids = * {“”,"",""};用ids数组的下标来获得id值,这样也可以实现,如果数据库的ID值发生改变,只需要改变service层的ID数组就行。 * * 3、用配置文件(这个配置文件以.properties结尾)。将ID值写在配置文件中,表示的方法就和map一样,key=value,这样就OK。 * * 下面我要说的是第三种方案,个人觉得第三种方案比第二种方案好,因为第二种方案改数据时,去找的话,有点不太方便。 * * @author 姚大庆 * */ public class CommonParam { private String propertyFileName; private ResourceBundle resourceBundle; public CommonParam() { propertyFileName = "ydqfile"; resourceBundle = ResourceBundle.getBundle(propertyFileName); } public String getString(String key) { if (key == null || key.equals("") || key.equals("null")) { return ""; } String result = ""; try { result = resourceBundle.getString(key);// 根据key获取value } catch (MissingResourceException e) { e.printStackTrace(); } return result; } public static void main(String[] args) { /* * 方法一:直接通过java.util.ResourceBundle读取配置文件 * CommonParam test = new CommonParam(); * System.out.println(test.getString("ydqa")); * System.out.println(test.getString("ydqb")); * System.out.println(test.getString("ydqc")); */ /* * 方法二:通过java中java.util.Properties对象读取配置文件 * */ CommonParam test = new CommonParam(); System.out.println(test.getPara("ydqfile.properties")); } /** * 学习JAVA中java.util.Properties对象读取配置文件 * @param fileName 配置文件名 * @return 根据key返回value */ public String getPara(String fileName) { Properties prop = new Properties(); try { InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName); prop.load(is); if (is != null) is.close(); } catch (Exception e) { System.out.println(e + " file " + fileName + " not found"); } return prop.getProperty("ydqb"); } }
详细解决方案
读取配置文件中的参数(怎么设计JSP页面读取配置文件中参数)
热度:304 发布时间:2012-06-26 10:04:13.0
相关解决方案
- spring 配置文件 版本宣言
- hibernate 配置文件 pojo 工具 hibernate tools-3.0,该如何处理
- 【转】mac 系统上eclipse 配置文件
- 企业搬动应用平台-配置文件
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!该怎么处理
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!该如何处理
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!解决方案
- Struts2――(二)配置文件、通配符
- Spring 配置文件(组合ibatis)
- dwr 2.0兑现网页消息发送(配置文件)
- 配置文件(fckconfig.js)中主要配置项目如上
- c3p0 配置文件 寄放路径
- Hibernate(3)结构-配置文件-实体映射及配置文件
- myEclipse8.5调整SSH详细步骤+配置文件
- 怎么利用 myeclipse 生成Hibernate 配置文件
- Jar后:FileInputStream、配置文件 不能读入解决办法
- ThinkPHP数据模板展示——系统变量、常量、配置文件
- Emacs 配置文件,包括自动提示,yasnippet跟PHP-mod和cscope等
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!解决思路
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!该怎么处理
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!该如何处理
- 织梦5.5改系统基本参数时,配置文件'D:/wwwroot/sc-tnb/data/config.cache.inc.php'不支持写入,无法修改系统配置参数!解决方案
- spring(1)-spring/springmvc/spring+hibernate(mybatis)配置文件
- spring boot 外部固定目录 配置文件
- Mybatis -代码自动生成(generatorConfig.xml)配置文件
- 详解 Tomcat 配置文件 server.xml
- JDBC---连接数据库(配置文件)
- 如何写 Logback 配置文件 ?
- Qt: 通过注册表类QSetinggs存储数据,配置文件.
- Python3 配置文件(configparser)