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

发布于 2024-10-21 04:22:36 字数 1630 浏览 5 评论 0原文

我在 JSF 树中查找组件时遇到问题。假设我有以下模板:

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

命名空间定义为:

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

如您所见,有一个 a4j:repeat 标记,因此页面上可以有 n 个呈现的选择输入。如何在服务器端的 JSF 树中找到第 n 个组件?在客户端,组件的呈现方式如下:someForm:somePanel:0:personPanel:personAge。我试图以这种方式找到组件:

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

但找不到。我检查了树,似乎具有该 id 的组件不存在。

那么如何获得这个组件呢?有什么办法可以实现这一点吗?


编辑:

我找到了一些解决方法。实际上,我不需要组件,而是它们的值。可以通过名称从请求中检索值。以下代码:

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

完成工作。

I have a problem with finding component in JSF tree. 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. How can I find n-th component in JSF tree at the server side? At the client side, components are rendered like: 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琴流音 2024-10-28 04:22:36

a4j:repeat 不是一个为每次迭代创建专用组件的标记处理程序。相反,它会导致其子组件在 JSF 生命周期的每个阶段被重复访问。也就是说,每一行没有专用的组件。

有关标记处理程序和组件之间差异的更多信息,请参阅:
https://rogerkeays.com/jsf-c-foreach-vs-ui-重复

通常可以避免在Java端按名称查找组件。如果您告诉我们为什么要尝试这样做,我们可能会建议替代方案。

编辑:JSF 中的验证通常由 Validator 完成,或者(对于复杂的情况)在操作方法中直接处理支持 bean 中的数据,手动将 FacesMessage 放入 FacesContext 中。我不明白为什么您需要该组件进行验证?

a4j:repeat isn't a tag handler that would create dedicated components for each iteration. Rather, it causes its child components to be visited repeatedly during each phase of the JSF lifecycle. 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

It can usually be avoided to lookup components by name on the Java side. 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. I don't see why you'd need the component for validation?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文