Access raw expression of ValueExpression attribute to taglib component

倖福魔咒の 提交于 2019-12-05 21:49:31

As to the concrete question, you can access the ValueExpression representing the tag attribute value as defined in template client via FaceletContext#getVariableMapper() and then VariableMapper#resolveVariable() passing the tag attribute name. Then, you can get the literal expression string via ValueExpression#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

However, after that, it's not possible to put it in the EL scope via some "var", because PF 4.x would ultimately interpret the value of sortBy="#{sortBy}" literally as #{sortBy}, not as entity.name. You'd better nest it inside <p:column> and have the tag handler explicitly set it.

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

As to the functional problem you're actually trying to solve, the following works just fine for me on PF 5.2. Based on the source code they fixed it in 5.0 and improved further in 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>

Template client:

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!