首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将动作侦听器方法作为tagfile属性传递

将动作侦听器方法作为tagfile属性传递
EN

Stack Overflow用户
提问于 2013-11-27 12:26:47
回答 1查看 1.8K关注 0票数 2

我正在创建一个包含PrimeFaces3.5对话框的标签文件。该对话框包含一个命令按钮,该按钮使用actionlistener参数化。问题是我是否可以将actionlistener方法作为属性传递给tagfile。

为了澄清这个问题,我准备了一个带有以下依赖项的测试用例(使用maven)

代码语言:javascript
复制
<dependency>
  <groupId>org.primefaces</groupId>
  <artifactId>primefaces</artifactId>
  <version>3.5</version>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-api</artifactId>
  <version>2.2.4</version>
</dependency>

<dependency>
  <groupId>com.sun.faces</groupId>
  <artifactId>jsf-impl</artifactId>
  <version>2.2.4</version>
</dependency>

我定义了一个标签库:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd">
<namespace>http://demo.de/jsf/facelets</namespace>
<tag>
    <tag-name>DemoDialog</tag-name>
    <source>DemoDialog.xhtml</source>
</tag>  
</facelet-taglib>

DemoDialog.xhtml是这样的:

代码语言:javascript
复制
<ui:composition 
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:p="http://primefaces.org/ui"
   xmlns:c="http://java.sun.com/jsp/jstl/core">

  <c:if test="${empty dlgId}">
    <c:set var="dlgId" value="${id}" />
  </c:if>

  <c:if test="${empty widgetVar}">
    <c:set var="widgetVar" value="testDlg" />
  </c:if>

  ABC: #{okButtonActionListener}

  <p:dialog id="#{dlgId}" header="#{header}" widgetVar="#{widgetVar}">
    <h:form id="testForm" style="width: 400px;">
      <p:panelGrid>
        <p:row>
          <p:column>        
            <p:commandButton oncomplete="#{widgetVar}.hide()" value="Cancel" />
          </p:column>
          <p:column>
            <p:commandButton
               actionListener="${okButtonActionListener}"
               oncomplete="#{widgetVar}.hide()"
               value="Ok" />
          </p:column>
        </p:row>
      </p:panelGrid>
    </h:form>
  </p:dialog>
</ui:composition>

最后,DemoDialog标记在示例xhtml页面中使用。

代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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"
      xmlns:p="http://primefaces.org/ui"
      xmlns:li="http://demo.de/jsf/facelets">
  <f:view contentType="text/html">
    <h:head>
      <f:facet name="first">
        <meta http-equiv="X-UA-Compatible" content="EmulateIE8" />
        <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
        <title>Dialog Template test</title>
      </f:facet>
    </h:head>
    <h:body>
      <h:form id="demoForm">
      <p:commandButton onclick="testDlg.show()"/>
      </h:form>
      <li:DemoDialog id="lidemo" onclick="alert('hello')" okButtonActionListener="\${dialogBean.action_listener()}">
      </li:DemoDialog>
    </h:body>
  </f:view>
</html>

为了测试页面,我通过maven (:run)启动jetty。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-28 09:58:38

您需要通过显式声明一个.taglib.xml,将标记属性声明为<method-signature>中的方法表达式属性。它实际上默认为一个值表达式。

代码语言:javascript
复制
<tag>
    <tag-name>DemoDialog</tag-name>
    <source>DemoDialog.xhtml</source>
    <attribute>
        <name>okButtonActionListener</name>
        <method-signature>void actionListener(javax.faces.event.ActionEvent)</method-signature>
    </attribute>
</tag> 

(方法名actionListener完全可以供您选择;重要的是返回类型和参数类型,它们必须是完全限定的名称)

另外,在.taglib.xml中显式声明标记attribtues可以在这些标记上自动完成。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20242506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档