题
如何将 Groovlet 放入 Grails 应用程序中?例如,在 web-app/groovlet.groovy 中
import java.util.Date if (session == null) { session = request.getSession(true); } if (session.counter == null) { session.counter = 1 } println """ <html> <head> <title>Groovy Servlet</title> </head> <body> Hello, ${request.remoteHost}: Counter: ${session.counter}! Date: ${new Date()} <br> """
解决方案
grails install-templates
- 编辑
src/templates/web/web.xml
包括你的groovlet grails war
- 部署
我个人并没有这样做来合并 groovlet,但这是修改已部署 Grails 的记录方法 web.xml
其他提示
我理解它的方式,当你有一个带有Groovy脚本支持的Servlet容器时,会使用groovlet,
我认为在Grails中,您需要将业务逻辑代码移至控制器并离开视图部分到HTML或 GSP文件一>
沿着这些方向的东西(来自我头顶的元代码,未经测试):
的grails-app /控制器/ SampleController.groovy
class DateController {
def index = {
if (session == null) {
session = request.getSession(true);
}
if (session.counter == null) {
session.counter = 1
}
}
}
web的应用程序/样品/ index.gsp中
<html>
<head>
<title>Groovy Servlet</title>
</head>
<body>
Hello, ${request.remoteHost}: Counter: ${session.counter}! Date: ${new Date()}
<br>
希望有所帮助!
不隶属于 StackOverflow