为什么复合组件中的构面内的commandLink会出现错误?

时间:2015-08-13 18:40:20

标签: jsf composite-component facet mojarra commandlink

当我创建一个带有构面的复合组件并在该构面中放置命令链接时,我收到一条错误消息:dangerous

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

index.xhtml:

This link is disabled as it is not nested within a JSF form.

/resources/mycomp/component.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>

这是我的浏览器所做的:

enter image description here

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

2 个答案:

答案 0 :(得分:1)

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

<强> 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在两种情况下都不同:

  • 第一个帖子帖子 - &gt;没有表单ID的clientId
  • 我的帖子 - &gt; clientId包含表单ID

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

使用Mojarra 2.2.13(Primefaces 6.x)。

答案 1 :(得分:0)

我正在做相同的事情,除了我要通过构面插入PrimeFaces的p:commandButton-该按钮有效,唯一的问题是警告,因此我为这些问题提供了解决方案。

添加一个componentType:

<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测试。