문제

I am creating a tagfile that contains a primefaces 3.5 dialog. the dialog contains a commandbutton and this button is parameterized with an actionlistener. The question is wether or not i can pass the actionlistener method as an attribute to the tagfile.

To clarify the quetsion I have prepared a testcase (using maven) with following dependencies

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

i define a 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>

and the DemoDialog.xhtml is like this:

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

finally the DemoDialog tag is in use in a sample xhtml page

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

To test the page I start jetty via maven (mvn jetty:run-war).

도움이 되었습니까?

해결책

You need to declare the tag attribute as a method expression attribute in the .taglib.xml by explicitly declaring a <method-signature>. It indeed defaults to a value expression.

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

(the method name actionListener is fully free to your choice; what counts are the return type and parameter types, those have to be fully qualified names)

Additional bonus, explicitly declaring tag attribtues in .taglib.xml enables IDE autocompletion on those.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top