站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题jsp中访问资源包

jsp中访问资源包

摘要: jsp中访问资源包

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="736" border="0"> <tr> <td width="730"> </td> </tr> </table>

写一个工具类:

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); } }
在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+"
");
out.println("页面标题="+s3+"
");

out.println(ModelUtils.RESOURCES);
%>

运行结果:

窗口标题=JSF测试
页面标题=请填写下列的信息
jsfdb.model.res.ModelResources

附:ModelResources_zh_CN.properties
indexWindowTitle=JSF\u6d4b\u8bd5
indexPageTitle=\u8bf7\u586b\u5199\u4e0b\u5217\u7684\u4fe1\u606f

</td> </tr> <tr>


↑返回目录
前一篇: java统计当前在线人数
后一篇: rm播放器与jsp