如果使用者改变了JSF输入组件的值后送出窗体,就会发生值变事件(Value Change Event),这会丢出一个javax.faces.event.ValueChangeEvent对象,如果您想要处理这个事件,有两种方式,一是直接设定JSF输入组件的valueChangeListener属性,例如:
<h:selectOneMenu value="#{user.locale}"
οnchange="this.form.submit();"
valueChangeListener="#{user.changeLocale}">
<f:selectItem itemValue="zh_CN" itemLabel="Chinese"/>
<f:selectItem itemValue="en" itemLabel="English"/>
</h:selectOneMenu>
为选择了选单项目之后就立即发生反应,我们在onchange属性中使用了JavaScript,其作用是在选项项目发生改变之后,立即送出窗体,而不用按下提交按钮;而valueChangeListener属性所绑定的user.changeLocale方法必须接受ValueChangeEvent对象,例如:UserBean.java
package wsz.ncepu;
import javax.faces.event.ValueChangeEvent;
public class UserBean {
public String locale;
private String name;
private String password;
private String errMessage;
public void changeLocale(ValueChangeEvent event) {
if ("en".equals(locale))
locale = "zh_CN";
else
locale = "en";
}
public void setLocale(String locale) {
this.locale = locale;
}
public String getLocale() {
return this.locale;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setErrMessage(String errMessage) {
this.errMessage = errMessage;
}
public String getErrMessage() {
return errMessage;
}
public String verify() {
if (!name.equals("justin") || !password.equals("123456")) {
errMessage = "错误 ";
return "failure";
} else {
return "success";
}
}
}
另一个方法是实作javax.faces.event.ValueChangeListener接口,并定义其processValueChange()方法,例如:
SomeListener.java
package wsz.ncepu;
import javax.faces.event.ValueChangeEvent;
import javax.faces.event.ValueChangeListener;
public class SomeListener implements ValueChangeListener {
public void processValueChange(ValueChangeEvent event) {
System.out.println("en".equals(wsz.ncepu.UserBean.locale));
if ("en".equals(wsz.ncepu.UserBean.locale))
wsz.ncepu.UserBean.locale = "zh_CN";
else
wsz.ncepu.UserBean.locale = "en";
}
}
然后在JSF页面上使用<f:valueChangeListener>卷标,并设定其type属性,例如:
<h:selectOneMenu value="#{user.locale}"
οnchange="this.form.submit();">
<f:valueChangeListener type="wsz.ncepu.SomeListener" />
<f:selectItem itemValue="zh_CN" itemLabel="Chinese" />
<f:selectItem itemValue="en" itemLabel="English" />
</h:selectOneMenu>
完整的index.jsp
<%@ 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=UTF8"%>
<f:view locale="#{user.locale}">
<html>
<head>
<title><h:outputText value="#{msgs.titleText}" />
</title>
</head>
<body>
<f:loadBundle basename="messages" var="msgs" />
<h:form>
<h:selectOneMenu value="#{user.locale}"
οnchange="this.form.submit();">
<f:valueChangeListener type="wsz.ncepu.SomeListener" />
<f:selectItem itemValue="zh_CN" itemLabel="Chinese" />
<f:selectItem itemValue="en" itemLabel="English" />
</h:selectOneMenu>
<h3>
<h:outputText value="#{msgs.hintText}" />
</h3>
<h:outputText value="#{msgs.nameText}" />:
<h:inputText value="#{user.name}" />
<p>
<h:outputText value="#{msgs.passText}" />
:
<h:inputSecret value="#{user.password}" />
<p>
<h:commandButton value="#{msgs.commandText}"
action="#{user.verify}" />
</h:form>
</body>
</html>
</f:view>