zoukankan      html  css  js  c++  java
  • JSF>事件处理值变事件 小强斋

    如果使用者改变了JSF输入组件的值后送出窗体,就会发生值变事件(Value Change Event),这会丢出一个javax.faces.event.ValueChangeEvent对象,如果您想要处理这个事件,有两种方式,一是直接设定JSF输入组件的valueChangeListener属性,例如:

    <h:selectOneMenu value="#{user.locale}"
                      onchange="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}"
    					onchange="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}"
    					onchange="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>
    


     

  • 相关阅读:
    互斥锁Mutex与信号量Semaphore的区别
    c/c++强制类型转换
    c++中的隐藏、重载、覆盖(重写)
    运算符重载详解
    类的大小
    C++ static、const和static const 以及它们的初始化
    一种隐蔽性较高的Java ConcurrentModificationException异常场景
    Java编码常见的Log日志打印问题
    Java编程常见缺陷汇总(一)
    Java字符串连接的多种实现方法及效率对比
  • 原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5429582.html
Copyright © 2011-2022 走看看