1.创建一个properties文件.
文件名为MyMessages.properties
可以放在任意位置,比如放在:com.abc.messages包中
2.在properties文件中添加内容.
内容格式为:变量名=值
如在MyMessages.properties中添加
title=我的主页
3.在faces-config.xml中配置.
<application>
<message-bundle>com.abc.messages.MyMessages</message-bundle>
</application>
4.在JSF页面中使用.
先在JSF页面中绑定刚才写的MyMessages.properties文件
<f:loadBundle var="msg" basename="com.chengyi.common.Messages"/>
5.JSF使用
一个完整的例子:
1.MyMessages.properties文件代码如下:
title=我的主页
2.faces-config.xml文件内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
<!--配置MyMessages.properties-->
<application>
<message-bundle>com.chengyi.common.Messages</message-bundle>
</application>
</faces-config>
3.JSF页面代码如下:
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<f:loadBundle var="msg" basename="com.chengyi.common.Messages"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <f:view> <head>
<title><h:outputText value="#{msg.registerTitle}" /></title>
</head>
<body>
</body>
</f:view>
</html>