Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 2 自定义组件:如何将ELContext变量填充到渲染子级_Jsf 2_Custom Component - Fatal编程技术网

Jsf 2 自定义组件:如何将ELContext变量填充到渲染子级

Jsf 2 自定义组件:如何将ELContext变量填充到渲染子级,jsf-2,custom-component,Jsf 2,Custom Component,我试图通过编写一个小的“树”组件来更好地理解JSF2内部结构,该组件采用节点结构并将其呈现为简单的ul/li元素 应该可以定义这样的方式,叶的内容呈现如下(类似于h:DataTable): ... 目前,我正在努力弄清楚如何将该变量“填充”到当前上下文中。我试过这样的方法,但没用: @Override @SuppressWarnings("unchecked") public void encodeChildren(FacesContext context) throws IOExcepti

我试图通过编写一个小的“树”组件来更好地理解JSF2内部结构,该组件采用节点结构并将其呈现为简单的ul/li元素

应该可以定义这样的方式,叶的内容呈现如下(类似于h:DataTable):


...
目前,我正在努力弄清楚如何将该变量“填充”到当前上下文中。我试过这样的方法,但没用:

@Override
@SuppressWarnings("unchecked")
public void encodeChildren(FacesContext context) throws IOException {
    if ((context == null)){
        throw new NullPointerException();
    }

    ArrayList<String> list = (ArrayList<String>) getStateHelper().eval("value");
    String varname = (String) getStateHelper().eval("var");
    if(list != null){
        for(String str : list){
            getStateHelper().put(varname, str);
            for(UIComponent child: getChildren()){
                child.encodeAll(context);
            }
        }
    }
}
@覆盖
@抑制警告(“未选中”)
public void encodeChildren(FacesContext上下文)引发IOException{
if((上下文==null)){
抛出新的NullPointerException();
}
ArrayList=(ArrayList)getStateHelper().eval(“值”);
String varname=(String)getstateheloper().eval(“var”);
如果(列表!=null){
for(字符串str:list){
getStateHelper().put(varname,str);
对于(UIComponent子级:getChildren()){
全部编码(上下文);
}
}
}
}
为了简化,我首先开始使用一个简单的字符串数组列表,并以迭代方式打印内容。下面是xhtml:

<custom:tree value="#{testBean.strings}" var="testdata">
    <h:outputText value="#{testdata}" />
</custom:tree>

那么,实现这一目标的正确方法是什么

致以最良好的祝愿, Christian Voß

谢谢BalusC

为了简单起见,这基本上是(或更好的)回答我的问题:

您可以将给定键下的新变量放入requestMap,任何子组件都可以使用指定的ValueExpression访问它

@Override
@SuppressWarnings("unchecked")
public void encodeChildren(FacesContext context) throws IOException {
    if ((context == null)){
        throw new NullPointerException();
    }

    ArrayList<String> list = (ArrayList<String>) getStateHelper().eval("value");
    String varname = (String) getStateHelper().eval("var");
    Map<String, Object> requestMap = context.getExternalContext().getRequestMap();

    varStore = requestMap.get(varname); // in case this Object already exists in the requestMap,
                                        // emulate "scoped behavior" by storing the value and
                                        // restoring it after all rendering is done.

    if(list != null){
        for(String str : list){
            requestMap.put(varname, str);
            for(UIComponent child: getChildren()){
                child.encodeAll(context);
            }
        }           
        // restore the original value
        if(varStore != null){
            requestMap.put(varname, varStore);
        }else{
            requestMap.remove(varname);
        }
    }
}
@覆盖
@抑制警告(“未选中”)
public void encodeChildren(FacesContext上下文)引发IOException{
if((上下文==null)){
抛出新的NullPointerException();
}
ArrayList=(ArrayList)getStateHelper().eval(“值”);
String varname=(String)getstateheloper().eval(“var”);
Map requestMap=context.getExternalContext().getRequestMap();
varStore=requestMap.get(varname);//如果requestMap中已经存在此对象,
//通过存储值和
//在所有渲染完成后恢复它。
如果(列表!=null){
for(字符串str:list){
requestMap.put(varname,str);
对于(UIComponent子级:getChildren()){
全部编码(上下文);
}
}           
//恢复原值
if(varStore!=null){
put(varname,varStore);
}否则{
requestMap.remove(varname);
}
}
}

这是一个相当广泛的主题,因此这里只是一个指向基于JSF的开源树组件的链接,它应该提供一些见解:(您可以在底部找到源代码链接)
@Override
@SuppressWarnings("unchecked")
public void encodeChildren(FacesContext context) throws IOException {
    if ((context == null)){
        throw new NullPointerException();
    }

    ArrayList<String> list = (ArrayList<String>) getStateHelper().eval("value");
    String varname = (String) getStateHelper().eval("var");
    Map<String, Object> requestMap = context.getExternalContext().getRequestMap();

    varStore = requestMap.get(varname); // in case this Object already exists in the requestMap,
                                        // emulate "scoped behavior" by storing the value and
                                        // restoring it after all rendering is done.

    if(list != null){
        for(String str : list){
            requestMap.put(varname, str);
            for(UIComponent child: getChildren()){
                child.encodeAll(context);
            }
        }           
        // restore the original value
        if(varStore != null){
            requestMap.put(varname, varStore);
        }else{
            requestMap.remove(varname);
        }
    }
}