鍍金池/ 教程/ HTML/ JSF操作事件
JSF數(shù)據(jù)表(h:dataTable)添加刪除
JSF <h:commandLink>標(biāo)簽
JSF應(yīng)用程序入門示例
JSF數(shù)據(jù)表(ui:repeat)創(chuàng)建表
JSF列表框
JSF數(shù)據(jù)表(h:dataTable)DataModel排序數(shù)據(jù)
JSF復(fù)合組件
JSF <h:inputText>標(biāo)簽
JSF表單組合框
JSF <h:messages>標(biāo)簽
JSF <h:message>標(biāo)簽
JSF轉(zhuǎn)換日期時間
JSF JDBC連接
JSF <h:inputHidden>標(biāo)簽
JSF多選列表框
JSF <h:inputSecret>標(biāo)簽
JSF自定義轉(zhuǎn)換器
JSF <f:ajax>標(biāo)簽
JSF生命周期
JSF可重定位資源
JSFJSF用戶界面組件模型
JSF <h:attribute>標(biāo)簽
JSF驗(yàn)證器標(biāo)簽
JSF驗(yàn)證字符串長度
JSF轉(zhuǎn)換器標(biāo)簽
JSF托管bean(Managed Bean)
JSF值變化的事件
JSF UI組件示例
JSF MySQL CURD實(shí)例
JSF數(shù)據(jù)表(h:dataTable)排序數(shù)據(jù)
JSF <h:graphicImage>標(biāo)簽
JSF <f:convertNumber>標(biāo)簽
JSF教程
JSF托管Bean
JSF輸出腳本
JSF <h:outputText>標(biāo)簽
JSF操作事件
JSF驗(yàn)證正則表達(dá)式
JSF數(shù)據(jù)表(h:dataTable)行號
JSF <h:setPropertyActionListener>標(biāo)簽
JSF注入托管bean實(shí)例
JSF <h:commandButton>標(biāo)簽
JSF Web資源
JSF <h:inputFile>標(biāo)簽
JSF驗(yàn)證浮點(diǎn)數(shù)值范圍
JSF Facelets視圖
JSF是什么?
JSF Facelets模板
JSF的特性(特點(diǎn))
JSF自定義驗(yàn)證器類
JSF單選按鈕
JSF輸出樣式
JSF數(shù)據(jù)表(h:dataTable)更新數(shù)據(jù)
JSF HTML5友好標(biāo)記
JSF表單復(fù)選框(CheckBox)示例
JSF <h:form>標(biāo)簽
JSF Facelets技術(shù)介紹
JSF輸出格式化
JSF <h:inputtextarea>標(biāo)簽
JSF驗(yàn)證整數(shù)范圍
JSF <h:panelGrid>標(biāo)簽

JSF操作事件

在JSF中,我們可以處理如<h:commandButton><h:link>組件的用戶點(diǎn)擊事件。要注冊事件處理程序,我們可以在UI組件的actionListener屬性中傳遞托管bean方法的名稱。

或者也可以選擇實(shí)現(xiàn)ActionListener接口,并將實(shí)現(xiàn)類名稱傳遞給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>

以下代碼顯示了如何實(shí)現(xiàn)ActionListener并使用f:actionListener標(biāo)簽。

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.yiibai.test.UserActionListener" />
</h:commandButton>

實(shí)例

打開NetBeans,創(chuàng)建一個名稱為:Actionlistener 的Web項(xiàng)目,其結(jié)構(gòu)如下所示 -

以下是文件: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.yiibai;

/**
 *
 * @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 = "yiibai.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.yiibai;

/**
 *
 * @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.yiibai.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>

運(yùn)行項(xiàng)目

Actionlistener 項(xiàng)目上點(diǎn)擊右鍵,選擇 【運(yùn)行】,在Tomcat啟動完成后,打開瀏覽器訪問以下地址:

http://localhost:8084/Actionlistener/

如果程序沒有錯誤,應(yīng)該會看到如下界面 -

點(diǎn)擊上面的按鈕后,應(yīng)該會看到如下結(jié)果 -