访问 taglib 组件的 ValueExpression 属性的原始表达式

2023-12-27

我可以访问作为属性值传递给我的 taglib 组件的 ValueExpression 的表达式字符串吗?

我的目标是以编程方式从中导出缺失的属性值。在这种情况下,我试图避免将属性重复作为文字。

now:

<a:columnText title="name" value="#{entity.name}" sortBy="entity.name" />

期望:

<a:columnText title="name" value="#{entity.name}" />

-taglib.xml

<tag>
    <tag-name>columnText</tag-name>
    <source>column-text.xhtml</source>
    <attribute>
        <name>value</name>
        <required>true</required>
    </attribute>
    <attribute>
        <name>title</name>
        <required>false</required>
    </attribute>
    <attribute>
        <name>sortBy</name>
        <required>false</required>
    </attribute>
</tag>

列文本.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui" xmlns:h="http://java.sun.com/jsf/html"
                xmlns:a="http://www.kwa.nl/jsf/app-legacy" xmlns:c="http://java.sun.com/jstl/core">

    <c:if test="#{empty sortBy}">
        <a:expressionAsLiteral var="#{sortBy}" value="#{value}" />
    </c:if>

    <p:column headerText="#{title}" sortable="true" sortBy="#{sortBy}">
        <h:outputText value="#{value}"/>
        <a:sortByUnwrapper/>
    </p:column>
</ui:composition>

<a:expressionAsLiteral />旨在将 ValueExpression '#{value}' 解包为 '#{entity.name}' 并将 '#{sortBy}' 设置为文字 'entity.name'。在此示例中,将输入 primefaces sortBy 列。

public class ExpressionAsLiteral extends TagHandler {

    private final TagAttribute var;
    private final TagAttribute value;

    public ExpressionAsLiteral(TagConfig config) {
        super(config);
        var = getRequiredAttribute("var");
        value = getRequiredAttribute("value");
    }    

    @Override
    public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
       // abstracted for example.
       setAsLiteral(ctx, var, unwrapFaceletAttributeValue(ctx,value));
    }
}

我的调试器告诉我所需的信息隐藏在值的 ValueExpressionImpl 中private VariableMapper varMapper。我的问题是在不诉诸代码气味的情况下解开返回的 ValueExpressionImpl 。

我的 google-fu 让我失望了。我感觉我的方法完全错误,有什么建议吗?

编辑#1: 尝试了以下操作。结果是#{value}而不是所需的属性值#{iRow.title}.

valueExpressionString = value.getValueExpression(ctx, Object.class).getExpressionString();

至于具体问题,您可以访问ValueExpression表示模板客户端中定义的标签属性值FaceletContext#getVariableMapper() http://docs.oracle.com/javaee/7/api/javax/el/ELContext.html#getVariableMapper--进而VariableMapper#resolveVariable() http://docs.oracle.com/javaee/7/api/javax/el/VariableMapper.html#resolveVariable-java.lang.String-传递标签属性名称。然后,您可以通过以下方式获取文字表达式字符串ValueExpression#getExpressionString() http://docs.oracle.com/javaee/7/api/javax/el/Expression.html#getExpressionString--.

<my:expressionAsLiteral tagAttributeName="value" />
String tagAttributeName = getRequiredAttribute("tagAttributeName").getValue();
ValueExpression ve = context.getVariableMapper().resolveVariable(tagAttributeName);
String expression = ve.getExpressionString(); // #{entity.name}
String literal = expression.substring(2, expression.length() - 1); // entity.name

然而,在那之后,不可能通过一些“var”将其放入 EL 范围,因为 PF 4.x 最终会解释的值sortBy="#{sortBy}"字面意思是#{sortBy},不作为entity.name。你最好把它窝在里面<p:column>并让标签处理程序显式设置它。

<p:column>
    <my:expressionAsLiteral tagAttributeName="value" componentAttributeName="sortBy" />
    #{value}
</p:column>
String componentAttributeName = getRequiredAttribute("componentAttributeName").getValue();
parent.getAttributes().put(componentAttributeName, literal);

至于您实际想要解决的功能问题,以下内容在 PF 5.2 上对我来说效果很好。根据源代码,他们在 5.0 中修复了该问题,并在 5.1 中进一步改进。

/WEB-INF/tags/column.xhtml:

<ui:composition 
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
>
    <p:column sortBy="#{empty sortBy ? value : sortBy}">#{value}</p:column>
</ui:composition>

模板客户端:

<p:dataTable value="#{bean.items}" var="item">
    <my:column value="#{item.id}" />
    <my:column value="#{item.name}" sortBy="#{item.value}" />
    <my:column value="#{item.value}" />
</p:dataTable>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

访问 taglib 组件的 ValueExpression 属性的原始表达式 的相关文章