簡體   English   中英

JSF 2.0 Composite組件 - ajax呈現參數OUTSIDE組件定義

[英]JSF 2.0 Composite components - ajax render parameter OUTSIDE component definition

考慮一個簡單的復合組件,它采用某種動作參數 - 例如,一個簡單的鏈接'美化'。 我想'ajaxify'它。

   <composite:interface>
        <composite:attribute name="act" method-signature="java.lang.String action()"></composite:attribute>
        <composite:attribute name="text" required="true"></composite:attribute>
            <composite:clientBehavior  name="click" event="action"  targets="l"/>    </composite:interface>

   <composite:implementation>
        <h:commandLink id="l" act="#{cc.attrs.action}" immediate="true">            <b>#{cc.attrs.text}</b>         </h:commandLink>    </composite:implementation>

我通過客戶端行為公開事件。 我這樣使用它:

<h:panelGroup layout="block" id="outside">

        #{mybean.otherdata} <br/>

                <mc:mylink text="Click click" action="#{mybean.click}" >
                    <f:ajax event="click" render="outside"/>"
                </mc:mylink><br/>

</h:panelGroup>

你可以看到我想做的事情:我想在復合定義之外做一個ajax渲染; 只是將渲染設置為“外部”會給可怕的<f:ajax> contains an unknown id錯誤。

是的,我知道命名容器,我知道我們可以用冒號前置並指定一個絕對路徑,但這非常笨重。 如果我將它包裝在更多層中(這是整點),我必須手動將這些引用鏈接在一起。

我可以制作一些像render="../outside"類的相對引用來跳過對組件父容器的引用嗎?

我用a4j做了一個jsf 1應用程序,這個模式在整個地方都被使用了。

在JSF 2.0中,您可以在EL中使用隱式cccomponent對象。 要獲取任何組件的完整客戶端ID,請執行以下操作:

#{component.clientId}

要檢索復合組件的客戶端ID,請執行以下操作:

#{cc.clientId}

同樣,您也可以使用#{cc.parent}獲取父#{cc.parent} 在這種情況下,這可能是你想要的。 有關更長的答案,請參閱使用JSF 2.0獲取命名容器內的組件clientId的完整前綴

您可以使用render="@all"render="@form"分別渲染所有內容或整個內容。

或者,您可以將要更新的絕對ID作為參數傳遞到組件中。 這樣可以保持靈活性,而不會不必要地重新渲染過多的頁面。

經過一些修補,這是一個解決方案:

把聽眾放在事件上:

<f:ajax event="click" listener="#{mycomp.listen}" render="#{mycomp.getParId('outside')}"/>"

執行:

public void listen(AjaxBehaviorEvent event) {
    String clid=event.getComponent().getClientId();

    StringTokenizer st=new StringTokenizer(clid,":");

    StringBuilder sb=new StringBuilder(":");
    for (int x=1;x<st.countTokens();x++)
    {
        sb.append(st.nextToken()).append(":");
    }

    parId=sb.toString();
}

public String getParId(String suff) {
    //must precheck as id is prevalidated for existence. if not set yet set to form
    if (parId==null)
    {
        return "@form";
    }
    return parId+suff;
}

即便如此,你為什么要這樣做呢?

我找到了一個更好的解決方案,我很高興利用模板中的隱式“組件”映射。 它允許使用../ filesystem表示法從復合組件返回基本相對映射。 它可以擴展到更多處理,但這是我所需要的。 它也適用於執行參數。

我想感謝我搞清楚! 我又誤解了什么? 我不需要弄清楚這一點。

用法:

<mc:mylink text="Click me" action="#{blah.myaction}" render="../../pg" />

執行:

<composite:implementation>

    <h:commandLink id="l" action="#{cc.attrs.action}" immediate="true">
        <f:ajax render="#{pathProcessor.pathProcess(3, cc.attrs.render, component.clientId)}" />
                     #{simon.rand} . <b>#{cc.attrs.text}</b>
    </h:commandLink>

.....

路徑處理器方法:

public static String pathProcess(int currentNesting, String path, String clid) {

    //System.out.println("clientid is "+clid);
    //System.out.println("path is "+path);
    //System.out.println("nesting is "+currentNesting);

    String backTok="../";
    String sepchar=":";

    StringBuilder src=new StringBuilder(path);
    int backs=0;
    int inc=backTok.length();

    while (src.indexOf(backTok,backs*inc)==backs*inc)
    {
        backs++;
    }

    //get rid of the source
    String suffix=src.substring(backs*inc);

    //add in internal nesting
    backs+=(currentNesting-4);

    StringTokenizer st=new StringTokenizer(clid,sepchar);

    StringBuilder sb=new StringBuilder(sepchar);
    for (int x=0;x<st.countTokens()-backs;x++)
    {
        sb.append(st.nextToken()).append(sepchar);
    }

    //backtracked path
    String p=sb.toString();

    //add suffix to the backtracked client path to get full absolute path
    String abs=p+suffix;

    return abs;
}

errrr - 我不知道為什么我在計算背面時使用了StringBuilder ,但是你明白了。 就是這樣的。

cc.parent.cliendId解析父復合組件而不是直接父組件。 獲取JSF2復合組件的父級的clientId

<composite:interface>
    <cc:attribute name="render"  type="java.lang.String"   default="@this"/>
    <cc:attribute name="action"  method-signature="java.lang.String action()">
</composite:interface>

</composite:implementation>
    <h:form>
        <h:commandLink action="#{cc.attrs.action}" value="Click"> 
            <f:ajax render="{cc.attr.render}" execute="@form"
        </h:commandLink>
    </h:form>    
</composite:implementation>

因此,您可以自行決定要渲染的組件。

<h:panelGroup layout="block" id="outside">
    #{mybean.otherdata}
    <mc:mylink action="#{mybean.click}" render=":outside"/>
</h:panelGroup>               

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM