目录

f:setPropertyActionListener

h:setPropertyActionListener标记将一个动作侦听器添加到将bean属性设置为给定值的组件。

JSF标签 (JSF Tag)

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

例子 Example Application

让我们创建一个测试JSF应用程序来测试上面的标记。

描述
1com.iowiki.test包下创建一个名为helloworld的项目,如JSF - First Application一章中所述。
2 修改home.xhtml ,如下所述。 保持其余文件不变。
3 在webapps目录中创建result.xhtml ,如下所述。
4com.iowiki.test包下创建UserData.java作为托管bean,如下所述。
5 编译并运行应用程序以确保业务逻辑按照要求运行。
6 最后,以war文件的形式构建应用程序并将其部署在Apache Tomcat Webserver中。
7 使用适当的URL启动Web应用程序,如下面的最后一步所述。

UserData.java

package com.iowiki.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
   private static final long serialVersionUID = 1L;
   public String data = "1";
   public String getData() {
      return data;
   }
   public void setData(String data) {
      this.data = data;
   }
}

home.xhtml

<!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">
   <head>
      <title>JSF Tutorial!</title>
   </head>
   <body>
      <h2>f:attribute example</h2>
      <hr />
      <h:form>
         <h:commandButton id = "submit" action = "result" value = "Show Message"> 
            <f:setPropertyActionListener 
            target = "#{userData.data}" value = "JSF 2.0 User" />
         </h:commandButton>
      </h:form>
   </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:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/jsf/html"
   xmlns:ui = "http://java.sun.com/jsf/facelets">
   <head>
      <title>JSF Tutorial!</title>
   </head>
   <h:body>
      <h2>Result</h2>
      <hr />
      #{userData.data}
   </h:body>
</html>  

一旦准备好完成所有更改,让我们像在JSF - First Application章节中那样编译和运行应用程序。 如果您的应用程序一切正常,这将产生以下结果。

JSF h:setPropertyActionListener

按“ Show Message按钮,您将看到以下结果。

JSF h:setPropertyActionListener1
↑回到顶部↑
WIKI教程 @2018