gpt4 book ai didi

jsf - 将 ValueExpression 属性的原始表达式访问到 taglib 组件

转载 作者:行者123 更新时间:2023-12-04 19:15:19 24 4
gpt4 key购买 nike

我可以访问作为属性值传递到我的 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));
}
}

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

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

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

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

Variables in trace

最佳答案

具体的问题,你可以访问ValueExpression表示通过 FaceletContext#getVariableMapper() 在模板客户端中定义的标签属性值然后 VariableMapper#resolveVariable() 传递标签属性名称。然后,你可以通过 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

然而,在那之后,不可能通过一些“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>

关于jsf - 将 ValueExpression 属性的原始表达式访问到 taglib 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31311960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com