在 JSF 中,有没有办法根据条件或参数将 passThroughAttribute 添加到组件?
In JSF is there a way to add a passThroughAttribute to a component based on a condition or parameter?
我有一个 "inputSecret" 组件,我需要同时启用 HTML 本机非空验证和 JSF 验证。或者根据参数关闭它们。
首先,我导入了 xmlns:h="http://xmlns.jcp.org/jsf/html
和 xmlns:f="http://xmlns.jcp.org/jsf/core
命名空间
这个代码
<h:inputSecret required="#{passwordRequired}" >
</h:inputSecret>
如果 passwordRequired 参数为 'true', 将启用 JSF 验证。我认为 "required" 属性也会转化为最终的 HTML 以启用本机 HTML 验证,但事实并非如此。然后我使用 passThroughAttribute 将 "required" 属性向下放入 HTML 呈现的组件中。
<h:inputSecret required="#{passwordRequired}" >
<f:passThroughAttribute name="required" value="#{passwordRequired}" />
</h:inputSecret>
一个问题是 passThroughAttribute 的值并不重要,我可以将它设置为 "required"、"true"、"false" 甚至是空字符串,HTML 如果我如上所述输入 passThroughAttribute 标记(同样,无论值如何),本机验证只会发生。
这对于我最初的用例来说是可以的,但也有一种情况是用例不需要用户更改密码,所以我希望能够有条件地添加"required" 属性作为 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",但这并不总是有效,所以我需要其他东西。
有谁知道我可以不用一些可怕的黑客来完成我需要的事情的方法吗?
使用 JSTL <c:if>
:
<!-- 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}"/>
我有一个 "inputSecret" 组件,我需要同时启用 HTML 本机非空验证和 JSF 验证。或者根据参数关闭它们。
首先,我导入了 xmlns:h="http://xmlns.jcp.org/jsf/html
和 xmlns:f="http://xmlns.jcp.org/jsf/core
命名空间
这个代码
<h:inputSecret required="#{passwordRequired}" >
</h:inputSecret>
如果 passwordRequired 参数为 'true',将启用 JSF 验证。我认为 "required" 属性也会转化为最终的 HTML 以启用本机 HTML 验证,但事实并非如此。然后我使用 passThroughAttribute 将 "required" 属性向下放入 HTML 呈现的组件中。
<h:inputSecret required="#{passwordRequired}" >
<f:passThroughAttribute name="required" value="#{passwordRequired}" />
</h:inputSecret>
一个问题是 passThroughAttribute 的值并不重要,我可以将它设置为 "required"、"true"、"false" 甚至是空字符串,HTML 如果我如上所述输入 passThroughAttribute 标记(同样,无论值如何),本机验证只会发生。
这对于我最初的用例来说是可以的,但也有一种情况是用例不需要用户更改密码,所以我希望能够有条件地添加"required" 属性作为 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",但这并不总是有效,所以我需要其他东西。
有谁知道我可以不用一些可怕的黑客来完成我需要的事情的方法吗?
使用 JSTL <c:if>
:
<!-- 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}"/>