我正在尝试创建一系列嵌套组件,其中标准 jsf/primefaces 组件包装在标准标记中,并且仍然可以接收标准方面属性等
我在以下代码中遇到问题,将 f:validateLength 应用于指定字段。
<util:reducedwrap id="baz" label="Baz">
<f:validateLength minimum="3" for="value" />
</util:reducedwrap>
减少包装.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:util="http://java.sun.com/jsf/composite/components/util">
<cc:interface>
<cc:attribute name="id"/>
<cc:attribute name="value"/>
<cc:editableValueHolder name="value" targets="fld" />
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<util:reducedwrapper label="#{cc.attrs.label}">
<h:inputText id="fld" value="#{cc.attrs.value}"/>
</util:reducedwrapper>
</cc:implementation>
</html>
减少包装器.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<h:outputLabel for="fld" value="#{cc.attrs.label}" />
<cc:insertChildren />
<h:message for="fld" />
</cc:implementation>
</html>
但是,虽然 f:validateLength 正在运行,因此 h:message 看不到“fld”,但 h:outputLabel 会看到并最终以正确生成的 for 属性输出。(这让我很困惑——这是对的吗?两者都是?)
我用于 f:validateLength(或实际上是 h:message)的“for”属性的值是否错误?如果是这样,它应该是什么?
我进一步减少了代码,以便所有嵌套的东西要么发生在被调用的组件中(util:reduced),要么直接从它调用(util:reducedsplit)。此代码(以及调用它们的示例页面)位于此消息的末尾。
两者都按我的预期工作,但似乎都不是理想的解决方案。
util:reduced 显然需要在使用此模式的任何其他组件中复制代码。
util:reducedsplit 有效,尽管我可以在其他组件中插入 2 个组件,但由于有效性考虑,我无法将任何包装标记(我确实拥有)移动到组件。所以无论如何都需要复制。
这让我回想起如何让 util:reducedwrap 按我的意愿工作
还是我应该去自定义组件路线?
精简代码如下:
减少composite.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:f="http://java.sun.com/jsf/core"
xmlns:util="http://java.sun.com/jsf/composite/components/util">
<h:body>
<h:form id="default">
util:reduced<br />
<util:reduced id="foo" label="Foo">
<f:validateLength minimum="5" for="value" />
</util:reduced>
<hr />
util:reducedsplit<br />
<util:reducedsplit id="bar" label="Bar">
<f:validateLength minimum="4" for="value" />
</util:reducedsplit>
<hr />
util:reducedwrap<br />
<util:reducedwrap id="baz" label="Baz">
<f:validateLength minimum="3" for="value" />
</util:reducedwrap>
<hr />
<h:commandButton value="Do it!" />
</h:form>
</h:body>
</html>
实用组件 (/resources/components/util)
减少的.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
<cc:attribute name="id"/>
<cc:attribute name="value"/>
<cc:editableValueHolder name="value" targets="fld" />
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<h:outputLabel for="fld" value="#{cc.attrs.label}" />
<h:inputText id="fld" value="#{cc.attrs.value}"/>
<h:message for="fld" />
</cc:implementation>
</html>
缩减拆分.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:util="http://java.sun.com/jsf/composite/components/util">
<cc:interface>
<cc:attribute name="id"/>
<cc:attribute name="value"/>
<cc:editableValueHolder name="value" targets="fld" />
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<util:reducedlabel label="#{cc.attrs.label}" />
<h:inputText id="fld" value="#{cc.attrs.value}"/>
<util:reducedmessage />
</cc:implementation>
</html>
减少标签.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite" >
<cc:interface>
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<h:outputLabel for="fld" value="#{cc.attrs.label}" />
</cc:implementation>
</html>
减少消息.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite" >
<cc:interface>
</cc:interface>
<cc:implementation>
<h:message for="fld" />
</cc:implementation>
</html>
减少包装.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:util="http://java.sun.com/jsf/composite/components/util">
<cc:interface>
<cc:attribute name="id"/>
<cc:attribute name="value"/>
<cc:editableValueHolder name="value" targets="fld" />
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<util:reducedwrapper label="#{cc.attrs.label}">
<h:inputText id="fld" value="#{cc.attrs.value}"/>
</util:reducedwrapper>
</cc:implementation>
</html>
减少包装器.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
<cc:attribute name="label"/>
</cc:interface>
<cc:implementation>
<h:outputLabel for="fld" value="#{cc.attrs.label}" />
<cc:insertChildren />
<h:message for="fld" />
</cc:implementation>
</html>