在JSF中,我们可以处理如<h:commandButton>或<h:link>组件的用户点击事件。要注册事件处理程序,我们可以在UI组件的actionListener属性中传递托管bean方法的名称。
或者也可以选择实现ActionListener接口,并将实现类名称传递给UI 组件的actionListener属性。
以下代码显示了如何从<h:commandButton>向actionListener属性添加用户定义的方法。
public void updateData(ActionEvent e){
   data="Hello World";
}
使用上述方法
<h:commandButton id="submitButton" 
   value="Submit" action="#{userData.showResult}"
   actionListener="#{userData.updateData}" />
</h:commandButton>
以下代码显示了如何实现ActionListener并使用f:actionListener标签。
public class UserActionListener implements ActionListener{
   @Override
   public void processAction(ActionEvent arg0)
   throws AbortProcessingException {
      //access userData bean directly
      UserData userData = (UserData) FacesContext.getCurrentInstance().
         getExternalContext().getSessionMap().get("userData"); 
      userData.setData("Hello World");
   }
}
使用侦听器方法 -
<h:commandButton id="submitButton1" 
   value="Submit" action="#{userData.showResult}" >
   <f:actionListener type="com.zaixian.test.UserActionListener" />
</h:commandButton>
实例
打开NetBeans,创建一个名称为:Actionlistener 的Web项目,其结构如下所示 -

以下是文件:User.java 文件中的代码 - 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zaixian;
/**
 *
 * @author Maxsu
 */
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
@ManagedBean(name = "normal")
@SessionScoped
public class User implements java.io.Serializable {
    public String buttonId = "xuhuhu.com";
    public String getButtonId() {
        return buttonId;
    }
    public void setButtonId(String buttonId) {
        this.buttonId = buttonId;
    }
    public void printIt(ActionEvent event) {
        //Get submit button id
        buttonId = event.getComponent().getClientId();
    }
    public String outcome() {
        return "result";
    }
}
以下是文件:MyActionListener.java 文件中的代码 - 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zaixian;
/**
 *
 * @author Maxsu
 */
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
public class MyActionListener implements ActionListener {
    @Override
    public void processAction(ActionEvent event)
            throws AbortProcessingException {
        System.out.println("Any use case here?");
    }
}
以下是文件:index.xhtml 文件中的代码 - 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      >
    <h:body>
        <h:form id="form">
            <ui:remove>  
                <h:commandButton id="submitButton" 
                                 value="Submit" action="#{normal.outcome}" 
                                 actionListener="#{normal.printIt}" />
            </ui:remove>
            <h:commandButton id="submitButton" 
                             value="Submit" action="#{normal.outcome}" >
                <f:actionListener type="com.zaixian.MyActionListener" />
            </h:commandButton>
        </h:form>
    </h:body>
</html>
以下是文件:result.xhtml 文件中的代码 - 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >
    <h:body>
        #{normal.buttonId}
    </h:body>
</html>
运行项目
在Actionlistener 项目上点击右键,选择 【运行】,在Tomcat启动完成后,打开浏览器访问以下地址:
http://localhost:8084/Actionlistener/
如果程序没有错误,应该会看到如下界面 -

点击上面的按钮后,应该会看到如下结果 -

						上一篇:
								JSF值变化的事件
												下一篇:
								JSF Facelets技术介绍
												
						
						
					
					
					