grails - 如何在 g :if test ="..." without providing it (or object instance) to the model? 中调用自定义函数

标签 grails

背景是:

  1. 我有布局页面main.gsp
  2. 我需要有条件地显示或隐藏菜单项
  3. 条件评估通过查询数据库完成
  4. 我不想增加每个 Controller ,所以它会评估这个条件并将结果放入模型

问题是: 我正在尝试做类似的事情

<g:if test="${checkCondition()}"> some html here </g:if>

但是它没有说未定义的 checkCondition。

但这很完美:

<mytaglib:checkCondition />

问题是: 我应该怎么做才能在

中调用我的函数
<g:if test="${ expression }">

?

最佳答案

因为您在 namespace ('mytaglib') 中有标签,您也需要将其作为具有该 namespace 的方法来调用:

<g:if test="${mytaglib.checkCondition()}"> some html here </g:if>

关于grails - 如何在 g :if test ="..." without providing it (or object instance) to the model? 中调用自定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6303518/

相关文章:

spring - 抽象类中的 Grails 服务

caching - Grails/Tomcat : Avoiding denial of service attacks

groovy - Grails 中测试/开发模式的自定义 init

unit-testing - 使用 session 时出现问题,如何决定选择request.getSession(true)还是request.getSession(false)

grails - Grails-无法从grailsCentral安装任何插件

grails - messgaes.properties在IntelliJ中不起作用

hibernate - 使用别名,条件逻辑作为条件以及grails/groovy中的嵌套条件

grails - grails datePicker使用timeZone提交日期

Grails:g:渲染 channel 数据

grails - 在内存中创建文件并让用户下载