gpt4 book ai didi

jsf - 在JSF中何时以及如何生成clientID?

转载 作者:行者123 更新时间:2023-12-04 17:34:23 26 4
gpt4 key购买 nike

为了更了解JSF(2.x)中的clientID生成,有人可以向我解释一下JSF何时确切地生成客户端ID(哪个生命周期阶段,构建时间或呈现时间...)?

以及如何生成客户端ID(如果提供/不提供组件ID,或者不提供组件ID,则是随机生成或使用特定逻辑...)?

最佳答案

In order to more understand the clientID generation in JSF (2.x), could someone explain to me when exactely does JSF generates the client ID (which lifecycle phase, build time or render time ...)?



它必须以HTML响应结尾。因此,它是在渲染响应期间生成的。如果将调试断点放在 UIComponent#getClientId() 方法上,那么您会在堆栈的更下方看到(对于Mojarra) RenderResponsePhase#execute()已被调用。这足以说明命中断点时生命周期中的当前阶段。

And how the client ID will be generated (if providing /not providing component ID and not, randomly or using a specific logic...)?



可以在 UIComponent#getClientId() 中找到抽象 UIComponentBase#getClientId()方法的具体实现。 Its source code can be found at grepcode。如何生成它只是在 its javadoc中进行了描述:

public abstract String getClientId(FacesContext context)

Return a client-side identifier for this component, generating one if necessary. The associated Renderer, if any, will be asked to convert the clientId to a form suitable for transmission to the client.

The return from this method must be the same value throughout the lifetime of the instance, unless the id property of the component is changed, or the component is placed in a NamingContainer whose client ID changes (for example, UIData). However, even in these cases, consecutive calls to this method must always return the same value. The implementation must follow these steps in determining the clientId:

Find the closest ancestor to this component in the view hierarchy that implements NamingContainer. Call getContainerClientId() on it and save the result as the parentId local variable. Call getId() on this component and save the result as the myId local variable. If myId is null, call context.getViewRoot().createUniqueId() and assign the result to myId. If parentId is non-null, let myId equal parentId +UINamingContainer.getSeparatorChar(javax.faces.context.FacesContext)+ myId. Call Renderer.convertClientId(javax.faces.context.FacesContext, java.lang.String), passing myId, and return the result.



很清楚,是吗?最重要的部分可能是记住实现 NamingContainer并因此附加其客户端ID的组件。在标准JSF 2.x中,至少为 <h:form><h:dataTable><ui:repeat><f:subview><cc:implementation>If you gently give all components a fixed ID,那么您还将在生成的HTML输出中看到该模式。

如果您不给这些组件一个固定的ID,那么将使用JSF生成的ID,该ID可通过 UIViewRoot#createUniqueId() 获得(如上述javadoc摘录中所暗示的)。 Its javadoc说:

public String createUniqueId()

Generate an identifier for a component. The identifier will be prefixed with UNIQUE_ID_PREFIX, and will be unique within the non-NamingContainer child sub-trees of this UIViewRoot.



该前缀是 j_id 。尚不清楚实现应该如何生成,因此所有实现者都可以自由地实现它。他们通常使用树中组件数量的递增索引。因此,第一个组件 UIViewRoot可以获取 j_id1的ID。它的第一个 child 可以得到 j_id2的ID。 Etcetera。您可以通过在 UIViewRoot#createUniqueId()方法甚至 UIComponentBase#setId() 方法上放置调试断点来追溯逻辑。

也可以看看:
  • How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
  • By default, JSF generates unusable ids, which are incompatible with css part of web standards
  • Avoiding duplicate ids when reusing facelets compositions in the same naming container
  • 关于jsf - 在JSF中何时以及如何生成clientID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28351976/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com