存储(和检索)对动态/以编程方式创建的表单的引用 objects - UIComponents 是否不是通过引用传递的?

Storing (and retrieving) references to dynamically / programmatically created form objects - Are UIComponents NOT passed by reference?

我正在为内容管理系统编写程序,该系统根据给定 XML 的内容创建表单。 XML 包含有关要创建的内容结构(来自下拉列表)和与每个叶子关联的输入类型(段落的文本区域、章节标题的文本字段等)的信息。我将其称为 'fragment'。片段可以是叶子(文本框、文本区域等 - 包含句子/段落)或节点(其中包含其他片段)。

我想确认 material 中任何新创建的片段以前都不存在。为此,我需要知道它是什么片段类型(是章节标题、章节摘要等),以便我可以根据内容查询数据库以查明当前片段是否重复。

我试图在 ArrayList 中保存对动态创建的组件 object 的引用。对于创建的每个输入组件,此 arraylist 具有片段类型。在我的 bean 中,我调用:

List<UIComponent> createMaterialView =  SharedData.findComponent("template_form").getChildren();

并递归遍历 createMaterialView 中的 children,在表单中处理的 UIComponents none 与我之前添加到 ArrayList 中的 UIComponents 相匹配。

private FragmentTypeLeaf getFragmentTypeIfLeaf(UIComponent component) {
    System.out.println("Checking if "+component+" of class:"+component.getClass()+" is in LeafFrags");
    for(FragmentTypeLeaf currentLeaf : leafFragments) {
      if(component.equals(currentLeaf.getInputComponent())) {
          //Found leaf fragment input panel.
          System.out.println("Found a leaf fragment:"+currentLeaf.getName()+",Type:"+currentLeaf.getFragmentType());
          return currentLeaf;
          //Validate this content with all fragments existing (of this type) to ensure not exist.
      }
    }
    return null;
}

我在创建它们并将它们添加到表单 + leafFragments 时为 UIComponent.toString() 做了控制台输出,并将它们与下面粘贴的 getFragmentTypeIfLeaf() 中的 component.toString 进行了比较。我看到虽然预期的 类 匹配,但 UIComponents 不匹配。

UIComponents 不是通过引用传递的吗?如果我无法创建 UIComponent,将其添加到表单 + 通过将其添加到列表来跟踪其引用,那么我如何设计一个系统来检查动态创建的 UIComponent(需要与片段类型相关联)是否具有数据库中已存在该片段类型的内容?

谢谢!对不起,如果那太冗长了。我希望问题很清楚。

UIComponent 实例在请求范围内并在每次请求时重新创建。只有它们的状态是视图范围的。您不应该在更广泛的范围内将 UIComponent 实例作为 bean 的 属性 引用。此外,UIComponent class 没有实现 equals() 方法。

目前还不清楚您究竟想通过这种方式实现什么,但有一点可以肯定:您是从错误的角度接近它的。您不应该在控制器端动态操作组件树。您应该改为在视图侧使用例如JSTL and/or 迭代器组件,它们根据模型(bean 属性)完成任务。额外的优势是,XHTML+XML 是一种更适合创建和定义树结构任务的语言。

长话短说:How does the 'binding' attribute work in JSF? When and how should it be used?