我尝试编写一个 jsf 标签库。所以我创建了一个提供facelet的库:AbButton.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:b="http://bootsfaces.net/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<c:choose>
[... other button variants...]
<c:when test="#{type == 'command' and not empty look}">
<b:commandButton value="#{value}" action="#{bean[action]}" id="#{id}" actionListener="#{bean[actionListener]}"
update="#{update}" look="#{look}" size="#{size}" iconAwesome="#{icon}" style="#{style}"/>
</c:when>
</c:choose>
</ui:composition>
我创建了一个描述属性的 taglib.xml。
<tag>
<tag-name>abButton</tag-name>
<description><![CDATA[Flexible Button Implementierung. Von Bootsfaces Button abgeleitet.]]></description>
<source>resources/tags/AbButton.xhtml</source>
[...]
<attribute>
<description><![CDATA[ stuff]]></description>
<name>action</name>
<required>false</required>
<type>javax.el.MethodExpression</type>
</attribute>
<attribute>
<description><![CDATA[stuff]]></description>
<name>actionListener</name>
<required>false</required>
<type>javax.el.MethodExpression</type>
</attribute>
[...]
</tag>
最后但并非最不重要的一点是,我在测试应用程序中实现了它。按钮.xhtml
<h:body>
<h:form>
<a:abButton type="command" id="command" bean="#{button}" action="action" actionListener="actionListener" value="Command Button" style="margin-right:20px;" look="primary"/>
</h:form>
</h:body>
还有一个名为 Button.java 的 Backing Bean
@Named("button")
@RequestScoped
public class Button {
public void action(){
System.out.println("action");
}
public void actionListener(){
System.out.println("action listener");
}
}
如果我现在单击按钮,则 action 和 actionListener 方法都会执行 2 次。如果我删除 actionListener,则 action 方法只执行一次。同样的事情是我使用不同的豆子,让我们说
AbButton.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:b="http://bootsfaces.net/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<c:choose>
[... other button variants...]
<c:when test="#{type == 'command' and not empty look}">
<b:commandButton value="#{value}" action="#{bean2[action]}" id="#{id}" actionListener="#{bean[actionListener]}"
update="#{update}" look="#{look}" size="#{size}" iconAwesome="#{icon}" style="#{style}"/>
</c:when>
</c:choose>
</ui:composition>
和 Button.xhtml
<h:body>
<h:form>
<a:abButton type="command" id="command" bean="#{button}" bean2="#{button}" action="action" actionListener="actionListener" value="Command Button" style="margin-right:20px;" look="primary"/>
</h:form>
</h:body>
如果我以这种方式使用 Omnifaces o:methodParam,绝对会发生同样的情况:
AbButton.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:b="http://bootsfaces.net/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<c:choose>
[... other button variants...]
<c:when test="#{type == 'command' and not empty look}">
<o:methodParam name="actionMethod" value="#{action}"/>
<o:methodParam name="actionListenerMethod" value="#{actionListener}"/>
<b:commandButton value="#{value}" action="#{actionMethod}" actionListener="#{actionListenerMethod}" id="#{id}" look="#{look}"
update="#{update}" size="#{size}" iconAwesome="#{icon}" style="#{style}"/>
</c:when>
</c:choose>
</ui:composition>
和 button.xhtml
<h:body>
<h:form>
<a:abButton type="command" id="command" action="#{button.action}" actionListener="#{button.actionListener}" value="Command Button" style="margin-right:20px;" look="primary"/>
</h:form>
</h:body>
也会发生同样的情况。当 action 和 actionListener 处于活动状态时,两者都会执行两次。如果我删除 actionListener,则 action 方法只执行一次。我希望现在可以更好地理解。有什么帮助吗?