我正在使用 GlassFish 3.1.2 并尝试使用 composite:insertFacet
在复合组件中,但使用我的 CC 时不会生成 HTML 标记。
当我使用 composite:renderFacet
时,相同的代码工作正常,但我想知道为什么 composite:insertFacet
在那个例子中不起作用?什么时候使用它们?
这是我的 login.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:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface>
<composite:editableValueHolder name="nameInput" targets="form:name"/>
<composite:editableValueHolder name="passwordInput" targets="form:password"/>
<composite:editableValueHolder name="inputs"
targets="form:name form:password"/>
<composite:actionSource name="loginButton" targets="form:loginButton"/>
<composite:attribute name="name"/>
<composite:attribute name="password"/>
<composite:attribute name="namePrompt"/>
<composite:attribute name="passwordPrompt"/>
<composite:attribute name="loginValidate"
method-signature="void validateLogin(ComponentSystemEvent e)
throws javax.faces.event.AbortProcessingException"/>
<composite:attribute name="loginAction"
method-signature="java.lang.String action()"/>
<composite:facet name="heading"/>
<composite:facet name="error"/>
</composite:interface>
<composite:implementation>
<h:outputScript library="util" name="login.js" target="head"/>
<h:form id="form" onsubmit="return checkForm(this, '#{cc.clientId}')">
<composite:insertFacet name="heading"/>
<h:panelGrid columns="2">
#{cc.attrs.namePrompt}
<h:panelGroup>
<h:inputText id="name" value="#{cc.attrs.name}"/>
<h:message for="name"/>
</h:panelGroup>
#{cc.attrs.passwordPrompt}
<h:panelGroup>
<h:inputSecret id="password" value="#{cc.attrs.password}" size="8"/>
<h:message for="password"/>
</h:panelGroup>
</h:panelGrid>
<p>
<h:commandButton id="loginButton"
value="#{cc.attrs.loginButtonText}"
action="#{cc.attrs.loginAction}"/>
</p>
</h:form>
<composite:renderFacet name="error"/>
<p><composite:insertChildren/></p>
<p>#{cc.resourceBundleMap.footer}</p>
</composite:implementation>
</html>
index.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:util="http://java.sun.com/jsf/composite/util"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>#{msgs.loginHeading}</title>
<h:outputStylesheet library="css" name="styles.css" />
</h:head>
<h:body>
<util:login namePrompt="#{msgs.namePrompt}"
passwordPrompt="#{msgs.passwordPrompt}"
name="#{user.name}"
password="#{user.password}"
loginAction="#{user.login}"
loginButtonText="#{msgs.loginButtonText}">
<f:validateLength minimum="4" for="nameInput"/>
<f:validator validatorId="com.corejsf.Password" for="passwordInput"/>
<f:actionListener type="com.corejsf.LoginActionListener" for="loginButton"/>
<f:facet name="heading" styleClass="header">
<div class="prompt">#{msgs.loginPrompt}</div>
</f:facet>
<f:facet name="error" styleClass="error">
<h:messages layout="table" styleClass="error"/>
</f:facet>
<!-- Child component -->
<h:link outcome="register">#{msgs.registerLinkText}</h:link>
</util:login>
<ui:debug/>
</h:body>
</html>
我试图从组件树中找出原因,但我更困惑的是
insertFacet
出现,这是来自 的部分组件树 使用 insertFacet 时: <UIOutput id="j_idt7" inView="true" rendered="true" transient="false">
<UINamingContainer id="j_idt8" inView="true" rendered="true" transient="false">
error
<HtmlMessages globalOnly="false" id="j_idt10" inView="true" layout="table" redisplay="true" rendered="true" showDetail="false" showSummary="true" styleClass="error" tooltip="false" transient="false"/>
javax.faces.component.COMPOSITE_FACET_NAME
<UIPanel inView="true" rendered="true" transient="false">
<HtmlForm enctype="application/x-www-form-urlencoded" id="form" inView="true" onsubmit="return checkForm(this, '')" prependId="true" rendered="true" submitted="false" transient="false">
header
<div class="prompt">#{msgs.loginPrompt}</div>
<HtmlPanelGrid border="-2147483648" columns="2" id="j_idt15" inView="true" rendered="true" transient="false">
....
组件树 使用 renderFacet 时:
<UIOutput id="j_idt7" inView="true" rendered="true" transient="false">
<UINamingContainer id="j_idt8" inView="true" rendered="true" transient="false">
error
<HtmlMessages globalOnly="false" id="j_idt10" inView="true" layout="table" redisplay="true" rendered="true" showDetail="false" showSummary="true" styleClass="error" tooltip="false" transient="false"/>
heading
<div class="prompt">#{msgs.loginPrompt}</div>
javax.faces.component.COMPOSITE_FACET_NAME
<UIPanel inView="true" rendered="true" transient="false">
<HtmlForm enctype="application/x-www-form-urlencoded" id="form" inView="true" onsubmit="return checkForm(this, '')" prependId="true" rendered="true" submitted="false" transient="false">
<UIOutput id="j_idt15" inView="true" rendered="true" transient="false"/>
<HtmlPanelGrid border="-2147483648" columns="2" id="j_idt16" inView="true" rendered="true" transient="false">
....
最佳答案
<cc:insertFacet>
不是针对复合 Material 本身,而是针对复合 Material 的 child ,而后者又期望需要从外部提供的方面。
例如。 <h:dataTable>
支持 <f:facet name="header">
(糟糕的复合示例,但这只是为了提供想法):
<cc:implementation>
<h:dataTable>
<cc:insertFacet name="header">
哪个用作
<my:composite>
<f:facet name="header">
这与没有复合的情况下的效果完全相同
<h:dataTable>
<f:facet name="header">
<cc:insertFacet>
确实很少使用。<cc:renderFacet>
用于复合实现本身,基本上实现与 <ui:define>
相同的效果。/<ui:insert>
在模板和标记文件中。
关于jsf - 复合:insertFacet and composite:renderFacet?有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28137178/