TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
写一个工具类: package jsfdb.model;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ModelUtils {
//注意如何访问资源属性文件的位置
public static final String RESOURCES= ModelUtils.class.getPackage().getName() + ".res.ModelResources";
private static ResourceBundle resources;
public static void log(Throwable x) {
Logger.global.log(Level.SEVERE, x.getMessage(), x);
}
public static synchronized ResourceBundle getResources() {
if (resources == null)
try {
resources = ResourceBundle.getBundle(RESOURCES);
} catch (MissingResourceException x) {
log(x);
throw new InternalError(x.getMessage());
}
return resources;
}
public static String getResource(String key) {
return getResources().getString(key);
}
}
[/code] 在JSP中调用:
<%@ page contentType="text/HTML; charset=GBK" %>
<%@ page import="java.util.Locale" %>
<%@ page import="jsfdb.model.*" %>
<%
String s2=ModelUtils.getResource("indexWindowTitle");
String s3=ModelUtils.getResource("indexPageTitle");
out.println("窗口标题="+s2+"<br>");
out.println("页面标题="+s3+"<br>"); out.println(ModelUtils.RESOURCES);
%>
运行结果:
窗口标题=JSF测试
页面标题=请填写下列的信息
jsfdb.model.res.ModelResources 附:ModelResources_zh_CN.properties
indexWindowTitle=JSFu6d4bu8bd5
indexPageTitle=u8bf7u586bu5199u4e0bu5217u7684u4fe1u606f
源码下载:http://file.javaxxz.com/2014/10/1/060130719.zip |
|