Mat*_*ake 9 import grails groovy gsp
我试图在GSP中使用groovy函数.请帮忙,因为我要把头发弄到这里.
在我的GSP的顶部,我有 <%@ page import = company.ConstantsFile %>
在我的GSP里面我有
<p>
I have been in the heating and cooling business for <%(ConstantsFile.daysBetween())%>
</p>
Run Code Online (Sandbox Code Playgroud)
和我的ConstantsFile.groovy
package company
import static java.util.Calendar.*
class ConstantsFile {
def daysBetween() {
def startDate = Calendar.instance
def m = [:]
m[YEAR] = 2004
m[MONTH] = "JUNE"
m[DATE] = 26
startDate.set(m)
def today = Calendar.instance
render today - startDate
}
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将renter更改为puts,system.out等,但这不是我的主要问题.
Error 500: Internal Server Error
URI
/company/
Class
java.lang.NullPointerException
Message
Cannot invoke method daysBetween() on null object
Run Code Online (Sandbox Code Playgroud)
所以我试试
<p>
I have been in the heating and cooling business for <%(new ConstantsFile.daysBetween())%>
</p>
Run Code Online (Sandbox Code Playgroud)
但后来我明白了
Class: org.codehaus.groovy.control.MultipleCompilationErrorsException
unable to resolve class ConstantsFile.daysBetween @ line 37, column 1. (new ConstantsFile.daysBetween()) ^ 1 error
Run Code Online (Sandbox Code Playgroud)
请有人帮助我或指向一个显示该做什么的网站..我试过谷歌搜索和一切谈论ag:选择或其他类型的标签...我只是想输出像我用过的函数的结果在JSP中.
Gre*_*egg 18
首先,您的GSP导入应该是:
<%@ page import="company.ConstantsFile" %>
Run Code Online (Sandbox Code Playgroud)
其次,你的daysBetween应该是静态的(它更有意义)并且你不会从控制器以外的任何东西渲染:
class ConstantsFile {
static daysBetween() {
def startDate = Calendar.instance
def m = [:]
m[YEAR] = 2004
m[MONTH] = "JUNE"
m[DATE] = 26
startDate.set(m)
def today = Calendar.instance
return today - startDate
}
}
Run Code Online (Sandbox Code Playgroud)
第三,通过以下方式访问它:
<p>I have been in the heating and cooling business for ${ConstantsFile.daysBetween}</p>
Run Code Online (Sandbox Code Playgroud)
最后,你应该使用taglib.我正在编辑我的帖子以添加一个例子
class MyTagLib {
static namespace = "my"
def daysBetween = { attr ->
out << ConstantsFile.daysBetween()
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的GSP中使用
<p>I have been in the heating and cooling business for <my:daysBetween /></p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16674 次 |
| 最近记录: |