在实际开发中,我们经常会将一些属性写入到properties文件中,方便我们在开发或维护中的操作。这样做的好处十分明显,当我们需求或运行环境发生变化时,只需更改相应的配置文件即可符合要求。但这样做的同时也带来了一些开发上的问题,比如我们如何获取properties文件中的值。
ApplicationProperties.properties配置文件内容:
FtpUrl = http\://192.168.0.14\:8080/
一、JSP文件中获取该属性值得方法:
<%@ page language="java" import="java.util.*,com.sdzn.entity.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ path + "/";
// properties 配置文件名称
ResourceBundle res = ResourceBundle.getBundle("ApplicationResources");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<title>测试</title>
<script type="text/javascript">
// 获取properties配置文件中的属性值
var ftpurl= <%=res.getString("FtpUrl ")%>;
</script>
</head>
<body ></body>
</html>
二、Java后台程序中获取该属性值得方法:
import java.util.PropertyResourceBundle;
// properties 配置文件名称
PropertyResourceBundle res = (PropertyResourceBundle) PropertyResourceBundle.getBundle("ApplicationResources");
// 获取properties配置文件中的属性值
String ftpurl = res.getString("FtpUrl ");