【问题标题】:Why does a commandLink within a facet within a composite component renders an error?为什么复合组件的构面内的 commandLink 会呈现错误?
【发布时间】:2015-11-06 21:08:35
【问题描述】:

当我创建一个包含构面的复合组件并在该构面中放置一个命令链接时,我收到一条错误消息:This link is disabled as it is not nested within a JSF form.

commandButton 的行为方式不同,所以我倾向于这是一个错误。

index.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:mycomp="http://xmlns.jcp.org/jsf/composite/mycomp"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
    </h:head>
    <h:body>
        <mycomp:component>
            <f:facet name="someFacet">
                <h:commandLink value="this link should work, but does not (within form, within facet)"/><br/>
                <h:commandButton value="this button works as expected (within form, within facet)"/><br/>
            </f:facet>
        </mycomp:component>
    </h:body>
</html>

/resources/mycomp/component.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<ui:component
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:cc="http://xmlns.jcp.org/jsf/composite"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:a="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    >
    <cc:interface>
        <cc:facet name="someFacet" required="true"/>
    </cc:interface>
    <cc:implementation>
        <h:commandLink value="this link should not work (not in a form)"/><br/>
        <h:form>
            <h:commandLink value="this link works as expected (within form, but not in facet)"/><br/>
            <cc:renderFacet name="someFacet"/>
        </h:form>
    </cc:implementation>
</ui:component>

这就是我的浏览器所做的:

关于我可能做错了什么的任何想法,或者这确实是 Mojarra 2.2.7 中的一个错误? (与 NetBeans 8.0.2 捆绑在一起)

【问题讨论】:

  • Mojarra 2.1.6 呈现相同的输出(橙色项目符号除外)。
  • 我想知道您是否只是在滥用构面(refref)。我自己从来没有完全清楚方面,但它们似乎是向父组件提供输入的一种方式。 form 不接受名为 someFacet 的构面,因此行为未定义。不过我不确定,所以我不会发布“答案”。
  • 这确实是一个错误。你最好直接报告。然而,在复合材料中拥有一种形式反过来又是一种设计气味。您的复合材料不会“太多”吗?另见stackoverflow.com/questions/6822000/…
  • @DavidS 不是h:form,而是这个组件正在接受一个名为someFacet的构面:大概是表单的更多内容。
  • 是的,也许我在这里太努力了;我试图创建一个组合,它采用两组commandLink 组件来构建菜单结构。需要渲染的 html 已经定义好了,所以没有绕开这两个集合。

标签: jsf composite-component facet mojarra commandlink


【解决方案1】:

我正在做同样的事情,除了我通过 facet 插入 PrimeFaces 的 p:commandButton - 按钮有效,唯一的问题是警告,所以我正在为这些提供解决方案。

添加一个组件类型:

<cc:interface componentType="myComponent">

还有一个对应的java类:

import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.component.UIOutput;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;

@FacesComponent
public class MyComponent extends UIOutput /* or UINamingContainer - depends on what you need */ {

    @Override
    public String getFamily() {
        return UINamingContainer.COMPONENT_FAMILY;
    }

    @Override
    public boolean visitTree(VisitContext context, VisitCallback callback) {
        // skip the form checks; we know the form is guaranteed by the component anyway
        String callbackName = callback.getClass().getName();
        if (callbackName.contains("ValidateFormNestingCallback") || callbackName.contains("FormOmittedChecker")) {
            return false;
        }

        return super.visitTree(context, callback);
    }

}

使用 Mojarra 2.3.3 测试。

【讨论】:

    【解决方案2】:

    一个旧线程,但我认为当前行为是一个错误,因为以下内容有效,因此它应该在复合组件中工作:

    component.xhtml:

    <?xml version='1.0' encoding='UTF-8' ?>
    <ui:component
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:cc="http://xmlns.jcp.org/jsf/composite"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:a="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    >
    <cc:interface>
        <cc:facet name="someFacet" required="true"/>
    </cc:interface>
    <cc:implementation>
        <h:commandLink value="this link should not work (not in a form)"/><br/>
            <h:commandLink value="this link works as expected (within form, but not in facet)"/><br/>
            <cc:renderFacet name="someFacet"/>
    </cc:implementation>
    </ui:component>
    

    使用它(index.xhtml):

    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:mycomp="http://xmlns.jcp.org/jsf/composite/mycomp"
          xmlns:f="http://xmlns.jcp.org/jsf/core">
        <h:head>
        </h:head>
        <h:body>
         <h:form>
            <mycomp:component>
                <f:facet name="someFacet">
                    <h:commandLink value="this link should work, but does not (within form, within facet)"/><br/>
                    <h:commandButton value="this button works as expected (within form, within facet)"/><br/>
                </f:facet>
            </mycomp:component>
          </h:form>
        </h:body>
    </html>
    

    在这两种情况下生成的 HTML 输出也会在组件树的正确位置(在表单下)生成按钮。但是为按钮生成的 clientId 在两种情况下都不同:

    • 第一个线程帖子 --> 没有表单 ID 的 clientId
    • 我的帖子 --> clientId 包含表单 ID

    在我看来这似乎是一个错误,但也许有人可以说服我它不是;D

    使用 Mojarra 2.2.13 (Primefaces 6.x)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2022-11-23
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      相关资源
      最近更新 更多