1、properties文件位置: easemob.properties
2、easemob.properties 内容:
path=asdfg
3、jsp代码部分:
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="utf-8"%>
<%
Properties pro = new Properties();
String realpath = request.getRealPath("/WEB-INF/classes/config");
try{
//读取配置文件
FileInputStream in = new FileInputStream(realpath + "/easemob.properties");
pro.load(in);
}
catch(FileNotFoundException e){
out.println(e);
}
catch(IOException e){out.println(e);}
//通过key获取配置文件
String path = "";
path = pro.getProperty("path");
//byte b[]=title.getBytes("utf-8");
//title=new String(b);
%>
4、js代码
<script type="text/javascript">
var path = '<%=path%>';
alert(path);
</script>
5、上面是通过jsp读取properties,js只是获取了jsp里的变量的值。js直接读取properties方法如下:
用JS来读取properties文件,需要引入一个jQuery的插件jQuery.i18n.properties。这个插件主要是做国际化处理的,国际化一般是放到Properties文件中配置的,所以jquery提供了这个插件来读取properties文件。先把jquery引入js,再引入jquery.i18n.properties,将properties放置到某个位置,加上下面的代码就可以读取到了。
function loadProperties(){
JQuery.i18n.properties({//加载properties文件
name:'syscfg',//properties文件的名称
path:'config/',//properties文件的路径
mode:'map',//用map的方式使用资源文件中的值
callback:function(){
console.log($.i18n.prop("landUrl"));//根据key值取得需要的资源
}
});
}
默认加载的时候,其实需要加载三个properties文件,分别是syscfg.properties、syscfg_zh.properties和syscfg_zh_CN.properties,其实就是因为国际化的处理。