2

primefaces 3.5 ダイアログを含むタグファイルを作成しています。ダイアログにはコマンドボタンが含まれており、このボタンはアクションリスナーでパラメーター化されています。問題は、actionlistener メソッドを属性としてタグファイルに渡すことができるかどうかです。

質問を明確にするために、次の依存関係を持つテストケース(mavenを使用)を用意しました

<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>

私はtaglibを定義します:

<?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 は次のようになります。

<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>

最後に、サンプルの xhtml ページで DemoDialog タグが使用されています。

<!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 (mvn jetty:run-war) 経由で jetty を起動します。

4

1 に答える 1

3

.taglib.xmlを明示的に宣言して、タグ属性をメソッド式属性として宣言する必要があります<method-signature>。実際、デフォルトは値式です。

<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すると、それらの IDE オートコンプリートが有効になります。

于 2013-11-28T09:58:38.953 に答える