JSF操作事件


在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.yiibai.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.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 = "tw511.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>

執行專案

Actionlistener 專案上點選右鍵,選擇 【執行】,在Tomcat啟動完成後,開啟瀏覽器存取以下地址:

http://localhost:8084/Actionlistener/

如果程式沒有錯誤,應該會看到如下介面 -

點選上面的按鈕後,應該會看到如下結果 -