背景:
问题是: 我正在尝试做那样的事情
<g:if test="${checkCondition()}"> some html here </g:if>
但它没有说未定义的checkCondition。
但这完美无缺:
<mytaglib:checkCondition />
问题是: 我该怎么做才能在
中调用我的功能<g:if test="${ expression }">
答案 0 :(得分:6)
由于您在命名空间('mytaglib')中有标记,因此您需要将其称为具有该命名空间的方法:
<g:if test="${mytaglib.checkCondition()}"> some html here </g:if>
答案 1 :(得分:1)
将checkCondition()作为静态方法移动到类中,并在gsp中导入此类。您还可以使用此方法定义服务类,并直接从您的gsp调用此serivce方法。 最清晰的解决方案是如您所述定义自己的taglib。在标记库中,您可以使用辅助方法注入服务bean,等等。
在你的taglib中你可以做类似
的事情def checkCondition = { attrs ->
// place your condition here
if (session.user.id = 1) {
// show menu
out << "menu"
}
}