简体   繁体   English

在 JSF 树中查找嵌套在 a4j:repeat 标签中的第 n 个组件

[英]Finding n-th component nested in a4j:repeat tag in JSF tree

I have a problem with finding component in JSF tree.我在 JSF 树中查找组件时遇到问题。 Suppose I have the following template:假设我有以下模板:

<a4j:form id="someForm">
<a4j:outputPanel id="somePanel">
    <a4j:repeat id="people" value="#{bean.people}" rowKeyVar="_row" var="_data" stateVar="_state">
        <s:decorate id="personPanel" template="edit.xhtml">

            <h:outputLabel for="personAge" value="Choose your age:" />

            <h:selectOneMenu id="personAge" value="#{_data.age}">
                <s:selectItems var="_item" value="#{ageValues}" label="#{_item.description}" />
            </h:selectOneMenu>

        </s:decorate>
    </a4j:repeat>
</a4j:outputPanel>
</a4j:form>

Namespaces are defined as:命名空间定义为:

xmlns:a4j="http://richfaces.org/a4j"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:s="http://jboss.com/products/seam/taglib"

As you can see, there is a a4j:repeat tag, so there can be n rendered select inputs on the page.如您所见,有一个a4j:repeat标签,因此页面上可以有n 个渲染的选择输入。 How can I find n -th component in JSF tree at the server side?如何在服务器端的 JSF 树中找到第n个组件? At the client side, components are rendered like: someForm:somePanel:0:personPanel:personAge .在客户端,组件呈现如下: someForm:somePanel:0:personPanel:personAge I'm trying to find component this way:我正在尝试以这种方式查找组件:

UIViewRoot root = FacesContext.getCurrentInstance().getViewRoot();
UIInput ageInput = (UIInput) root.findComponent("someForm:somePanel:0:personPanel:personAge");

But it couldn't be find.但是找不到了。 I've checked tree, and it seems like component with that id doesn't exist.我检查了树,似乎不存在具有该 ID 的组件。

So how can I obtain this component?那么如何获得这个组件呢? Is there any way to achieve that?有什么方法可以实现吗?


EDIT:编辑:

I've found some workaround.我找到了一些解决方法。 Actually, I didn't need components, but their values.实际上,我不需要组件,而是需要它们的值。 Values can be retrieved from request by their names.可以通过名称从请求中检索值。 Following code:以下代码:

FacesContext facesContext = FacesContext.getCurrentInstance();
String ageValue = facesContext.getExternalContext().getRequestParameterMap().get("someForm:somePanel:0:personPanel:personAge");

did the work.做了工作。

a4j:repeat isn't a tag handler that would create dedicated components for each iteration. a4j:repeat 不是为每次迭代创建专用组件的标签处理程序。 Rather, it causes its child components to be visited repeatedly during each phase of the JSF lifecycle.相反,它会导致在 JSF 生命周期的每个阶段重复访问其子组件。 That is, there isn't a dedicated component for each row.也就是说,每一行都没有专用的组件。

For more information on the difference between tag handlers and components, see: https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat有关标签处理程序和组件之间差异的更多信息,请参阅: https : //rogerkeays.com/jsf-c-foreach-vs-ui-repeat

It can usually be avoided to lookup components by name on the Java side.通常可以避免在 Java 端按名称查找组件。 If you told us why you're trying to do this, we might suggest alternatives.如果您告诉我们您为什么要这样做,我们可能会建议替代方案。

Edit : Validation in JSF is usually done by a Validator or (for complex cases) in the action method by working directly on the data in the backing bean, putting FacesMessage into the FacesContext manually.编辑:JSF 中的验证通常由 Validator 或(对于复杂情况)在 action 方法中完成,方法是直接处理支持 bean 中的数据,手动将 FacesMessage 放入 FacesContext。 I don't see why you'd need the component for validation?我不明白您为什么需要组件进行验证?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM