Jsf 以编程方式注册标记库引用
有没有办法在java代码中以编程方式注册自定义taglib引用?Jsf 以编程方式注册标记库引用,jsf,el,jsf-1.2,taglib,Jsf,El,Jsf 1.2,Taglib,有没有办法在java代码中以编程方式注册自定义taglib引用? 我使用的是JSF1.2_09、RichFaces3.3.3、JSF1.1.14 具体而言: 在这段代码中,jsf表达式语言被用来为我们做一些工作,比如在一个字段中串联两个结果或类似的东西 FacesContext ctx = FacesContext.getCurrentInstance(); Application app = ctx.getApplication(); ExpressionFactory ef = app.ge
我使用的是JSF1.2_09、RichFaces3.3.3、JSF1.1.14 具体而言: 在这段代码中,jsf表达式语言被用来为我们做一些工作,比如在一个字段中串联两个结果或类似的东西
FacesContext ctx = FacesContext.getCurrentInstance();
Application app = ctx.getApplication();
ExpressionFactory ef = app.getExpressionFactory();
ELContext elContext = ctx.getELContext();
ValueExpression valueExpression = new OverrideValueExpression(singleResult.getClass(), singleResult);
elContext.getVariableMapper().setVariable("row", valueExpression);
for (int i = 0; i < jsfDisplayValue.size(); i++){
Object value = ef.createValueExpression(elContext, jsfDisplayValue.get(i), Object.class).getValue(ctx.getELContext());
//Do something with value...
}
ELContext无法识别自定义函数,并且无法解析该函数,因为ELContext不知道tagfoo
。
如何在java类中注册taglib引用,以便ELContext能够识别自定义函数
在JSF页面上,我会这样做:
<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:tagfoo="http://tagfoo.org/tags">
附加说明我的自定义函数在jsf页面上正常工作。很抱歉我迟到了一点,但我今天偶然发现了同样的问题 通过查看ElContext和FunctionMapper实例,我发现它们确实是标准抽象类的自定义实现 因为我想保持代码的可移植性,所以我决定不参与编写特定实现的代码,因为抽象类只定义读取契约,而忽略了写入方面的内容,所以我只编写了一个自定义FunctionMapper。以下是有意义的完整代码:
public void register(String prefix, String function, Method method) {
register.put(prefix + ":" + function, method);
}
@Override
public Method resolveFunction(String prefix, String localName) {
if (register.containsKey(prefix + ":" + localName)) {
return register.get(prefix + ":" + localName);
}
for (FunctionMapper it : delegates) {
final Method current = it.resolveFunction(prefix, localName);
if (current != null) {
return current;
}
}
return null;
}
通过包装标准FunctionMapper,我可以轻松地添加自己的函数并保持标准环境正常。在我的例子中,我还必须编写一个自定义的ElContext实现,基本上克隆默认的ElContext,只包装FunctionMapper
我对结果非常满意,希望这能有所帮助。谢谢您的回复。我发现我自己需要重写
resolveFunction
方法。您不认为更好的解决方案是代码自动找到所有标记库并使用它们解析函数吗?
public void register(String prefix, String function, Method method) {
register.put(prefix + ":" + function, method);
}
@Override
public Method resolveFunction(String prefix, String localName) {
if (register.containsKey(prefix + ":" + localName)) {
return register.get(prefix + ":" + localName);
}
for (FunctionMapper it : delegates) {
final Method current = it.resolveFunction(prefix, localName);
if (current != null) {
return current;
}
}
return null;
}