9

我正在开发一个 Grails (1.0.4) 应用程序,我想在网格视图的单个页面上编辑集合集合。我让它工作得很好,仅取决于 Spring MVC 的索引参数处理,除了一件事:

可以通过复选框设置网格中的布尔值(或就此而言,布尔值),但不能取消设置,即当我选中复选框并更新时,该值设置为 true,但之后当我再次编辑时,取消选中复选框和更新,它仍然是真实的。

这是复选框的 GSP 代码:

<g:checkBox name="tage[${indexTag}].zuweisungen[${indexMitarb}].fixiert" value="${z.fixiert}" />

这是生成的 HTML:

<input type="hidden" name="tage[0].zuweisungen[0]._fixiert" />
<input type="checkbox" name="tage[0].zuweisungen[0].fixiert" checked="checked" id="tage[0].zuweisungen[0].fixiert"  />

我发现了一个准确描述这种效果的Grails 错误,但它在 1.0.2 中被标记为已修复,并且那里描述的问题机制(隐藏字段名称中的下划线放在错误的位置)在我的情况下不存在。

任何想法可能是什么原因?

4

5 回答 5

4

这是一个名叫 Julius Huang 的人在 grails-user 邮件列表中提出的解决方案。它是可重用的,但依赖于 JavaScript 来填充隐藏字段,其中包含 HTML 不幸未发送的未选中复选框的“错误”响应。

当使用自定义 TagLib 取消选中框(true -> false)时,我破解 GSP 以发送“false”。

默认情况下,取消选中时复选框不发送任何内容,因此我使用复选框作为事件处理程序,而是发送隐藏字段。

Controller中的“params”可以处理“false”->“true”而不需要任何修改。例如。Controller 中的一切都保持不变。

GSP 中的自定义标签用法(示例 usedfunc_F 为“true”),

<jh:checkBox name="surveyList[${i}].usedfunc_F" value="${survey.usedfunc_F}"></jh:checkBox>

这是标签生成的内容,

<input type="hidden" name="surveyList[#{i}].usedfunc_F" id="surveyList[#{i}].usedfunc_F" value="false" />
<input type="checkbox" onclick="jhtoggle('surveyList[#{i}].usedfunc_F')" checked="checked" />

Javascript

<script type="text/javascript">
function jhtoggle(obj) {
   var jht = document.getElementById(obj);
   jht.value = (jht.value !='true' ? 'true' : 'false');
}
</script>
于 2009-03-01T13:13:42.710 回答
2

这是我自己的解决方案,基本上是手动执行 grails 数据绑定应该执行的操作(但不执行)的解决方法:

Map<String,String> checkboxes = params.findAll{def i = it.key.endsWith("._fixiert")} // all checkboxes
checkboxes.each{
    String key = it.key.substring(0, it.key.indexOf("._fixiert"))
    int tagIdx = Integer.parseInt(key.substring(key.indexOf('[')+1, key.indexOf(']')))
    int zuwIdx = Integer.parseInt(key.substring(key.lastIndexOf('[')+1, key.lastIndexOf(']')))
    if(params.get(key+".fixiert"))
    {
        dienstplanInstance.tage[tagIdx].zuweisungen[zuwIdx].fixiert = true
    }
    else
    {
        dienstplanInstance.tage[tagIdx].zuweisungen[zuwIdx].fixiert = false                    
    }
}

工作,不需要更改 grails 本身,但不可重用(可能可以通过一些额外的工作来实现)。

于 2009-03-01T13:08:59.447 回答
1

I think that the simplest workaround would be to attach a debugger and see why Grails is failing to populate the value. Considering Grails is open source you'll be able to access the source code and once you figure out the solution for it you can patch your version.

I have also found this other bug GRAILS-2861 which mentions the issue related to binding to booleans (see Marc's comment in the thread). I guess that is exactly the problem you are describing.

于 2009-02-26T00:26:24.640 回答
1

我将创建一个演示问题的小型示例应用程序并将其附加到 Grails 错误(或创建一个新错误)。这里的某个人可能能够调试您的示例应用程序,否则您将证明该错误并未真正修复。

于 2009-02-25T12:03:48.077 回答
0

试试这个,将日志设置为 DEBUG,首先尝试前 3 个,如果它们没有显示问题,将它们全部翻转到 DEBUG:

codehaus.groovy.grails.web.servlet="error"  //  controllers
codehaus.groovy.grails.web.pages="error" //  GSP
codehaus.groovy.grails.web.sitemesh="error" //  layouts
codehaus.groovy.grails."web.mapping.filter"="error" // URL mapping
codehaus.groovy.grails."web.mapping"="error" // URL mapping
codehaus.groovy.grails.commons="info" // core / classloading
codehaus.groovy.grails.plugins="error" // plugins
codehaus.groovy.grails.orm.hibernate="error" // hibernate integration

这应该可以让您准确地看到参数设置失败的时间和方式,并可能找到解决方法。

于 2009-02-28T02:53:33.240 回答