1.在java的src下建立message.properties,内容如下:
globleTitleText = JSF Demo
indexInputTip = Please input your name and password:
indexNameTip = Name:
indexPasswordTip = Password:
indexCommandText = Submit
chooseLanguageTip = Please Choose Your Language:
这个是默认的资源文件,即JSF会根据浏览器送来的Accept-Language header中的内容来决定该使用哪一个资源档名称,例如:
Accept-Language: zh_cn, en-US, en
如果浏览器送来这些header,则预设会使用繁体中文,接着是美式英文,再来是英文语系,如果找不到对应的讯息资源文件,则会使用预设的讯息资源文件。
再来个中文的资源文件message_zh.properties,通过jdk的native2ascii程序转换,转换后大致如下 :
globleTitleText = JSF2.0\u793a\u4f8b
indexInputTip = \u8bf7\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801
indexNameTip = \u7528\u6237\u540d
indexPasswordTip = \u5bc6\u7801
indexCommandText = \u63d0\u4ea4
chooseLanguageTip =\u8bf7\u9009\u62e9\u60a8\u7684\u8bed\u8a00
2.接下来使用<f:loadBundle/>标签来加载资源文件,改造后的页面大致如下:
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@page contentType="text/html;charset=gb18030"%>
<f:view locale="#{user.local }">
<html>
<head>
<title><h:outputText value="#{msgs.globleTitleText}"/></title>
</head>
<body>
<f:loadBundle var="msgs" basename="message"/>
<h:outputText value="#{user.local }"/><br>
<h:form>
<h:outputText value="#{msgs.chooseLanguageTip }"/>
<h:selectOneMenu value="#{user.local }" style="width:120px;" οnchange="submit()">
<f:selectItems value="#{user.languageItems}"/>
<f:valueChangeListener type="onlyfun.caterpillar.UserBean"/>
</h:selectOneMenu>
</h:form>
<h:form>
<h3><h:outputText value="#{msgs.indexInputTip }"/></h3>
<h:outputText value="#{user.errMsg}"></h:outputText><br>
<h:outputText value="#{msgs.indexNameTip }"></h:outputText><h:inputText value="#{user.name}"/><br>
<h:outputText value="#{msgs.indexPasswordTip }"/><h:inputSecret value="#{user.password }"></h:inputSecret><br>
<h:commandButton value="#{msgs.indexCommandText}" action="#{user.verify }"/>
</h:form>
</body>
</html>
</f:view>
注:这个在页面上加了一个选择语言的下拉列表。
4.UserBean.java,实现了ValueChangeListener接口的processValueChange(ValueChangeEvent vce)方法,当下拉列表的值改变时调用这个方法。
package com.jsf.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
import javax.faces.model.SelectItem;
import javax.swing.JOptionPane;
public class UserBean implements ValueChangeListener{
private String name;
private String password;
private String errMsg;
private String local;
private List<SelectItem> languageItems= new ArrayList<SelectItem>();
public List<SelectItem> getLanguageItems() {
return this.languageItems;
}
public void setLanguageItems(List<SelectItem> languageItems) {
this.languageItems = languageItems;
}
public String getLocal() {
return this.local;
}
public void setLocal(String local) {
this.local = local;
}
public UserBean() {
languageItems.add(new SelectItem("","请选择您的语言"));
languageItems.add(new SelectItem("en","English"));
languageItems.add(new SelectItem("zh","Chinese"));
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getErrMsg() {
return this.errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
*校验用户名和密码的方法。
*/
public String verify() {
System.out.println("name:" + name + ",password:" + password);
if (name.equals("admin") && password.equals("123456")) {
return "success";
} else {
errMsg = "用户名或密码错误!";
return "failure";
}
}
// valueChangeListener事件接收的方法必须是public,void,并且有ValueChangeListener参数。r
public void processValueChange(ValueChangeEvent vce) {
System.out.println("set user language...");
JOptionPane.showMessageDialog(null, "您选择的语言:" + vce.getNewValue());
}
}
5.好了,看下效果:
默认情况:
选择语言为英语:
这里给了一个提示,单击确定,就可以看到效果了。