播放框架:将参数传递给自定义标签

时间:2011-10-22 13:32:35

标签: java tags playframework

我想将字段的名称作为参数传递给custom Play! framework tag

  #{ifError ${_field}}
    <ul class="err">
    #{errors ${_field}}
      <li>${error}</li>
    #{/errors}
    </ul>
  #{/ifError}

但我得到的只是例外:

Template execution error (In /app/views/tags/errorList.html around line 1)
Execution error occured in template /app/views/tags/errorList.html. Exception raised was MissingMethodException : No signature of method: Template_1008.$() is applicable for argument types: (Template_1008$_run_closure1_closure2) values: [Template_1008$_run_closure1_closure2@2da75e1b] Possible solutions: _(java.lang.String), is(java.lang.Object), run(), run(), any(), get(java.lang.String).

play.exceptions.TemplateExecutionException: No signature of method: Template_1008.$() is applicable for argument types: (Template_1008$_run_closure1_closure2) values: [Template_1008$_run_closure1_closure2@2da75e1b]

如何将参数传递给我的标签?

解决方案

我稍微修改了“Codemwnci”的解决方案,最后得到了以下模板代码:

#{ifError _arg}
  <ul class="err">
  #{errors _arg}
    <li>${error}</li>
  #{/errors}
  </ul>
#{/ifError}

此模板的调用方式与#{errorList 'document.title' /}

类似

1 个答案:

答案 0 :(得分:2)

因为您已经使用标记语法#{..}在一段Groovy代码中,所以您不需要使用Expression语法(即您不需要使用${..}语法)

此外,errors标记不接受任何输入,您需要将字段名称传递给错误标记。您可以查看this documentation以了解error代码的详细信息。

以下内容适用于您

#{ifError _field}
  <ul class="err">
    <li>#{error _field /}</li>
  </ul>
#{/ifError}