如何为应用程序范围设置变量,即 Grails 控制器中的整个应用程序仅设置 1
我想为我的整个应用程序设置一个变量,并且每当请求进入特定操作(
例如
def action = {
// some integer variable is already set in application scope with name variable
variable++
render(view:"action", model:[variable])
}
In Gsp)
<html>
<body>
Variable is ${variable}
</body>
</html>
时我想更新它,输出将是
Variable is 1
// 后续请求
变量为2 等等......
谢谢
i want to set one variable for my entire application and i want to update it whenever a request comes in to a particular action
like
def action = {
// some integer variable is already set in application scope with name variable
variable++
render(view:"action", model:[variable])
}
In Gsp
<html>
<body>
Variable is ${variable}
</body>
</html>
The output would be
Variable is 1
// on subsequent requests
Variable is 2
and so on....
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于此用例,有一些机会:
不要在每个控制器操作中增加“全局”变量,而是使用过滤器。
for this use case there are some opportunities:
Don't increment your 'global' var in every controller action, use a filter instead.
它需要是整个应用程序中的一个吗?
可以通过使用(单例)服务来处理您的值,例如:
并在您需要的地方使用它
Is it needs to be one across whole app?
Its possible by using an (singleton) service, that will handle your value, like:
and the use it where you need