【发布时间】:2015-07-09 08:21:58
【问题描述】:
我可以访问作为属性值传递给我的 taglib 组件的 ValueExpression 的表达式字符串吗?
我的目标是以编程方式从中获取缺失的属性值。在这种情况下,我试图避免将属性作为文字重复。
现在:
<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>
column-text.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));
}
}
我的调试器告诉我所需的信息隐藏在 value 的 ValueExpressionImpl private VariableMapper varMapper 中。我的问题是在不诉诸代码气味的情况下解开返回的 ValueExpressionImpl。
我的 google-fu 让我失望了。我觉得我的方法全错了,有什么建议吗?
编辑#1:
尝试了以下。结果为#{value},而不是所需的属性值#{iRow.title}。
valueExpressionString = value.getValueExpression(ctx, Object.class).getExpressionString();
【问题讨论】:
-
您使用的是哪个 PF 版本?在 PF 5.2 中,模板客户端中的
sortBy="#{entity.name}"和标签正文中的sortBy="#{value}"对我来说都很好。 -
Primefaces 5.2.0 最初,如果可能的修复,我会再试一次。 :) 我仍然很想知道如何在不诉诸反射丛 TagValueExpression.orig.varMapper 的情况下展开包装。
标签: jsf el facelets jsf-2.2 taglib