当前位置: 动力学知识库 > 问答 > 编程问答 >

html - Behavior of setting default value to a Groovy bean property

问题描述:

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.

网友答案:

I don't think this behavior is caused by Groovy, it's probably due to the configuration of your container: you can usually configure how you want the container to deal with empty values.

If you are using Tomcat, you can configure the property ALLOW_EMPTY_QUERY_STRING to true so it will allow the empty string values, check out the docs.

分享给朋友:
您可能感兴趣的文章:
随机阅读: