使 Grails 形成开发 DRYer [英] Making Grails form development DRYer

查看:19
本文介绍了使 Grails 形成开发 DRYer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Grails 时,呈现每个表单字段的 GSP 代码如下所示:

When using Grails, the GSP code to render each form field looks something like this:

<tr class="prop">
  <td valign="top" class="name"><label for="username">Login Name:</label></td>
  <td valign="top" class="value ${hasErrors(bean: person, field: 'username', 'errors')}">
    <input type="text" id="username" name="username" value="${person.username?.encodeAsHTML()}"/>
  </td>
</tr>

<tr class="prop">
  <td valign="top" class="name"><label for="userRealName">Full Name:</label></td>
  <td valign="top" class="value ${hasErrors(bean: person, field: 'userRealName', 'errors')}">
    <input type="text" id="userRealName" name="userRealName" value="${person.userRealName?.encodeAsHTML()}"/>
  </td>
</tr>

<tr class="prop">
  <td valign="top" class="name"><label for="passwd">Password:</label></td>
  <td valign="top" class="value ${hasErrors(bean: person, field: 'passwd', 'errors')}">
    <input type="password" id="passwd" name="passwd" value="${person.passwd?.encodeAsHTML()}"/>
  </td>
</tr>

请注意,每个表单字段都重复了几乎完全相同的 5 行 GSP/HTML 代码.这对我来说似乎不是很枯燥,我想知道其他人是否找到了更好的方法?

Notice that almost exactly the same 5 lines of GSP/HTML code is repeated for each form field. This doesn't seem very DRY to me, and I'm wondering if others have found a better approach?

我找到了 2 个试图解决这个问题的插件,表单助手bean-fields.如果有人有使用其中任何一个的经验,我会非常有兴趣听取他们的意见.或者,如果有其他解决方案/插件,请告诉我.

I've found 2 plugins which attempt to address this problem, the form helper and bean-fields. If anyone has experience using either of these, I'd be very interested to hear from them. Alternatively, if there are other solutions/plugins, please let me know.

谢谢.唐

推荐答案

使用 bean-field 插件.您的代码将变为:

Using the bean-field plugin. Your code will become:

<bean:withBean beanName="person">
    <bean:field property="username" label="Login Name:"/>
    <bean:field property="userRealName" label="Full Name:"/>
    <bean:field property="passwd" label="Password:"/>
</bean:withBean>

您能找到 DRYer 解决方案吗?

Can you find a DRYer solution?

这篇关于使 Grails 形成开发 DRYer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆