Groovy 中的变量
我在一个使用 Grails 的项目中,
我使用 beanFields 插件,其中我将 bean:inputTemplate 更改为以下内容
<bean:inputTemplate>
<div class="prop ${hasErrors(bean:$beanName,field:'$fieldId','errors')}">${label}
<span class="value">${field}
</span>
</div>
</bean:inputTemplate>
如您所知,我尝试使用 $beanName 作为 BeanName ..那是因为 beanFields 传递 beanName 和 fieldId以及 inputTemplate 标记的更多其他属性。
但是,问题是我不能这样做。而且我真的很懒,不想花所有时间复制和粘贴相同的字段 div 并维护一个巨大的因此
,如果有人能在这种情况下提供帮助,我将非常感激。
我想在 $ { } 代码块中引用一个变量,就像在 PHP 中一样, $$variable 使用 $variable 的值作为要计算的变量的名称。
希望我说得足够清楚..并感谢您的帮助。
I'm in a project using Grails,
I user beanFields plugin where I'm changing the bean:inputTemplate into the following
<bean:inputTemplate>
<div class="prop ${hasErrors(bean:$beanName,field:'$fieldId','errors')}">${label}
<span class="value">${field}
</span>
</div>
</bean:inputTemplate>
As you can, I'm trying to use $beanName as the BeanName .. that is because beanFields passes beanName and fieldId and some more other properties to the inputTemplate tag..
But, the problem is that I can't do that.. And I'm really lazy and dont want to spend all the time copy and pasting the same field div and maintaining a huge file for that...
So, I will be really greatful if any could help in that situation.
I want to reference a variable inside the $ { } block of code, as in PHP there is $$variable that uses the value of the $variable as a name of a variable to evaluate.
Hope I was clear enought.. and thank you for helping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是直接回答你的问题,但你见过 bean-fields 插件吗?
http://grails.org/plugin/bean-fields
我认为它做了你的事情正在尝试做,以及更多
Not a direct answer to your question, but have you seen the bean-fields plugin?
http://grails.org/plugin/bean-fields
I think it does what you're trying to do, and more
beanName 前面不需要 $,它应该在范围内。
另外,我认为 beanFields 已经通过错误变量提供了错误消息。
因此,您可以测试错误是否不为空,而不是调用 hasErrors。
You shouldn't need a $ in front of beanName, it should be in scope.
Also, I think beanFields already provides the error messages via the errors variable.
So you could test to see if errors is not null instead of calling hasErrors.
在调查这个问题之后..我发现是的 beanName 被传递到模板,我不需要在 beanName 前面使用 $...
但是,当我使用 hasErrors(beans:beanName,field:'username' ,'错误')它不起作用。
但是,我可以做到这一点
即使它不起作用,它取决于域类上的验证方法
所以写这篇文章
是因为, &&失败并显示第一个 False 结果,并且其他验证方法不会执行。
因此将它们更改为这样
每个 bean 都会得到验证,并且所有字段错误都会得到正确呈现。
After investigating the issue.. I found the yeah beanName get passed to the template and I don't need to use $ in front of the beanName...
But, still when I use hasErrors(beans:beanName,field:'username','errors') it does not work.
But, I could do this
Even though, it didn't work, it depends on the validate method on domain classes
so writing this
Because, the && fails with the first False result, and the other validate methods don't get executed.
so changing them to this
Every bean gets validated, and all fields errors get rendered correctly.