一、jsp從配置文件*.properties讀取信息
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% ResourceBundle resource = ResourceBundle.getBundle("config"); <!--配置文件名-->
String logoPath =ctx + resource.getString("System_Logo");
request.setAttribute("logoPath", logoPath);
%> <title> <!-- 直接輸出配置值 --> <%=resource.getString("System_Name") %> <!-- 由於properties配置文件默認的編碼為:ISO-8859-1,是不支持中文的,會亂碼 --> <%=new String(resource.getString("System_Name").getBytes("ISO-8859-1"), "UTF8") %> </title> <body> <script type="text/javascript"> // 賦值給js變量 var systemName ='resource.getString("System_Name")';
或
'${logoPath }'
</script> </body>
import屬性用於導入java中的包,import屬性可以指定多個值,這些值之間需要用逗號(,)進行分隔。
表示 java.util包 中的所有類在使用時無需給出明確的包標識符。
jsp中import 是 page 的屬性中惟一允許在同一文檔中多次出現的屬性。盡管 page 指令可以出現在文檔中的任何地方,但一般不是將 import 語句放在文檔頂部附近,就是放在相應的包首次使用之前。
java:
通過 java.util.ResourceBundle 類來讀取,這種方式比使用 Properties 要方便一些
1>通過 ResourceBundle.getBundle() 靜態方法來獲取(ResourceBundle是一個抽象類),這種方式來獲取properties屬性文件不需要加.properties后綴名,只需要文件名即可
public static ResourceBundle resource = ResourceBundle.getBundle("config"); public static String system_Type_Config = resource.getString("System_Type");
EL表達式:
<% request.setAttribute("logoPath", logoPath); %> //設置值
${logoPath} //取值
