Jsf 将ValueExpression属性的原始表达式访问到taglib组件
我可以访问作为属性值传递给taglib组件的ValueExpression的表达式字符串吗 我的目标是以编程方式从中派生缺少的属性值。在本例中,我试图避免将属性作为文本重复 现在: 我的调试器告诉我所需的信息隐藏在值的ValueExpressionImplJsf 将ValueExpression属性的原始表达式访问到taglib组件,jsf,el,facelets,jsf-2.2,taglib,Jsf,El,Facelets,Jsf 2.2,Taglib,我可以访问作为属性值传递给taglib组件的ValueExpression的表达式字符串吗 我的目标是以编程方式从中派生缺少的属性值。在本例中,我试图避免将属性作为文本重复 现在: 我的调试器告诉我所需的信息隐藏在值的ValueExpressionImpl私有变量apper varMapper中。我的问题是在不使用代码气味的情况下展开返回的ValueExpressionImpl 我的谷歌fu让我失望了。我觉得我的方法全错了,有什么建议吗 编辑#1: 尝试了以下操作。结果是{value}而不是所需
私有变量apper varMapper
中。我的问题是在不使用代码气味的情况下展开返回的ValueExpressionImpl
我的谷歌fu让我失望了。我觉得我的方法全错了,有什么建议吗
编辑#1:
尝试了以下操作。结果是{value}
而不是所需的属性值{iRow.title}
valueExpressionString = value.getValueExpression(ctx, Object.class).getExpressionString();
至于具体问题,您可以通过访问表示模板客户端中定义的标记属性值的
值表达式,然后传递标记属性名称。然后,您可以通过获取文本表达式字符串
然而,在那之后,不可能通过一些“var”将其放入EL范围,因为PF 4.x最终将sortBy=“#{sortBy}”
的值解释为{sortBy}
,而不是entity.name
。您最好将其嵌套在
中,并让标记处理程序显式设置它
<p:column>
<my:expressionAsLiteral tagAttributeName="value" componentAttributeName="sortBy" />
#{value}
</p:column>
至于您实际要解决的函数问题,下面的内容在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>
#{value}
模板客户端:
<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>
您使用的是哪个PF版本?在PF 5.2中,模板客户端中的sortBy=“#{entity.name}”
和标记体中的sortBy=“#{value}”
对我来说都很好。Primefaces 5.2.0最初,我会在给出可能的修复后再试一次我仍然有兴趣知道如何在不使用反射cludge TagValueExpression.orig.varMapper的情况下展开。
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));
}
}
valueExpressionString = value.getValueExpression(ctx, Object.class).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
<p:column>
<my:expressionAsLiteral tagAttributeName="value" componentAttributeName="sortBy" />
#{value}
</p:column>
String componentAttributeName = getRequiredAttribute("componentAttributeName").getValue();
parent.getAttributes().put(componentAttributeName, literal);
<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>