JSF ActionListener示例

2018-02-22 12:22 更新

JSF教程 - JSF ActionListener示例


我们可以处理用户点击事件为h:commandButton或h:link。

要注册事件处理程序,我们可以传递UI Component的actionListener属性中的托管bean方法的名称。

或者我们可以选择实现ActionListener接口,并将实现类名称传递给UI Component的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.tutorialspoint.test.UserActionListener" />
</h:commandButton>

例子

以下代码来自ActionListener.java。

package cn.w3cschool.common;
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?");
  
  }
  
}

以下代码来自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>

以下代码来自demo.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="cn.w3cschool.common.MyActionListener" />
      </h:commandButton>
    </h:form>
    </h:body>
</html>

下面的代码来自UserBean.java。

package cn.w3cschool.common;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
 
@ManagedBean(name="normal")
@SessionScoped
public class UserBean implements java.io.Serializable{

  public String buttonId="www.w3cschool.cn"; 
  
  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";
  }
}
下载 ActionListener.zip

运行

将生成的WAR文件从目标文件夹复制到Tomcat部署文件夹并运行Tomcat-Install-folder/bin/startup.bat。

Tomcat完成启动后,在浏览器地址栏中键入以下URL。

http://localhost:8080/simple-webapp/demo.xhtml
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号