JSF入门十五(动作事件)
2013-04-14 22:11
148 查看
JSF的事件模型提供一个近似的桌面GUI事件方式,让熟悉GUI设计的人员也能快速上手Web程序设计。
如果您需要使用同一个方法来应付多种事件来源,并想要取得事件来源的相关信息,您可以让处理事件的方法接收一个javax.faces.event.ActionEvent事件参数,例如:UserBean.java
在上例中,我们让verify方法
接收一个ActionEvent对象,当使用者按下按钮,会自动产生ActionEvent对象代表事件来源,我们故意在错误信息之后如上事件来源的字符串描述,这样就可以在显示错误信息时一并显示事件来源描述。
为了提供ActionEvent的存取能力,您的index.jsp可以改写如下:
主要改变的是按钮上使用了actionListener属性,这种方法可以使用一个ActionListener,JSF会先检查是否有指定的actionListener,然后再检查是否指定了动作方法并产生预定义的ActionListener,并根据其传回值导航页面。
如果您要注册多个ActionListener,例如当使用者按下按钮时,顺便在记录文件中增加一些记录信息,您可以实现javax.faces.event.ActionListener,例如:LogHandler.java
VerifyHandler.java
这么一来,您就可以使用<f:actionListener>标签向组件注册事件,例如:
<f:actionListener>会自动产生type所指定的对象,并呼叫组件的addActionListener()方法注册Listener。
如果您需要使用同一个方法来应付多种事件来源,并想要取得事件来源的相关信息,您可以让处理事件的方法接收一个javax.faces.event.ActionEvent事件参数,例如:UserBean.java
package bruce.zhao; import javax.faces.event.ActionEvent; public class UserBean { private String name; private String password; private String errMessage; private String outcome; 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 void verify(ActionEvent e) { if(!name.equals("justin") || !password.equals("123456")) { errMessage = "名称或密码错误" + e.getSource(); outcome = "failure"; } else { outcome = "success"; } } public String outcome() { return outcome; } }
在上例中,我们让verify方法
接收一个ActionEvent对象,当使用者按下按钮,会自动产生ActionEvent对象代表事件来源,我们故意在错误信息之后如上事件来源的字符串描述,这样就可以在显示错误信息时一并显示事件来源描述。
为了提供ActionEvent的存取能力,您的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=Big5"%> <html> <head> <title>第一个JSF程序</title> </head> <body> <f:view> <h:form> <h3>请输入您的名称</h3> <h:outputText value="#{user.errMessage}"/><p> 名称: <h:inputText value="#{user.name}"/><p> 密码: <h:inputSecret value="#{user.password}"/><p> <h:commandButton value="送出" actionListener="#{user.verify}" action="#{user.outcome}"/> </h:form> </f:view> </body> </html>
主要改变的是按钮上使用了actionListener属性,这种方法可以使用一个ActionListener,JSF会先检查是否有指定的actionListener,然后再检查是否指定了动作方法并产生预定义的ActionListener,并根据其传回值导航页面。
如果您要注册多个ActionListener,例如当使用者按下按钮时,顺便在记录文件中增加一些记录信息,您可以实现javax.faces.event.ActionListener,例如:LogHandler.java
package bruce.zhao; import javax.faces.event.ActionListener; .... public class LogHandler implements ActionListener { public void processAction(ActionEvent e) { // 处理Log } }
VerifyHandler.java
package bruce.zhao; import javax.faces.event.ActionListener; .... public class VerifyHandler implements ActionListener { public void processAction(ActionEvent e) { // 处理验证 } }
这么一来,您就可以使用<f:actionListener>标签向组件注册事件,例如:
<h:commandButton value="送出" action="#{user.outcome}"> <f:actionListener type="bruce.zhao.LogHandler"/> <f:actionListener type="bruce.zhao.VerifyHandler"/> </h:commandButton>
<f:actionListener>会自动产生type所指定的对象,并呼叫组件的addActionListener()方法注册Listener。
相关文章推荐
- MC新手入门(十五)------ 路径事件
- MC新手入门(十五)------ 路径事件
- JSF Gossip: 动作事件
- Legolas工业自动化平台入门(三)交互事件响应动作
- JSF---->事件处理---动作事件
- Spread for Windows Forms快速入门(8)---单元格中用户动作触发的事件
- JSF---->事件处理---动作事件
- JSF入门十六(即时事件)
- OpenGL ES2.0入门之Android篇(二)——添加动作及触摸事件
- JSF入门十七(值变事件)
- Spread for Windows Forms快速入门(8)---单元格中用户动作触发的事件
- H5 canvas基础入门到捕鱼达人小游戏实现(4)canvas事件处理和图形动作
- Node.js入门:事件机制
- 跟我学ASP.NET委托和事件的入门应用和提高事例
- Vue入门 - 模版语法与事件
- 【Android2D游戏开发十五】关于Android 游戏开发中 OnTouchEvent() 触屏事件的性能优化笔记!
- logz.io一个企业级的ELK日志分析器 内部集成了机器学习识别威胁——核心:利用用户对于特定日志事件的反馈处理动作来学习判断日志威胁 + 类似语音识别的专家系统从各方收集日志威胁信息
- Android基础入门教程——3.2 基于回调的事件处理机制
- Qt入门(8)——事件和事件过滤器
- SDL入门学习二.事件查询机制