2

I have a simple Groovy bean that looks something like this:

class GroovyBean {
    Integer id
    String title
}

This bean is then used on a JSP page to support a basic HTML form with a text input for the title. For the database, the title column is set to be non-nullable. This meant, when the user left this field blank and submitted, which is done via a POST, an exception was thrown. I found that the following change to the bean fixed this however:

class GroovyBean {
    Integer id
    String title = ""
}

I'm a bit perplexed how this fixed it however. What I question is what is happening now when the input for the title is left blank by the user and submitted? I'd think that this empty String I set by default would be overwritten by a null sent in from the HTML form, but that's not happening. Everything here appears to work correctly, including clearing out an existing title and resubmitting. If anyone could provide me information on how the generated setter from Groovy is handling this, it would be greatly appreciated. Thank you.

4

1 回答 1

2

我不认为这种行为是由 Groovy 引起的,它可能是由于您的容器的配置:您通常可以配置您希望容器如何处理空值。

如果您使用的是 Tomcat,则可以将属性 ALLOW_EMPTY_QUERY_STRING 配置为true允许空字符串值,请查看文档

于 2013-06-14T14:18:50.700 回答