JSF h:setPropertyActionListener标签

<h:setPropertyActionListener>标签向一个将bean属性设置为给定值的组件添加了一个actionlistener
以下代码显示如何使用<f:setPropertyActionListener>标签。

<h:commandButton id="submit" action="result" value="Show Message"> 
   <f:setPropertyActionListener target="#{userData.data}" 
      value="JSF 2.0 User" />
</h:commandButton>

实例

以下是文件:UserBean.java 中的代码。

package com.zyiz.netmon;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

  public String username;

  public String outcome(){
    return "result";
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

}

以下是文件: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"
      >
    <h:body>
        <h:form id="form">
            <h:commandButton action="#{user.outcome}" value="Click Me">
                <f:setPropertyActionListener target="#{user.username}" value="zyiz" />
            </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>

        <h1>JSF 2 setPropertyActionListener example</h1>

        #{user.username}

    </h:body>

</html>

运行测试

打开 NetBeans 创建一个名称为: setPropertyActionListener 的Web工程,并使用上面文件代码。运行项目,打开浏览器访问以下网址:

http://localhost:8084/setPropertyActionListener

如果没有错误,应该会看到以下结果 -

点击“Click Me”按钮,结果如下 -


上一篇:JSF h:attribute标签

下一篇:JSF转换器标签

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程