【问题标题】:JSF 2 nested composite component, passing f:validateLengthJSF 2 嵌套复合组件,传递 f:validateLength
【发布时间】:2012-06-22 15:29:43
【问题描述】:

我正在尝试创建一系列嵌套组件,其中标准 jsf/primefaces 组件被包装在标准标记中,并且仍然可以接收标准方面属性等

我在以下代码中遇到问题,将 f:validateLength 应用于指定字段。

<util:reducedwrap id="baz" label="Baz">
    <f:validateLength minimum="3" for="value" />
</util:reducedwrap>

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>

reducedwrapper.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 按我的意愿工作

还是应该去自定义组件路由?

简化代码如下:

reducedcomposite.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)

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

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

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

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

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>

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

【问题讨论】:

  • 您是否尝试将验证器的“for”属性更改为组件输入的 id(“fld”)?我这里也有类似的情况。

标签: jsf jsf-2 nested facelets composite-component


【解决方案1】:

迟到总比不到好,你需要这个:

<composite:interface>
    ...
   <composite:editableValueHolder name="value" targets="inputText" />
    ...
</composite:interface>

<composite:implementation>
    ...
   <h:inputText id="inputText" value="#{cc.attrs.value}" />
    ...
</composite:implementation>

假设组件名为“inputText”,而您将以传统方式使用它:

<util:inputText value="#{beanName.valueForInput}">
    <f:validateLength minimum="10" />
</util:inputText>

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 2014-03-07
    • 1970-01-01
    • 2012-11-18
    • 2012-07-11
    • 2011-07-24
    • 2017-11-17
    • 2018-02-23
    • 2013-11-10
    相关资源
    最近更新 更多