【问题标题】:In JSF is there a way to add a passThroughAttribute to a component based on a condition or parameter?在 JSF 中有没有办法根据条件或参数向组件添加 passThroughAttribute?
【发布时间】:2015-03-11 11:47:47
【问题描述】:

我有一个“inputSecret”组件,我需要同时启用 HTML 原生非 null 验证以及 JSF 验证。或者根据参数将它们都关闭。

首先,我导入了 xmlns:h="http://xmlns.jcp.org/jsf/htmlxmlns:f="http://xmlns.jcp.org/jsf/core 命名空间

这段代码

<h:inputSecret required="#{passwordRequired}" >
</h:inputSecret>

如果passwordRequired 参数为'true',将启用JSF 验证。而且我认为“必需”属性也会转换为最终的 HTML 以启用本机 HTML 验证,但事实并非如此。因此,我使用 passThroughAttribute 将“必需”属性放入 HTML 呈现的组件中。

<h:inputSecret required="#{passwordRequired}" >
    <f:passThroughAttribute name="required" value="#{passwordRequired}" />
</h:inputSecret>

一个问题是 passThroughAttribute 的值并不重要,我可以将其设置为“required”、“true”、“false”甚至空字符串,如果我输入如上所述的 passThroughAttribute 标记(同样,无论值如何)。

这对于我最初的用例来说是可以的,但是也有这种用例不需要用户更改密码的情况,所以我希望能够有条件地添加“必需” attrib 作为 JSF 代码中的“passThroughAttribute”标签。

有点意思

<h:inputSecret required="#{passwordRequired}" >
    <if:condition value="#{passwordRequired}">
        <f:passThroughAttribute name="required" value="true" />
    </if>
</h:inputSecret>

对于 UI 组件,我会使用“render”属性,但事实并非如此。我只是希望能够根据我的参数打开或关闭“passthroughAttribute”的包含。我知道这听起来像 JSTL 和 "c:if" 但这并不总是有效,所以我需要其他东西。

有没有人知道一种方法可以让我在没有一些可怕的黑客攻击的情况下完成我需要的事情?

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    使用 JSTL &lt;c:if&gt;:

    <!-- xmlns:c="http://java.sun.com/jsp/jstl/core" -->
    
    <h:inputSecret required="#{passwordRequired}" >
        <c:if test="#{passwordRequired}">
            <f:passThroughAttribute name="required" value="true" />
        </c:if>
    </h:inputSecret>
    

    作为替代方案,您可以使用 rendered 条件复制代码:

    <h:inputSecret required="#{passwordRequired}" rendered="#{passwordRequired}">
        <f:passThroughAttribute name="required" value="#{passwordRequired}" />
    </h:inputSecret>
    <h:inputSecret required="#{passwordRequired}" rendered="#{!passwordRequired}"/>
    

    【讨论】:

    • 关于您对JSTL的不了解(基于原始答案),请仔细阅读stackoverflow.com/questions/3342984/…
    • @BalusC 在我的原始答案中,我写道我目前无法尝试,所以我不能确定它是否有效。感谢您提供 JSTL 信息,这很有帮助。
    • @BalusC 感谢您的输入,我正试图避免 JSTL,因为我浏览了一些说 JSF 和 JSTL 不能很好混合的帖子,但您的帖子更有意义。 解决方案似乎在这种情况下有效,所以我会认为问题已解决。
    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 2021-03-14
    • 1970-01-01
    • 2022-07-22
    • 2021-05-31
    • 1970-01-01
    • 2021-10-09
    • 2011-11-22
    相关资源
    最近更新 更多