无法让我的自定义 taglib 工作

can not get my custom taglib to work

我使用以下命令创建了我的标签库:"grails create-taglib TimeTag" 并创建了一个名为 TimeTagLib 的骨架标签。

package com.buffer

class TimeTagLib {
    static final namespace = 'myTag'
    static defaultEncodeAs = [taglib:'html']
    //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
    def weekNo = {attrs, body ->
        date = new Date()
        def calendar = date.toCalendar()
        def week = calendar.get(calendar.WEEK_OF_YEAR)
        out << body() << calendar.format('yy') + String.format("%02d", week+attrs.offset)
    }
}

我在 gsp 文件中添加了这一行:

<g:sortableColumn property="availW01" title="${timetaglib.weekno(offset: '0')}" />

我收到以下消息: 留言

Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error evaluating expression [timetaglib.weekno(offset: '0')] on line [38]: Cannot invoke method weekno() on null object
Caused by
Cannot invoke method weekno() on null object

我只是不知道该怎么做...我以为我已经很好地遵循了文档,但显然没有。 我有 grails-3.2.4

===================解决方案==================

标签库:

package com.buffer

class TimeTagLib {
    static final namespace = 'myTag'
    static defaultEncodeAs = [taglib:'html']
    //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
    def weekNo = {attrs, body ->
        Date date = new Date()
        def calendar = date.toCalendar()
        def week = calendar.get(calendar.WEEK_OF_YEAR)
        out  << String.format("V%02d", (week+(attrs.offset as Integer)))
    }
}

普惠制:

<g:sortableColumn property="availW01" title="${myTag.weekNo(offset: "0")}" />

您似乎对如何调用自定义标记库有一个简单但根本的误解。您需要使用 namespace 而不是使用 class 名称,因此您的标签库(在您的问题中)将这样调用:${myTag.weekNo(offset: 0)}<myTag:weekNo offset="0" />.