如何从p:repeat元素内的h:outputText获取值

时间:2018-11-21 13:59:56

标签: jsf primefaces

我正在尝试从重复组件内部的outputText组件获取值:

<p:dataTable>
...
<p:column sortBy="#{row.trainnumber}" style="text-align: right; text-overflow: ellipsis; width: 80px" exportFunction="#{columnExporter.export}">
    <f:facet name="header">
        <h:outputText value="Zugnr." title="Zugnummer" />
    </f:facet>

    <p:repeat var="item" value="#{row.values}" varStatus="aendStatus">
        <h:outputText  value="#{item.trainnumber}" />
        <h:outputText value="&lt;br /&gt;" escape="false" rendered="#{!someValue.last}" />
    </p:repeat>
</p:column>
</p:dataTable>

为此,我正在使用exportFunction。下面是示例代码:

import java.util.List;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.component.api.UIColumn;
import org.primefaces.component.repeat.UIRepeat;
import org.primefaces.util.ComponentUtils;

@Named
@ApplicationScoped
public class ColumnExporter {

    public String export(final UIColumn column) {
        String value = StringUtils.EMPTY;
        for (final UIComponent child : column.getChildren())
            value += buildValue(child);
        return value;
    }

    private String buildValue(final UIComponent uiComponent) {
        if (uiComponent.isRendered()) {
            if (uiComponent instanceof ValueHolder) {
                return ComponentUtils.getValueToRender(FacesContext.getCurrentInstance(), uiComponent);
            } else if (uiComponent instanceof UIRepeat) {
                UIRepeat repeat = (UIRepeat) uiComponent;
                return buildRepeatValue(repeat);
            } else if (uiComponent.getChildCount() > 0) {
                String value = "";
                for (UIComponent child : uiComponent.getChildren()) {
                    value += buildValue(child);
                }
                return value;
            }
        }
        return "";
    }

    private String buildRepeatValue(UIRepeat repeat) {
        String value = "";
        String variableName = repeat.getVar();
        String statusName = repeat.getVarStatus();
        List<?> values = (List) repeat.getValueExpression("value")
                .getValue(FacesContext.getCurrentInstance().getELContext());
        for (Object valueObj : values) {
            for (UIComponent child : repeat.getChildren()) {
                String childValue = buildValue(child);
                if (StringUtils.isNotEmpty(childValue)) {
                    value += childValue + "\n";
                }
            }
        }
        return value;
    }
}

但是尽管该值正确显示在表中,但我始终会得到NULL值。 有谁知道如何从outputText元素正确检索此值?

0 个答案:

没有答案