我是grails的新手,并尝试学习grails。我做了很多谷歌搜索,但无法找到原因,请帮忙。必要的代码如下: 控制器:
class TagLibTestController {
def dateTest() {
def Date date=new Date()
date.setTime(1379874600000)
render date
}
}
的TagLib:
class DateTagLib {
def dateFromNow = { attrs,body ->
def date = attrs.date1
println "DateTagLib"
def niceDate = getNiceDate(date)
out << niceDate
}
String getNiceDate(Date date) {
def now = new Date()
def diff = Math.abs(now.time - date.time)
final long second = 1000
final long minute = second * 60
final long hour = minute * 60
final long day = hour * 24
def niceTime = ""
long calc = 0;
calc = Math.floor(diff / day)
if (calc) {
niceTime += calc + " day" + (calc > 1 ? "s " : " ")
diff %= day
}
calc = Math.floor(diff / hour)
if (calc) {
niceTime += calc + " hour" + (calc > 1 ? "s " : " ")
diff %= hour
}
calc = Math.floor(diff / minute)
if (calc) {
niceTime += calc + " minute" + (calc > 1 ? "s " : " ")
diff %= minute
}
if (!niceTime) {
niceTime = "Right now"
} else {
niceTime += (date.time > now.time) ? "from now" : "ago"
}
return niceTime
}
}
GSP页面:
<g:dateFromNow date1="${date}"/>
我们只获得日期值。 Tag Lib Class甚至不执行任何行。需要更多配置吗?